Hey!!!
Iam new to arduino and Iam doing a project about a smart traffic lights with 2 ways , and Iam having a problem while writing the code
I want to compare the distance read by first ultrasonic sensor with the second distance which is read by the second ultrasonic sensor , inorder to make one of the green led turn on while the other turn red .
so how I could write my code ?
And thankyou !!!!
am having a problem while writing the code
You didn't post any of your code, so any help is going to be guesswork.
Do you have a question?
Rayandehaini:
I want to compare the distance read by first ultrasonic sensor with the second distance which is read by the second ultrasonic sensor , in order to make one of the green led turn on while the other turn red .
so how I could write my code ?
A = the distance read by first ultrasonic sensor
B = the second distance which is read by the second ultrasonic sensor
// I want to compare A with B
if (A > B)
{
// in order to make:
digitalWrite(ASideGreenPin, HIGH); // one of the green led turn on
digitalWrite(BSideRedPin, HIGH); // while the other turn red .
}
So post your code that reads the two distances and your best attempt at comparing and switching. We can help from there.
Steve
const int ledred=7;
const int ledred1=6;
const int ledgreen=8;
const int ledgreen1=9;
const int trigpin=10;
const int echopin=11;
const int trigpin1=12;
const int echopin1=13;
void setup(){
PinMode (ledred,OUTPUT);
PinMode (ledred1,OUTPUT);
PinMode (ledgreen,OUTPUT);
PinMode (ledgreen1,OUTPUT);
PinMode (trigpin,OUTPUT);
PinMode (trigpin1,OUTPUT);
PinMode (echopin,INPUT);
PinMode (echopin1,INPUT );
Serial.begin(9600);
}
void loop(){
long duration,distance;
digitalWrite(trigpin,LOW);
delayMicroseconds (2);
digitalWrite(trigpin,HIGH);
delayMicroseconds(2);
digitalWrite(trigpin,LOW);
duration=pulseIn(echopin,HIGH);
distance=(duration/2)/29.1;
long duration1,distance1;
digitalWrite(trigpin1,LOW);
delayMicroseconds (2);
digitalWrite(trigpin1,HIGH);
delayMicroseconds (2);(digitalWrite(trigpin1,LOW);
duration1=pulseIn(echopin1,HIGH);
distance1=(duration1/2)/29.1;
if(distance<distance1)
{digitalWrite(ledgreen,HIGH);
delay(3000);
digitalWrite(ledred1,HIGH);
delay(3000);
digitalWrite(ledgreen1,LOW);
digitalWrite(ledred,LOW);}
else if (distance1<distance)
{digitalWrite(ledred,HIGH);
delay(3000);
digitalWrite(ledgreen1,HIGH);
delay(3000);
digitalWrite(ledgreen,LOW);
digitalWrite(ledred1,LOW);}
Serial.print(distance);
Serial.println("cm");
}
(This is my code but it is not working )
Rayandehaini:
(This is my code but it is not working )
and it's not in 'code' tags, and please could you make sure any closing '}' brace is on a line by itself and use ctrl-T to format your code.
but you'll have to be a bit more precise, what isn't working ? what does the Serial monitor tell you ? can you get the Serial-monitor to tell you which distances you measure ?
long duration1,distance1;
these are likely going to be floats not Long Integers.
and like this you will probably get a more comprehensible output.
Serial.print(distance,DEC);
PinMode (ledred, OUTPUT);
Did you invent the PinMode() function or did you perhaps mean pinMode() ?
{digitalWrite(ledred,HIGH);
Why does this line start with a { ?
Please put opening and closing braces {} on lines by themselves. It makes the code MUCH easier to read.
Then put some Serial.println()s for distance and distance1 immediately after reading them because that's useful information.
When it compiles and loads, test it and then tell us what it is doing and how that differs from what you want it to do.
Steve
I put the Srial.println() for the distances but nothing happen
when I upload the code directly one of the red leds turn on and one of the green leds also as if the arduino is not reading the code .
Rayandehaini:
I put the Srial.println() for the distances but nothing happen
when I upload the code directly one of the red leds turn on and one of the green leds also as if the arduino is not reading the code .
Please post the code with the Serial.print()s in it. Auto format it in the IDE and use code tags when you post it.
I assume that you have fixed the problems pointed out with the original code
const int ledred=7;
const int ledred1=6;
const int ledgreen=8;
const int ledgreen1=9;
const int trigpin=10;
const int trigpin1=12;
const int echopin=11;
const int echopin1=13;
void setup() {
// put your setup code here, to run once:
pinMode(ledgreen,OUTPUT);
pinMode(ledgreen1,OUTPUT);
pinMode(ledred,OUTPUT);
pinMode(ledred1,OUTPUT);
pinMode(trigpin,OUTPUT);
pinMode(trigpin1,OUTPUT);
pinMode(echopin,INPUT);
pinMode(echopin1,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
float duration,distance;
Serial.print(distance,DEC);
Serial.println("cm");
digitalWrite(trigpin,LOW);
delayMicroseconds(2);
digitalWrite(trigpin,HIGH);
delayMicroseconds(2);
digitalWrite(trigpin,LOW);
duration=pulseIn(echopin,HIGH);
distance=(duration/2)/29.1;
float duration1,distance1;
Serial.print(distance1,DEC);
Serial.println("cm");
digitalWrite(trigpin1,LOW);
delayMicroseconds(2);
digitalWrite(trigpin1,HIGH);
delayMicroseconds(2);
digitalWrite(trigpin1,LOW);
duration1=pulseIn(echopin1,HIGH);
distance1=(duration1/2)/29.1;
if (distance<distance1)
{digitalWrite(ledgreen,HIGH);
delay(3000);
digitalWrite(ledred,LOW);
digitalWrite(ledred1,HIGH);
delay(3000);
digitalWrite(ledgreen1,LOW);
}
else if (distance1<distance)
{digitalWrite(ledred,HIGH);
delay(3000);
digitalWrite(ledgreen,LOW);
digitalWrite(ledred1,LOW);
digitalWrite(ledgreen1,HIGH);
delay(3000);
}
}
You are printing the values of distance and distance1 before you assign anything to them. Move the print statements down after the appropriate assignments.
I get results on one of the US sensors (I only have the one) using this code. Once you get the distance measurement working and being reporting correctly you need to work on removing as many of the delay() calls from your code as possible...
const int ledred = 7;
const int ledred1 = 6;
const int ledgreen = 8;
const int ledgreen1 = 9;
const int trigpin = 10;
const int trigpin1 = 12;
const int echopin = 11;
const int echopin1 = 13;
void setup()
{
// put your setup code here, to run once:
pinMode(ledgreen, OUTPUT);
pinMode(ledgreen1, OUTPUT);
pinMode(ledred, OUTPUT);
pinMode(ledred1, OUTPUT);
pinMode(trigpin, OUTPUT);
pinMode(trigpin1, OUTPUT);
pinMode(echopin, INPUT);
pinMode(echopin1, INPUT);
Serial.begin(9600);
while(!Serial);
}
void loop()
{
// put your main code here, to run repeatedly:
unsigned long
duration;
float
distance1,
distance2;
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
duration = pulseIn(echopin, HIGH);
distance1 = ((float)duration / 2.0) / 29.1;
digitalWrite(trigpin1, LOW);
delayMicroseconds(2);
digitalWrite(trigpin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin1, LOW);
duration = pulseIn(echopin1, HIGH);
distance2 = ((float)duration / 2.0) / 29.1;
Serial.print("Distances: "); Serial.print(distance1); Serial.print(" "); Serial.println(distance2);
Serial.print( "Result: " );
if (distance1 <= distance2)
{
Serial.println( "D1 <= D2");
digitalWrite(ledgreen, HIGH);
delay(3000);
digitalWrite(ledred, LOW);
digitalWrite(ledred1, HIGH);
delay(3000);
digitalWrite(ledgreen1, LOW);
}
else
{
Serial.println( "D2 < D1");
digitalWrite(ledred, HIGH);
delay(3000);
digitalWrite(ledgreen, LOW);
digitalWrite(ledred1, LOW);
digitalWrite(ledgreen1, HIGH);
delay(3000);
}//else
Serial.println(" *** ");
}//loop
const byte ledred = 7;
const byte ledred1 = 6;
const byte ledgreen = 8;
const byte ledgreen1 = 9;
const byte trigpin = 10;
const byte trigpin1 = 12;
const byte echopin = 11;
const byte echopin1 = 13;
void setup()
{
// put your setup code here, to run once:
pinMode(ledgreen, OUTPUT);
pinMode(ledgreen1, OUTPUT);
pinMode(ledred, OUTPUT);
pinMode(ledred1, OUTPUT);
pinMode(trigpin, OUTPUT);
digitalWrite (trigPin, LOW);
pinMode(trigpin1, OUTPUT);
digitalWrite (trigPin1, LOW);
pinMode(echopin, INPUT);
pinMode(echopin1, INPUT);
Serial.begin(9600);
while(!Serial);
}
float getRange (const byte trigPin, const byte echoPin)
{
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
unsigned long duration = pulseIn(echopin, HIGH);
return (duration / 2.0) / 29.1;
}
void loop()
{
float distance1 = getRange (trigPin, echoPin),
distance2 = getRange (trigPin1, echoPin1);
etc, etc.