Problem on traffic light with on/off button

Hi guys, I'm new to Arduino and this forum also.

Currently I have a task to simulate the traffic light with a switch button.
What I wanted:
Press button > Turns traffic light on (indefinitely looped) > Press a button once more > All LEDs off (or break the loop) > Press button again > Traffic light turns on again > ...

I have the script below, which gave me an uninterruptible infinite loop of traffic light (cannot be turned off).
Tried searching in this forum, but only similar problems exist (not the ones I intent to)

Any ideas on this (should be) simple problem?
Thanks in advance for the help :slight_smile:

const int RED = 7;
const int YEL = 8;
const int GRE = 12;
const int BUTTON = 2;
int val = 0;
int state = 0;
void setup()
{
  pinMode(RED, OUTPUT);
  pinMode(YEL, OUTPUT);
  pinMode(GRE, OUTPUT);
  pinMode(BUTTON, INPUT);
}
void loop()
{
  val=digitalRead(BUTTON);
  if(val==HIGH)
  {state=1-state;}
  if(state>0)
  {light();}
   else {digitalWrite(RED, LOW);digitalWrite(YEL, LOW);digitalWrite(GRE, LOW);}
}
void light()
{
  digitalWrite(RED, HIGH);
   if(digitalRead(RED)==HIGH){digitalWrite(YEL, LOW);digitalWrite(GRE, LOW);}
   delay(2000);
   digitalWrite(YEL, HIGH); delay(1000);
   digitalWrite(GRE, HIGH);
   if(digitalRead(GRE)==HIGH){digitalWrite(RED, LOW);digitalWrite(YEL, LOW);}
   delay(2000);
}

In short way, I think that the arduino ignored the input button while doing the loop.

  pinMode(BUTTON, INPUT);

How is the button sewn onto the Arduino? Do you have an external resistor? Why are you not using the internal pullup resistor and much simpler wiring?

In short way, I think that the arduino ignored the input button while doing the loop.

Of course it does, because you used delay(). There must be something like 779767658 tutorials around showing how NOT to use delay(). Find one and pay heed.

Hi,
Welcome to the forum.

How have you got your button wired.
From 5V to digital input?
If so you need to add a 10K resistor from the digital input to gnd.

When you press the button, you put 5V on the input pin (HIGH).
When you release it, the input pin is now open circuit, it usually keeps the 5V charge on it from pressing the button(stays HIGH).
You need the 10K to pull the input to gnd (LOW) when the button is not pressed.

Tom... :slight_smile:

Thanks for the fast reply guys!
Here is my circuit:

PaulS:
How is the button sewn onto the Arduino? Do you have an external resistor? Why are you not using the internal pullup resistor and much simpler wiring?

Umm... I just got to know Arduino a few days ago and I just heard about that :cold_sweat: . Thanks, i'll look up for it!

Of course it does, because you used delay(). There must be something like 779767658 tutorials around showing how NOT to use delay(). Find one and pay heed.

Did you mean to use millis function instead?

TomGeorge:
If so you need to add a 10K resistor from the digital input to gnd.

You need the 10K to pull the input to gnd (LOW) when the button is not pressed.

Like the one in my attached image, right? Or is there anything else wrong?

Sam.

Did you mean to use millis function instead?

Using millis() is part of the solution. Using Tools + Auto Format is another part. Your piss-poorly laid out code is just too damned difficult to read.

Thereisnoexcusefornotusingspacesorcarriagereturnsbeforeand/oraftercurlybraces.

PaulS:
Using millis() is part of the solution. Using Tools + Auto Format is another part. Your piss-poorly laid out code is just too damned difficult to read.

Thereisnoexcusefornotusingspacesorcarriagereturnsbeforeand/oraftercurlybraces.

Lol sorry, i forgot to add the comments too.
Thanks anyway :slight_smile:

Sam.

Hi,
Have you checked with a DMM that the button is working?

Those tactile buttons can be connected many ways, one of which makes you think the switch is ON all the time, or OFF all the time.

Tom... :slight_smile:

How have you got your button wired.
From 5V to digital input?
If so you need to add a 10K resistor from the digital input to gnd.

Is it only old farts like me (went to college in the 80’s or before) who cringe at wiring a switch like that? We would never even think of doing anything but using a 10K pull up on the input and have the switch pull it to ground.

Of course, we also only connected a TTL gate to light an LED by sinking current.

gfvalvo:
Is it only old farts like me (went to college in the 80’s or before) who cringe at wiring a switch like that?

I don't cringe but I do wince. ;D It always seems just a little.... wrong.