Digital Electronics Lecture Assignment #5

Code:

int ledPinArray[4] = {7, 8, 9, 10};
int totalLedPins = 4;
int tempoValue = 0;
int slideSwitch = 30;
unsigned long lastStep = 0;
int currentStep = 0;
int totalSteps = 4;

void setup() {

  pinMode(slideSwitch, INPUT);                                 

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


}

void loop() {
  if (digitalRead(slideSwitch) == LOW) {
    stepForwards();
  }

  if (digitalRead(slideSwitch) == HIGH) {
    stepBackwards();
  }
}


void stepForwards() {

  tempoValue = analogRead(A13);
  if (millis() > lastStep + tempoValue) {
    lastStep = millis();
    digitalWrite(ledPinArray[currentStep], LOW);
    countUp();
    digitalWrite(ledPinArray[currentStep], HIGH);

  }
}

void stepBackwards() {

  tempoValue = analogRead(A13);
  if (millis() > lastStep + tempoValue) {
    lastStep = millis();
    digitalWrite(ledPinArray[currentStep], LOW);
    countDown();
    digitalWrite(ledPinArray[currentStep], HIGH);


  }
}


void countUp() {

  currentStep++;
  if (currentStep == totalSteps) {
    currentStep = 0;

  }
}

void countDown() {

  currentStep--;
  if (currentStep < 0) {
    currentStep = 3;
  }

}


Video:

Leave a comment

Design a site like this with WordPress.com
Get started