I have a program that flashes three LED in sequence indefinitely. How can O program a pushbutton to pause the program in the middle of the code?
Also, can I modify the code so that pressing the button a second time would speed up the flashing of the LED?
Each time I try to use a pushbutton to pause the program, it only pauses it once the program loops back to the beginning.
Here's my code at the moment (sorry, it's not very clean):
int ledpins[] = {4, 5, 6};
int buttonpin = 3;
int buttonstate = 0;
void setup() {
Serial.begin(9600);
for (int pin = 6; pin > 3; pin--) {
pinMode(ledpins[pin], OUTPUT);
}
pinMode(buttonpin, INPUT);
}
void loop() {
buttonstate = digitalRead(buttonpin);
if (buttonstate == HIGH) {
digitalWrite(ledpins, HIGH);
}
if (buttonstate == LOW) {
for (int pin = 4; pin < 7; pin++) {
digitalWrite(pin, HIGH);
delay(200);
digitalWrite(pin, LOW);
}
for (int pin = 6; pin > 3; pin--) {
digitalWrite(pin, HIGH);
delay(200);
digitalWrite(pin, LOW);
}
}
}
setup(), you are accessingledpins[6], which is out of bounds. 2) Inloop(),digitalWrite(ledpins, HIGH)makes no sense, asledpinsis not an integer.