Go Down

Topic: How to control traffic lights (Read 895 times) previous topic - next topic

TheTomas

Hi, i am new in arduino and i want your help. The thing is, i want to make traffic lights for EV preemtion. Traffic lights simulation for one direction just work fine. But when i give High signal(preemtion request)  to input 9 or input 10 nothing happends. all i want to do is when 9 pin signal is high, green light should turn on for one direction  while other direction turns red and vice versa. after that, traffic lights shouldn't stop and work in normal rhythm. I don't know how to control this and have watched many videos, but they are only about simple lights simulation. Please help :( Bellow is my code:

Code: [Select]
int green=2;
int yellow=3;
int red=4;
int value=9;
int i;
int n;
int value2=10;//
void setup() {
  // put your setup code here, to run once:
pinMode(green,OUTPUT);
pinMode(yellow,OUTPUT);
pinMode(red,OUTPUT);
pinMode(value, INPUT);// if signal is high preemtion for one direction (from N to E)
pinMode(value2, INPUT);// if signal is high premtion for another direction (from S to N)

}

void loop() {
  value = digitalRead(9);
  if(value==HIGH){
    turn_on_green();
    }
   
  // put your main code here, to run repeatedly:
//green
n=10;
for (i=0; i<n; i++)
{
delay(1000);
turn_on_green();
turn_on_green2();
digitalWrite(green,HIGH);

}

digitalWrite(green,LOW);
//yellow
n=5;
for (i=0; i<n; i++)
{
delay(1000);
turn_on_green();
turn_on_green2();
digitalWrite(yellow,HIGH);
}

digitalWrite(yellow,LOW);
//red
n=5;
for (i=0; i<n; i++)
{
delay(1000);
turn_on_green();
turn_on_green2();
digitalWrite(red,HIGH);
}
digitalWrite(red,LOW);
}
void turn_on_green()
{
 if (digitalRead(value)==HIGH)
 {
 //green
n=10;
for (i=0; i<n; i++)
{
delay(1000);
digitalWrite(green,HIGH);
}
digitalWrite(green,LOW);
geltona
n=5;
for (i=0; i<n; i++)
{
delay(1000);
digitalWrite(yellow,HIGH);
}
digitalWrite(yellow,LOW);
//red
n=5;
for (i=0; i<n; i++)
{
delay(1000);
digitalWrite(red,HIGH);
}
digitalWrite(red,LOW);
 }
}
void turn_on_green2()
{
 if (digitalRead(value2)==HIGH)
 {
 //green
n=10;
for (i=0; i<n; i++)
{
delay(1000);
digitalWrite(green,HIGH);
}
digitalWrite(green,LOW);
//yellow
n=5;
for (i=0; i<n; i++)
{
delay(1000);
digitalWrite(yellow,HIGH);
}
digitalWrite(yellow,LOW);
//raudona
n=5;
for (i=0; i<n; i++)
{
delay(1000);
digitalWrite(red,HIGH);
}
digitalWrite(red,LOW);
}
}

Grumpy_Mike

You need to get rid of those delays and write your code as a state machine.
See my
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0

TheTomas

Thank you for reply. Maybe someone with greater minds could help me with this?

Grumpy_Mike

As I said when you PMed me, as this is a school project many people will help but they will not do it for you. That is called cheating.

TheTomas

As I said when you PMed me, as this is a school project many people will help but they will not do it for you. That is called cheating.
It not school project. I am doing it for my self. :D i will ask for a help maybe if i will not understand something.

Go Up