CrossRoads - this button press works great - however the direction change can only occur at the beginning of the loop.
How could I adjust the code to alter direction from the point it the button is pressed during the loop? i.e. instead of 1234123412(press)3443214321, it would go 1234123412(press)143214321 (does that make sense - not sure if I am asking the question correctly)
const int buttonPin = 13;
int buttonState = 0;
int timer = 500;
void setup() {
pinMode(buttonPin, INPUT);
for (int thisPin = 3; thisPin < 13; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop()
{
if (buttonState == HIGH) {
for (int thisPin = 3; thisPin < 13; thisPin++)
{
digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW);
if (digitalRead (buttonPin) == 0){ // assumes buttonPin declared as INPUTwith internal pullup enabled
buttonState = 1-buttonState; // result is 1,0,1,0,1,0
delay(50); // crude debounce
}
}
}
else {
for (int thisPin = 12; thisPin >= 3; thisPin--) {
digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW);
if (digitalRead (buttonPin) == 0){ // assumes buttonPin declared as INPUTwith internal pullup enabled
buttonState = 1-buttonState; // result is 1,0,1,0,1,0
delay(50); // crude debounce
}
}
}
}