Hi all,
I bought myself a PING sensor and want to use it to control a LED. I got the PING working fine as the Serial Monitor is displaying the distances correctly, so the problem lies with the code added.
Basically, what I want it to do, is when an object is within 12" from the PING it turns on the light. If the object moves further than 12' from the PING the light stays on for the Interval time, then switches off.
I have gone over the code repeatedly but I can't seem to see what I am doing wrong. I have tested the LED (it works), I have tried using a different Arduino pin for LED (no difference).
Any help would be greatly appreciated
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
long previousMillis = 0; // will store last time LED off timing sequence was triggered
long interval = 5000; // interval from stepping away from machine to LED off
const int pingPin = 6; //pin for ping pulses & reading
int LED = 12; //pin for controlling LEDs based on ping and interval
void setup()
{
Serial.begin(9600);
pinMode(LED,OUTPUT);
LED = LOW;
delay(500);
}
void loop()
{
unsigned long currentMillis = millis();
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
if (inches < 12) //if an object is within 12 inches
{
LED = HIGH; //turn LED on
}
else if (inches > 12) //if object goes outside 12 inch range
{
previousMillis = currentMillis; //start counting for interval
}
else if ((inches > 12)&&(currentMillis - previousMillis > interval)) //if object is out of 12 inch range and interval time has elapsed
{
LED = LOW; //turn off LED
}
}
long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}