Hey guys :),
I'm trying to learn the ways of programming and electronics and I've come by a particularly difficult situation (for me anyway :P).
http://www.ebay.com.au/itm/161181376374?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649
I have hooked 3 of the above sensors to my arduino and lined them side by side. Now that poses a particularly problem. They each need to fire at different intervals so that they don't interfere with each other.
I figured out a way to easily (but not effeciently xD) do this with the delay command. But using delay is problematic as I'm using other sensors and it'd interfere with them.
The library I am using for the sensors is:
http://playground.arduino.cc/Code/NewPing#.Uyv9KIVYPuU
and my current code is:
#include <NewPing.h>
#define TRIGGER_PIN1 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN1 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PIN2 14 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN2 13 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PIN3 16 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN3 15 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters).
int convert;
unsigned int pingRx;
NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonar2(TRIGGER_PIN2, ECHO_PIN2, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
NewPing sonar3(TRIGGER_PIN3, ECHO_PIN3, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
double currentTime = millis();
double sonarTime = 0.0;
void Sonic() {
for (i=0,i<3,i++) {
if (currentTime>(sonarTime+300)) {
lcd.clear();
pingRx = sonar1.ping(); // Send ping, get ping time in microseconds (uS).
convert = pingRx / US_ROUNDTRIP_CM; // Convert ping time to distance in cm and print result (0 = outside set distance range)/
//Serial.print("Distance: ");
//Serial.print(convert); // Convert ping time to distance in cm and print result (0 = outside set distance range)
//Serial.println("cm");
lcd.print("Distance: ");
lcd.setCursor(0, 1);
lcd.print(convert);
lcd.print(" cm");
sonarTime = currentTime;
}
}
}
}
void setup() {
}
void loop() {
sonic()
(other stuff)
delay(100)
}
You might have noticed two problems with the above code:
- I have used a for loop but have not cycled the sensor for each increment of i. (i.e: every time i increments, sonar1 is pinged, you'll notice this in the line pingRx = sonar1.ping(). It should change to sonar2.ping() at i=1 and sonar3.ping() at i=2. Maybe this is possible to someone more advanced at coding than me?
- Even if problem 1 is fixed. The sensors won't fire sequentially thanks to the if statement, for example sonar1 would go off resetting the timer back to 0 (in a sense), i=1 kicks in and sonar2.ping() should occur next but 300ms haven't passed yet, so it skips and goes straight to i=2 instead of looping i=1 until it is successful. Again maybe the solution to this is more obvious to someone more adept than me =/.
- Or maybe I'm going about this the wrong way entirely and there's a much easier way altogether :P.
My attempts at finding a solution involved:
-
Trying to compare the variables:
if (SONAR == sonar1) {
SONAR = sonar2;
}
else if (SONAR == sonar2) {
SONAR = sonar3;
}
else if (SONAR == sonar3) {
SONAR = sonar1;
obviously didn't work since thats not a valid comparison xD... -
Try to put sonar1, sonar2, sonar3 into a matrix SONAR[sonar1, sonar2, sonar3] which is ALSO not valid since sonars are not primitive data types but are functions(NewPing) =/.
PLEASE help me guys this is doing my head in >.o.
Thanks :).