in need of help with Simple LED lights with Ping sensor
trozanmax
Posts: 1
wrote this code to turn on three different LED lights when the ping sensor senses three different distance. Green for .3 m and less, yellow for .3 m to .7 m, and red for above .7m.
My green and yellow LED work with the code and turn on and off at their appropriate distances but my red LED will not turn on at any distance. The red LED works, just unable to get the code below to run it on when the ping sensor is reading above .7 m
#define fP 10 //Pin the ping sesnor to pin 10
void setup()
{
pinMode(13, OUTPUT); //GREEN
pinMode(12, OUTPUT); //RED
pinMode(11, OUTPUT); //YELLOW
//declare output to all LED light and corresponding pin
}
void loop()
{
if (ping() > 70) //Search mode, greater than .7 m, turn on red LED
{
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
digitalWrite(13, LOW);
}
if ((ping() > 30) && (ping() < 70)) //Move torward mode, between .3 m and .7 m, turn on yellow LED
{
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
if (ping() < 30) //attack mode, turn on green LED
{
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
}
}
int ping() //introduce ping
{
long duration; //ping function
pinMode(fP, OUTPUT);
digitalWrite(fP, LOW);
delayMicroseconds(2);
digitalWrite(fP, HIGH);
delayMicroseconds(15);
digitalWrite(fP, LOW);
delayMicroseconds(20);
pinMode(fP, INPUT);
duration = pulseIn(fP, HIGH);
return duration / 29.0 / 2.0;
}
My green and yellow LED work with the code and turn on and off at their appropriate distances but my red LED will not turn on at any distance. The red LED works, just unable to get the code below to run it on when the ping sensor is reading above .7 m
#define fP 10 //Pin the ping sesnor to pin 10
void setup()
{
pinMode(13, OUTPUT); //GREEN
pinMode(12, OUTPUT); //RED
pinMode(11, OUTPUT); //YELLOW
//declare output to all LED light and corresponding pin
}
void loop()
{
if (ping() > 70) //Search mode, greater than .7 m, turn on red LED
{
digitalWrite(12, HIGH);
digitalWrite(11, LOW);
digitalWrite(13, LOW);
}
if ((ping() > 30) && (ping() < 70)) //Move torward mode, between .3 m and .7 m, turn on yellow LED
{
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
if (ping() < 30) //attack mode, turn on green LED
{
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(11, LOW);
}
}
int ping() //introduce ping
{
long duration; //ping function
pinMode(fP, OUTPUT);
digitalWrite(fP, LOW);
delayMicroseconds(2);
digitalWrite(fP, HIGH);
delayMicroseconds(15);
digitalWrite(fP, LOW);
delayMicroseconds(20);
pinMode(fP, INPUT);
duration = pulseIn(fP, HIGH);
return duration / 29.0 / 2.0;
}
Comments
int ping()
{
long duration;
...
return duration/ 29.0 / 2 .0
You're mixing int, long and floating point. Not sure what the compiler makes of that.
However, the example that is shown is a basic Arduino example for using the ping sensor (I have something similar for sweeping the ping sensor and controlling servos) and the "return duration 29.0 / 2.0" converts the duration to centimeters. In this instance, the duration is usually defined as a long and the function return value is set as long as well. The return value of the ping() function is an "int" so this might cause an issue. Since duration is long, I would remove the ".0" from the values since that might get promoted to a float; that is unless that is your intent in which case you might want to adjust the rest of your code accordingly. Also, you might want to break out the conversion of duration to centimeters into a separate function where duration is passed as a long and the return value is long as well; my ping function does not return a value and is set to void.
Also, in the example I have, I do not have a "delayMicroseconds(20)" in between the "digitalWrite(fP, LOW);" and "pinMode(fP, INPUT);". Unless you have a particular reason for having that there, try commenting that out and see what happens.
Good luck.
Accessories would be appropriate, as the issue is regarding code for a PING))) sensor and simple LEDs. Moving this thread again; hopefully it will be more visible to forum users who can help.