I am trying to get a PING sensor to turn on one led when the distance is increasing and turn on another led when the distance is decreasing. I have the code up to the point where I can turn one on and off based on a set distance. Even though they are kind of glitchy at the moment. Any advice on how to make the distances incremental? I haven't been able to get the increment (++) and decrement (--) to work but maybe I'm doing it wrong.
Here's my code:
int led = 13;
int led2 = 12;
const int pingPin = 7;
// the setup routine runs once when you press reset:
void setup() {
//initialize serial communication
Serial.begin(9600);
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// get Distance
long duration, inches;
// to set speakerPin and servoPin mode to OUTPUT
// pinMode(servoPin, OUTPUT);
// The PING Sensor 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 Sensor, 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);
if (inches < 5) {
digitalWrite(led,HIGH);
digitalWrite(led2,LOW);
}
if (inches > 8) {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
}
long microsecondsToInches(long microseconds)
{
return microseconds/74/2;
}
I altered what pins everything was connected with and tried the increment and decrement. It sort of works, but the two leds don't ever turn completely off or on. It's like the one will flicker when it's supposed to be off. Is there a way to turn these completely off and on via code?
Adjustments to previous post:
int led = 10;
int led2 = 9;
const int pingPin = 6;
int x =5;
if (inches < x++) {
digitalWrite(led,HIGH);
digitalWrite(led2,LOW);
}
if (inches > x--) {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
Do you have a suggestion for how I would store the distance in another variable after the loop? I'm wondering if it has something to do with the duration being set to HIGH.
Could that make the sensor go back to the led that is set to HIGH when it isn't supposed to? I just didn't see any alternative to set it to other then HIGH.
michinyon:
I can't see why you would have the pinMode() function calls in loop().
Unless you are changing the use of the pins, you should set them once, in setup().
Some of the PING sensors have 3 pins (VCC, DATA & GND) and you have to pulse the DATA pin (as output) to trigger a distance reading and then read the result using same pin (as input). Other PING sensors have an extra pin that is used for trigger so the DATA pin will always be input.
If I move the pinMode() function into setup the leds don't change at all. I have gotten them to work at a set distance.
if (inches < x) {
digitalWrite(led,HIGH);
digitalWrite(led2,LOW);
}
if (inches > x) {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
I'm having an issue getting them to change based on increasing or decreasing distances. I want it to read the distance and store that reading as a variable and then base the next reading on the previous variable. So if it is decreasing one led is low and when it is increasing the other led is low. Is this possible?
if (inches < x) {
digitalWrite(led,HIGH);
digitalWrite(led2,LOW);
}
if (inches > x) {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
inches = prevInches;
if (inches < prevInches) {
digitalWrite(led,HIGH);
digitalWrite(led2,LOW);
}
if (inches > prevInches) {
digitalWrite(led,LOW);
digitalWrite(led2,HIGH);
}
I realize there's something wrong with how I am storing the variable.