So the other day I posted about getting an Arduino to make an LED turn on with one switch and off with another. I want the LED to blink when I press the first switch and keep blinking until I press the second switch. with the code below, it will blink while i have the first switch pressed, but stop as soon as I let it go. If anyone knows how to extend the blinking until i press down the second switch I would very much appreciate it. Thanks
// constants won't change
const int Switch[] = {7,11};
const int LED = 13;
// variables will change:
int SwitchState = 0;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 100;
void setup() {
pinMode(LED, OUTPUT);
// initialize the pushbutton pin as an input:
for(int x=0; x<=1; x++){
pinMode(Switch[x], INPUT);
}
}
void loop(){
// read the state of the pushbutton value:
for(int x=0; x<=1; x++){
SwitchState = digitalRead(Switch[x]);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (SwitchState == HIGH && Switch[x] == 7) {
// turn LED on:
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;}
else {
ledState = LOW;}
// set the LED with the ledState of the variable:
digitalWrite(LED, ledState);
}
}
if (SwitchState == HIGH && Switch[x] == 11) {
// turn LED off:
digitalWrite(LED, LOW);
}
}
}