I'm trying to compare distance sensor readings from a Parallax ultrasound Ping sensor. My goal is to see if an object (human or other) is coming closer or moving away from the sensor to control other functions based on that. I am completely lost. Currently I'm only trying to make a red LED light if objects are getting closer, and a blue LED light if objects are moving farther away.
I'm not entirely sure how to compare 1 input with itself over time. If I send the signal to an array, the compare the first with the last, every item in the array is the same number. Not sure what I'm doing wrong. Any help would be appreciated.
Thanks much for the help here, using a function was a great start for me and forced me to learn how to use functions! Thanks for that, I needed the kick.
I would have posted my unworkable code, but it was such a mess, it was practically unreadable. Anyway, here's the somewhat cleaned up working code if anyone is interested:
int ultraSoundSignal = 7; // Ultrasound signal pin
int val = 0;
int oldDistance;
int newDistance;
int timecount; // Echo counter
int ledPin = 13;
int red = 8;
int blue = 9;
int count = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
int oldDistance = 0;
pinMode(red,OUTPUT);
pinMode(blue,OUTPUT);
// oldDistance = read_the_sensor();
}
void loop() {
if(timecount > 0){
digitalWrite(ledPin, HIGH);
}
oldDistance = read_the_sensor();
delay(500);
newDistance = read_the_sensor();
//newDistance = timecount; // echo pulse time to newDistance
Serial.print("New Distance = "); // Example identifier for the sensor
Serial.print(newDistance);
Serial.write(10);
Serial.write(13);
//oldDistance = timecount; // echo pulse time to newDistance
Serial.print("Old Distance = "); // Example identifier for the sensor
Serial.print(oldDistance);
Serial.write(10);
Serial.write(13);
/* Lite up LED if any value is passed by the echo pulse
* -------------------------------------------------------------------
*/
//do our logic test
if(oldDistance > newDistance)
{
digitalWrite(red,HIGH);
digitalWrite(blue,LOW);
}
else
if(oldDistance < newDistance)
{
digitalWrite(red,LOW);
digitalWrite(blue,HIGH);
}
}
int read_the_sensor() {
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
/* Send low-high-low pulse to activate the trigger pulse of the sensor
* -------------------------------------------------------------------
*/
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
/* Listening for echo pulse
* -------------------------------------------------------------------
*/
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}
while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}
return timecount;
}
Moderator edit: </mark> <mark>[code]</mark> <mark>