Shop OBEX P1 Docs P2 Docs Learn Events
in need of help with Simple LED lights with Ping sensor — Parallax Forums

in need of help with Simple LED lights with Ping sensor

trozanmaxtrozanmax Posts: 1
edited 2016-11-25 20:52 in Accessories
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;
}

Comments

  • kwinnkwinn Posts: 8,697
    edited 2016-11-26 04:18
    First thing I would try is to replace the "ping()" code so that it returns a value that starts at 0 and increases by 5 until it reaches 70 to see if the problem is in the code or the hardware.
  • check your ping function code aswell:

    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.
  • The odd thing is that this is listed under BasicStamp but it is an Arduino sketch. Is there support in the Arduino IDE for the BASIC Stamp?

    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.
  • PublisonPublison Posts: 12,366
    edited 2016-11-26 22:36
    Moved to Prop1 as there is no support for BS2 under SimpleIDE.
  • Actually, I believe the code is from an Arduino IDE Sketch and not SimpleIDE so it should not run on a Propeller either; that is without the proper modifications.
  • We do not have an Arduino forum for this.

    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.
Sign In or Register to comment.