Video:
My Code:
int ledPins[4] = {29, 30, 31, 32}; // LED array
int toggle = 34;
int tempo = 0;
int currentStep = 0; //"counter" variable
unsigned long lastStepTime = 0;
void setup() {
for (int i = 0; i < 4; i++) { //for loop to set up all LEDs
pinMode(ledPins[i], OUTPUT);
}
pinMode(toggle, INPUT);
}
void loop() {
tempo = analogRead(A14); //setup tempo analog input
if (digitalRead(toggle) == HIGH) {
stepForwards();
}else {
stepBackwards();
}
}
void stepForwards() {
if (millis() >= lastStepTime + tempo) { //run the function with millis() instead of delay
if (currentStep == 0) { //LED off
digitalWrite(ledPins[3], LOW);
}else {
digitalWrite(ledPins[currentStep - 1], LOW);
}
lastStepTime = millis();
digitalWrite(ledPins[currentStep], HIGH);
currentStep++; //increase "counter"
if (currentStep == 4) {
currentStep = 0; //reset "counter"
}
}
}
void stepBackwards() {
if (millis() >= lastStepTime + tempo) {
if (currentStep == 3) {
digitalWrite(ledPins[0], LOW);
}else {
digitalWrite(ledPins[currentStep + 1], LOW);
}
lastStepTime = millis();
digitalWrite(ledPins[currentStep], HIGH);
if (currentStep == 0) {
currentStep = 3;
}else {
currentStep--;
}
}
}