Digital Lecture Assignment 5

Below is my code:

int ledPin1 = 8;
int ledPin2 = 9;
int ledPin3 = 10;
int ledPin4 = 11;
int ledPinArray[4] = { 8, 9, 10, 11 };
int totalLeds = 4;
unsigned long lastStepTime = 0;
int currentStep = 0;
int totalSteps = 4;
int tempo = 400;
int backwardsSwitch = 39;


void setup() {
  for (int i = 0; i < totalLeds; i++) {
    pinMode(ledPinArray[i], OUTPUT);
  }
  pinMode(backwardsSwitch, INPUT);
  Serial.begin(9600);
}

void loop() {
  if (digitalRead(backwardsSwitch) == LOW) {
    sequenceForwards();
  }
  if (digitalRead(backwardsSwitch) == HIGH) {
    sequenceBackwards();
  }
}


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

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

void sequenceForwards() {
  if (millis() >= lastStepTime + tempo) {
    lastStepTime = millis();
    digitalWrite(ledPinArray[currentStep], LOW);
    countUp();
    digitalWrite(ledPinArray[currentStep], HIGH);
    
    Serial.println(currentStep);
  }
}

void sequenceBackwards() {
  if (millis() >= lastStepTime + tempo) {
    lastStepTime = millis();
    digitalWrite(ledPinArray[currentStep], LOW);
    countDown();
    digitalWrite(ledPinArray[currentStep], HIGH);

    Serial.println(currentStep);
  }
}

Leave a comment

Design a site like this with WordPress.com
Get started