I am now trying to figure out how to include in my "button state high" to run one function once, than loop another function until the button goes low. My goal is when the code reads the button state as high to display the red led for 4 seconds, than turn off the red light and flash the yellow led until button state is low again. I am having trouble to find any pages online that may cover this topic so I appreciate any help. Below is the code I have so far with descriptions of what I want it to do
const int redPin = 10; //RED LED ON PIN 10
const int yelPin = 11; //YELLOW LED ON PIN 11
const int grnPin = 12; //GREEN LED ON PIN 12
const int yelswitch = 3; //SWITCH FOR YELLOW FLASH ON PIN 3
int buttonState1 = 0;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(yelPin, OUTPUT);
pinMode(grnPin, OUTPUT); //SETS OUTPUTS
pinMode(redswitch, INPUT);
pinMode(yelswitch, INPUT);
}
void loop() {
buttonState1 = digitalRead(yelswitch); //THE CODE FOR YELLOW FLASH WHEN BUTTON IS HIGH
if (buttonState1 == HIGH) {{
digitalWrite(redPin, HIGH); //RED TIME OF 4 SECONDS TO ONLY HAPPEN ONCE
delay(4000); // WHEN BUTTON IS PUSHED I ONLY WANT THE RED TO LIGHT FOR 4
digitalWrite(redPin, LOW); // THAN GO OUT AND LOOP THE YELLOW FLASH CODE BELOW UNTIL
//BUTTON IS RELEASED
digitalWrite(yelPin, HIGH); //YELLOW FLASH TO CONTINUE UNTIL BUTTON STATE IS LOW
delay(538); //THE WAY IT RUNS NOW IS TURNING THE RED ON FOR 4 SECONDS
digitalWrite(yelPin, LOW); //THAN TURNING IT OFF AND FLASHING THE YELLOW ONCE THAN LOOP
delay(538); //BACK TO THE RED FOR 4 SECONDS AGAIN WHERE I ONLY WANT THE
}} //RED TO COME UP ONCE
else {
}
//NORMAL OPP BELOW WHILE BUTTON IS LOW
else {
digitalWrite(redPin, HIGH);
delay(4000); //RED TIME
digitalWrite(redPin, LOW);
digitalWrite(grnPin, HIGH);
delay(4000); //YELLOW TIME
digitalWrite(grnPin, LOW);
digitalWrite(yelPin, HIGH);
delay(2000); //GREEN TIME
digitalWrite(yelPin, LOW);
}
}
}
I'm sorry for posting my code right in the message but I have not figured out how to put it in the code box yet.