Hi everyone, total newb here!
I have been messing with a Leonardo the past few days trying to make a sketch of seven leds that are lit in sequence without switching off the previous led, while and as long as a button is pressed. When the button is released the leds are switched off in sequence as well.
Well everything works perfect... but I am trying to do the exact same thing without using the delay function.
I have been trying the Millis(), the simpleTimer(), the isTime() and many many others... its official I'm stuck.
The code with the delay(), I am trying to rearrange is the following:
/*
Leds light on and off in sequence
Lights multiple LEDs in sequence when button is pressed, then in reverse when button is released.
*/
const int buttonPin2 = 2; // the number of the pushbutton pin
int buttonState2 = 0; // variable for reading the pushbutton status
int timer = 100; // The higher the number, the slower the timing.
void setup() {
//use a for loop to initialize each pin as an output:
for (int thisPin = 7; thisPin < 14; thisPin++) {
pinMode(thisPin, OUTPUT);
}
pinMode(buttonPin2, INPUT);
}
void loop() {
buttonState2 = digitalRead(buttonPin2);
// when pushbutton is high all related leds light on, in sequence
if (digitalRead(buttonPin2) == HIGH) {
// loop from the lowest pin to the highest:
for (int thisPin = 7; thisPin < 14; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, HIGH);
}
}
// when push button is low all related leds are turned off in sequence
else {//(digitalRead(buttonPin2) == LOW)
// loop from the highest pin to the lowest:
for (int thisPin = 13; thisPin > 6; thisPin--) {
// turn the pin on:
digitalWrite(thisPin, LOW);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
}
}
Please anything or better yet the solution to my puzzled mind in order to substitute delay() with millis() or any other solution.
Thanks in advance!
P.S. Is there a proper way to upload code in the forum so that it is more read-friendly???