Ultasonic sensors
hi there need advice, found code , use sensors, want know if work sensors start @ same time dont understand code well, if please tell me first of if work , second need change in code?
thank
#include <newping.h>
#define sonar_num 3 // number of sensors.
#define max_distance 200 // maximum distance (in cm) ping.
#define ping_interval 0 // milliseconds between sensor pings (29ms min avoid cross-sensor echo).
unsigned long pingtimer[sonar_num]; // holds times when next ping should happen each sensor.
unsigned int cm[sonar_num]; // ping distances stored.
uint8_t currentsensor = 0; // keeps track of sensor active.
newping sonar[sonar_num] = { // sensor object array.
newping(6, 7, max_distance), // each sensor's trigger pin, echo pin, , max distance ping.
newping(8, 9, max_distance),
newping(11, 12, max_distance)
};
void setup() {
serial.begin(115200);
pingtimer[0] = millis() + 75; // first ping starts @ 75ms, gives time arduino chill before starting.
(uint8_t = 1; < sonar_num; i++) // set starting time each sensor - can change make them start @ same time?
?
pingtimer = pingtimer[i - 1] + ping_interval;
}
void loop() {
(uint8_t = 0; < sonar_num; i++) { // loop through sensors.
if (millis() >= pingtimer) { // sensor's time ping?
pingtimer += ping_interval * sonar_num; // set next time sensor pinged.
if (i == 0 && currentsensor == sonar_num - 1) onesensorcycle(); // sensor ping cycle complete, results.
sonar[currentsensor].timer_stop(); // make sure previous timer canceled before starting new ping (insurance).
currentsensor = i; // sensor being accessed.
cm[currentsensor] = 0; // make distance 0 in case there's no ping echo sensor.
sonar[currentsensor].ping_timer(echocheck); // ping (processing continues, interrupt call echocheck echo).
}
}
// other code *doesn't* analyze ping results can go here.
}
void echocheck() { // if ping received, set sensor distance array.
if (sonar[currentsensor].check_timer())
cm[currentsensor] = sonar[currentsensor].ping_result / us_roundtrip_cm;
}
void onesensorcycle() { // sensor ping cycle complete, results.
// following code replaced code ping results.
(uint8_t = 0; < sonar_num; i++) {
serial.print(i);
serial.print("=");
serial.print(cm);
serial.print("cm ");
}
serial.println();
}
thank
#include <newping.h>
#define sonar_num 3 // number of sensors.
#define max_distance 200 // maximum distance (in cm) ping.
#define ping_interval 0 // milliseconds between sensor pings (29ms min avoid cross-sensor echo).
unsigned long pingtimer[sonar_num]; // holds times when next ping should happen each sensor.
unsigned int cm[sonar_num]; // ping distances stored.
uint8_t currentsensor = 0; // keeps track of sensor active.
newping sonar[sonar_num] = { // sensor object array.
newping(6, 7, max_distance), // each sensor's trigger pin, echo pin, , max distance ping.
newping(8, 9, max_distance),
newping(11, 12, max_distance)
};
void setup() {
serial.begin(115200);
pingtimer[0] = millis() + 75; // first ping starts @ 75ms, gives time arduino chill before starting.
(uint8_t = 1; < sonar_num; i++) // set starting time each sensor - can change make them start @ same time?

pingtimer = pingtimer[i - 1] + ping_interval;
}
void loop() {
(uint8_t = 0; < sonar_num; i++) { // loop through sensors.
if (millis() >= pingtimer) { // sensor's time ping?
pingtimer += ping_interval * sonar_num; // set next time sensor pinged.
if (i == 0 && currentsensor == sonar_num - 1) onesensorcycle(); // sensor ping cycle complete, results.
sonar[currentsensor].timer_stop(); // make sure previous timer canceled before starting new ping (insurance).
currentsensor = i; // sensor being accessed.
cm[currentsensor] = 0; // make distance 0 in case there's no ping echo sensor.
sonar[currentsensor].ping_timer(echocheck); // ping (processing continues, interrupt call echocheck echo).
}
}
// other code *doesn't* analyze ping results can go here.
}
void echocheck() { // if ping received, set sensor distance array.
if (sonar[currentsensor].check_timer())
cm[currentsensor] = sonar[currentsensor].ping_result / us_roundtrip_cm;
}
void onesensorcycle() { // sensor ping cycle complete, results.
// following code replaced code ping results.
(uint8_t = 0; < sonar_num; i++) {
serial.print(i);
serial.print("=");
serial.print(cm);
serial.print("cm ");
}
serial.println();
}
now can see why ask use code tags when posting code?
Arduino Forum > Using Arduino > Project Guidance > Ultasonic sensors
arduino
Comments
Post a Comment