Assignment 5

Video Link

Code:

int delayPot = A14;
int mappedDelay = 0;

int ledPins[4] = {8, 9, 10, 11};
int numLedPins = 4;

unsigned long lastStepTime = 0;
int currentStep = 0;
int totalSteps = numLedPins;

int directionSwitch = 32;

void setup() {
  pinMode(directionSwitch, INPUT);

  for (int i = 0; i < numLedPins; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  mappedDelay = map(analogRead(delayPot), 0, 1023, 100, 1000);

  if (digitalRead(directionSwitch) == HIGH) {
    stepForward();
  }
  else if (digitalRead(directionSwitch) == LOW) {
    stepBackward();
  }
}

void stepForward() {
  if (millis() >= lastStepTime + mappedDelay) {
    lastStepTime = millis();
    digitalWrite(ledPins[currentStep], LOW);
    countUp();
    digitalWrite(ledPins[currentStep], HIGH);
  }
}

void stepBackward() {
  if (millis() > lastStepTime + mappedDelay) {
    lastStepTime = millis();
    digitalWrite(ledPins[currentStep], LOW);
    countDown();
    digitalWrite(ledPins[currentStep], HIGH);
  }
}

void countUp() {
  currentStep++;
  if (currentStep == totalSteps) {
    currentStep = 0;
  }
}

void countDown() {
  currentStep--;
  if (currentStep < 0) {
    currentStep = totalSteps;
  }
}

Design a site like this with WordPress.com
Get started