INSTANT EXECUTION OF PUSH BUTTON INSTRUCTIONS??

Ok, so here’s my question. I have my code to run a traffic light, and 2 switch inputs to send the signal to flash red or yellow flash. the problem is that when I flip the switch I have to wait until the program gets to spot where my digitalRead(redswitch) code is. My goal would be to go into flash the instant the switch is activated and to stay in flash until the switch is flipped back. Could anyone shed some light on how this may be done?

I have included the code I have so far

Thanks =)

working_button.ino (1.34 KB)

delay(4000);

that is your problem...

the delay() function blocks the program from progressing. You need to eliminate the delays so you should start by looking at the BlinkWithoutDelay.ino sketch included in the arduino examples.

store the light status in a variable.

Something like this>

if(buttonPressed){
if(green == true) {red = false; yello = true; green = false;}
else if(red == false && yello == true) {red = true; yello = false; green = false;}
else if(red == true && yello == true) {red = false; yello = false; green = true;}
else if(red == true && yello == false) {red = true; yello = true; green = false;}
}

and then put ON or OFF the appropriate LED by:

digitalWrite(redPin, red);
digitalWrite(yelPin, yello);
digitalWrite(grnPin, green);

the program will keep LEDs ON/OFF untill you press the button.

Look at the demo several things at a time

…R

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.

Select your code, then press this

Or just type the code tags before and after your code like this:

[code]
// your code
[code]

dlloyd:
Select your code, then press this

Or just type the code tags before and after your code like this:

[code]

// your code

[code]

We should have a button to insert this message. Right there beside the “reply” button.

steve101100:
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.

If you organize your code into functions (as in the demo several things at a time and the Thread planning and implementing a program) you separate the actions from the control logic and things are much easier to figure out.

...R

if (buttonState1 == HIGH)

No. You want to check if the button has changed to HIGH, not if it is HIGH. Look up 'State Change'.