Coding trouble

Good day, im programming a two way traffic light with two switches..the first switch is to turn on the normal lights settings and take off the normal light settings or the second switch function. the second switch have to take out the normal light and put the two red lights to blink and also take off the lights..however when the first switch is pressed the lights work but pressing the switch back do not take off the lights..same for the second switch..the board needs to be reset in order to take off the lights and then either switch can be pressed....below is the code i used, any suggestions how to fix it will be greatly appreciated...

/buttons

int button1 =2;

int button2 = 3;

// light one

int red1 = 11;

int yellow1 = 12;

int green1 = 7;

// light two

int red2 = 8;

int yellow2 = 9;

int green2 = 10;

void setup(){

// light one

pinMode(red1, OUTPUT);

pinMode(yellow1, OUTPUT);

pinMode(green1, OUTPUT);

// light two

pinMode(red2, OUTPUT);

pinMode(yellow2, OUTPUT);

pinMode(green2, OUTPUT);

//buttons

pinMode ( button1, INPUT);

pinMode ( button2, INPUT) ;

}

void loop(){

if(digitalRead(button1) == HIGH)

for(int thisPin = 7; thisPin<12; thisPin++)

{

digitalWrite(green1, HIGH);

digitalWrite(yellow1, LOW);

digitalWrite(red2, HIGH);

delay(15000);

digitalWrite(red2,HIGH);

digitalWrite(yellow1, HIGH);

digitalWrite(green1,LOW);

delay(3000);

digitalWrite(yellow1,LOW);

digitalWrite(red1,HIGH);

digitalWrite(green2,HIGH);

digitalWrite(red2,LOW);

delay(10000);

digitalWrite(green2,LOW);

digitalWrite(yellow2,HIGH);

digitalWrite(red1,HIGH);

delay(3000);

digitalWrite ( yellow2, LOW);

digitalWrite ( red1, LOW);

}

else

{

(digitalRead(button1) == LOW);

}

if(digitalRead(button2) == HIGH)

for(int thisPin = 11; thisPin =8; thisPin++)

{

digitalWrite(red2,HIGH);

digitalWrite(red1,HIGH);

delay(3000);

digitalWrite(red1,LOW);

digitalWrite(red2,LOW);

delay(3000);

}

else

{

(digitalRead(button2) == LOW);

}

}

You may make a common mistake when using button. See Button FAQ: common mistake - button does NOT work as expected. - Introductory Tutorials - Arduino Forum

And lease format code on your post.

You need to get rid of the delays and that is going to take learning how to make multiple things happen at the same time.

If you control 1 led +and+ watch a switch at the same time, that's more than one thing. When delay() runs, NOTHING ELSE DOES.

Try this tutorial: Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs

First he shows the use of delay and how that leads to the problem that will be solved.
Then he explains the lesson in commonsense terms:

Let's look at an analogy. Say you want to cook breakfast. You need to cook:

Coffee - takes 1 minute
Bacon - takes 2 minutes
Eggs - takes 3 minutes

Now a seasoned cook would NOT do this:

Put coffee on. Stare at watch until 1 minute has elapsed. Pour coffee.
Cook bacon. Stare at watch until 2 minutes have elapsed. Serve bacon.
Fry eggs. Stare at watch until 3 minutes have elapsed. Serve eggs.

The flaw in this is that whichever way you do it, something is going to be cooked too early (and get cold).

In computer terminology this is blocking. That is, you don't do anything else until the one task at hand is over.

What you are likely to do is this:

Start frying eggs. Look at watch and note the time.
Glance at watch from time to time. When one minute is up then ...
Start cooking bacon. Look at watch and note the time.
Glance at watch from time to time. When another minute is up then ...
Put coffee on. Look at watch and note the time.
When 3 minutes are up, everything is cooked. Serve it all up.

In computer terminology this is non-blocking. That is, keep doing other things while you wait for time to be up.

And from there he shows how to work with the Arduino millis() function (returns ms since startup) to make things happen on time.

The second tutorial I list below teaches about state machine code, a way to make different code run for different situations, process according to what has happened so far. The main loop runs many times (as fast as possible if you want smooth action) with just a little bit done each run... so the led code +and+ the button code =both= get attention every time loop runs which lets them work together and do different things to match changing conditions like button press detected.

If you take your time and learn these two things, you will be able to write automation code.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Are you using press buttons or toggle switches?

Thanks.. Tom.. :slight_smile:

{

(digitalRead(button2) == LOW);

}

What did you intend there?