Hello there! I am a newbie when it comes to this kind of technology. Currently, I am building my project which is a 2-way traffic light for road construction purpose. Both roads have 1 ultrasonic sensor each for detecting which side would have a go and stop signal, which has 15 seconds(3mins actual) delay for switching lights, but I kinda want it to have a 5 seconds delay for the lights to switch if the green side has already all cars left while the red side has many vehicles queuing. I would be glad to hear some response
Difficult to figure out the thought process that put this question in "website and forum"
Moved
Study up on state machines. Sounds like a project ripe for one.
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?
Show us what code you have working and we can advise on how to go about modifying your code.
Thanks.. Tom...
Here it is sir.
#define trigPin 11
#define echoPin 12
#define Gled 13
#define Rled 6
#define trigPin2 2
#define echoPin2 3
#define Gled2 7
#define Rled2 5
const int switchDelay = 20000;
const int vehicleDelay = 10000;
unsigned long PreviousMillis = 0;
unsigned long CurrentMillis;
void sensorLoop(){
long duration, distance, duration2, distance2;
digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
digitalWrite(trigPin2, LOW);
duration = pulseIn(echoPin, HIGH);
duration2 = pulseIn(echoPin2, HIGH);
distance = (duration/2)/29.1;
distance2 = (duration2/2)/29.1;
CurrentMillis = millis();
if (distance <= 5 ){
TL_Seq_1();
}
else if (distance > 5 && CurrentMillis - PreviousMillis == vehicleDelay){
TL_Seq_2();
}
else{
TL_OFF();
}
delay (1000);
if (distance2 <= 5 ){
TL_Seq_2();
}
else if (distance2 > 5 && CurrentMillis - PreviousMillis == vehicleDelay){
TL_Seq_1();
}
else{
TL_OFF();
}
delay(500);
//return loop();
}
void TL_Seq_1(){
digitalWrite(Gled, HIGH);
digitalWrite(Rled2, HIGH);
digitalWrite(Gled2, LOW);
digitalWrite(Rled, LOW);
switchDelay;
}
void TL_Seq_2(){
digitalWrite(Gled, LOW);
digitalWrite(Rled2, LOW);
digitalWrite(Gled2, HIGH);
digitalWrite(Rled, HIGH);
switchDelay;
}
void TL_OFF(){
digitalWrite(Gled, LOW);
digitalWrite(Rled2, LOW);
digitalWrite(Gled2, LOW);
digitalWrite(Rled, LOW);
}
void setup()
{
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(Gled, OUTPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(Gled2, OUTPUT);
pinMode(Rled, OUTPUT);
pinMode(Rled2, OUTPUT);
}
void loop()
{
sensorLoop();
}
TL_Project.ino (1.9 KB)
Please read reply #3
Please do check it if it's okay already sir.