Hey all, I'm new to coding Arduinos, so it would be much appreciated if you could help me out. I'm trying to get the onboard LED to blink on and off for 100 ms each when I press one switch and then stop blinking and turn off when I press a second switch. I've figured out how to get the LED to turn on with the first switch and off with the second switch with this code:
//variables that don't change
const int Switch1=7;
const int Switch2=11;
//variables that do change
int currentS1state=0;
int lastS1state=0;
int currentS2state=0;
int lastS2state=0;
void setup (){
pinMode(Switch1,INPUT);
pinMode(Switch2,INPUT);
pinMode(13,OUTPUT);
}
void loop(){
currentS1state = digitalRead(Switch1);
lastS1state = digitalRead(Switch1);
currentS2state = digitalRead(Switch2);
lastS2state = digitalRead(Switch2);
if (currentS1state != lastS1state){
if(currentS1state == HIGH){
digitalWrite(13,HIGH);
}
}
if (lastS1state == LOW){
if (currentS2state == HIGH){
digitalWrite(13,LOW);
}
}
}
When I add a delay to the LED to make it blink, it only turns on for 100 ms and then off, which I guess is due to the nature of delay. I've tried messing around with millis(), but I haven't been able to get it to work inside the if statement. Thanks for all the help.