Pages: [1]   Go Down
Author Topic: Traffic light simulator/Red light "camera" programming  (Read 1141 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm trying to make a light simulation where when one switch it hit, it causes the other light to sequence to red- making the light where the switch is located to turn green

if someone was to run the red light, I want the "police lights" to turn on.  How I have it rigged is that if the one switch is hit (which turns the opposing light red) and then another switch after it is hit (meaning the car is running the red light) it should turn the police lights on.

Right now, I can't get any interrupt functions to work, as once the first button is pressed it starts the sequence of turning the opposing light red-- and does not allow the 2nd switch to be noticed..

Code:
int PoliceRed = 13;
int Yellow1 = 12;
int Green1 = 11;
int Green2 = 10;
int Yellow2 = 9;
int Red2 = 8;
int Red1 = 7;
int PoliceBlue = 5;
int ButtonStop = 3;
int ButtonRun = 2;


void setup()
{ pinMode(Red1, OUTPUT);
pinMode(Green1, OUTPUT);
pinMode(Yellow1, OUTPUT);
pinMode(ButtonStop, INPUT);
pinMode(Red2, OUTPUT);
pinMode(Yellow2, OUTPUT);
pinMode(Green2, OUTPUT);
pinMode(ButtonRun, INPUT);
pinMode(PoliceRed, OUTPUT);
pinMode(PoliceBlue, OUTPUT);

}





void loop(){
noInterrupts();
delay(1000);
if (digitalRead(ButtonStop)==LOW)
{delay(100000);
digitalWrite(Green1, LOW);
digitalWrite(Yellow1, HIGH);
delay(100000);
digitalWrite(Yellow1, LOW);
digitalWrite(Red1, HIGH);
digitalWrite(Yellow2, LOW);
delay(100000);
   digitalWrite(Green2, HIGH);
   digitalWrite(Red2, LOW);
delay(100000);

digitalWrite(Green2, LOW);
digitalWrite(Yellow2, HIGH);
delay(100000);
digitalWrite(Yellow2, LOW);
digitalWrite(Red2, HIGH);
digitalWrite(Yellow1, LOW);
delay(100000);
   digitalWrite(Green1, HIGH);
   digitalWrite(Red1, LOW);


   digitalWrite(Green2, LOW);
   digitalWrite(Yellow2, LOW);
   delay(100000);
   digitalWrite(Green1, HIGH);}
  

if (digitalRead(ButtonStop) == HIGH)
{  digitalWrite(Red2, HIGH);
   digitalWrite(Yellow2, LOW);
  digitalWrite(Green2, LOW);
  digitalWrite(Green1, HIGH);
  digitalWrite(Red1, LOW);
  digitalWrite(Yellow1, LOW);
      
}
interrupts();
if (digitalRead(ButtonRun)==LOW)
{
  digitalWrite(PoliceRed, HIGH);
digitalWrite(PoliceBlue,LOW);
delay(200);
digitalWrite(PoliceRed, LOW);
digitalWrite(PoliceBlue, HIGH);
delay(200);
digitalWrite(PoliceRed, HIGH);
digitalWrite(PoliceBlue, LOW);
delay(200);
digitalWrite(PoliceRed, LOW);
digitalWrite(PoliceBlue, HIGH);
delay(200);
digitalWrite(PoliceRed, HIGH);
digitalWrite(PoliceBlue, LOW);
delay(200);
digitalWrite(PoliceRed, LOW);
digitalWrite(PoliceBlue, HIGH);
delay(200);
digitalWrite(PoliceRed, HIGH);
digitalWrite(PoliceBlue, LOW);
delay(200);
digitalWrite(PoliceRed, LOW);
digitalWrite(PoliceBlue, HIGH);
delay(200);
digitalWrite(PoliceRed, HIGH);
digitalWrite(PoliceBlue, LOW);
delay(200);
digitalWrite(PoliceRed, LOW);
digitalWrite(PoliceBlue, HIGH);
delay(200);
digitalWrite(PoliceRed, HIGH);
digitalWrite(PoliceBlue, LOW);
delay(200);
digitalWrite(PoliceRed, LOW);
digitalWrite(PoliceBlue, HIGH);
delay(200);
digitalWrite(PoliceRed, HIGH);
digitalWrite(PoliceBlue, LOW);
delay(200);
digitalWrite(PoliceRed, LOW);
digitalWrite(PoliceBlue, HIGH);
delay(200);
digitalWrite(PoliceRed, HIGH);
digitalWrite(PoliceBlue, LOW);
delay(200);
digitalWrite(PoliceRed, LOW);
digitalWrite(PoliceBlue, HIGH);
delay(200);
digitalWrite(PoliceRed, HIGH);
digitalWrite(PoliceBlue, LOW);
delay(200);
digitalWrite(PoliceRed, LOW);
digitalWrite(PoliceBlue, HIGH);
delay(200);
digitalWrite(PoliceRed, HIGH);
digitalWrite(PoliceBlue, LOW);
delay(200);
digitalWrite(PoliceRed, LOW);}
}
« Last Edit: April 28, 2012, 06:07:28 pm by txjeeptj » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the "read light camera" part is the police lights going on by the way
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 498
Posts: 19059
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
digitalWrite(PoliceRed, LOW);
digitalWrite(PoliceBlue, HIGH);
delay(200);
digitalWrite(PoliceRed, HIGH);
digitalWrite(PoliceBlue, LOW);
delay(200);

Where to start? I suggest you read up on "for" loops.


Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 498
Posts: 19059
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Right now, I can't get any interrupt functions to work, as once the first button is pressed it starts the sequence of turning the opposing light red-- and does not allow the 2nd switch to be noticed..

Code:
...
if (digitalRead(ButtonStop)==LOW)
{delay(100000);
...

It certainly won't be noticed for 100 seconds (1.6 minutes) because you told it to do nothing for that time.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

accidental post
« Last Edit: April 28, 2012, 06:09:39 pm by txjeeptj » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Right now, I can't get any interrupt functions to work, as once the first button is pressed it starts the sequence of turning the opposing light red-- and does not allow the 2nd switch to be noticed..

Code:
...
if (digitalRead(ButtonStop)==LOW)
{delay(100000);
...

It certainly won't be noticed for 100 seconds (1.6 minutes) because you told it to do nothing for that time.

yeah, the reason I had to do that was because if I didn't it ran through the sequence extremely fast, I'm really confused.  Let me read up on for loops some more and I'll get back to ya!

thanks
Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
Power up to your potential
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This sketch could be tested on
Simulator for Arduino
- the free version has a delay of 30s. Probably a good idea to change the 100second delay to a shorter delay.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great info, and I agree. The delay caused me issues with my first project. When you use this delay in this amount of time, basically your putting the processor to sleep. In a sense. Your telling it to do nothing for that period of time. So if a event happens during that elapsed time, it will be missed. I'm still very new at this too, and this bit me in the rear as well. I  use the interrupts to detect my events. Try reading on those maybe. It's gots some great info on them. Just remember on interrupts that they are not supposed to return anything and you can't use delays in them, I believe.
Watch me get my hand swatted for that
Logged

Pages: [1]   Go Up
Jump to: