0% found this document useful (0 votes)
77 views2 pages

New Ping

This code defines an array of 15 ultrasonic sensor objects that are used to ping distances to objects and store the results. It initializes the sensor objects with trigger and echo pins. It sets up a ping timer for each sensor that sequences the sensors to ping one after another. In the main loop, it checks each sensor's timer, pings that sensor to get the distance, stores it in the distance array, and updates the timer for the next ping. After pinging all sensors, it prints the measured distances to the serial monitor.

Uploaded by

hulio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views2 pages

New Ping

This code defines an array of 15 ultrasonic sensor objects that are used to ping distances to objects and store the results. It initializes the sensor objects with trigger and echo pins. It sets up a ping timer for each sensor that sequences the sensors to ping one after another. In the main loop, it checks each sensor's timer, pings that sensor to get the distance, stores it in the distance array, and updates the timer for the next ping. After pinging all sensors, it prints the measured distances to the serial monitor.

Uploaded by

hulio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

// new ping sensor

#include <NewPing.h>
#define
#define
#define
min to

SONAR_NUM
15 // Number or sensors.
MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the
avoid cross-sensor echo).

unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should
happen for each sensor.
unsigned int cm[SONAR_NUM];
// Where the ping distances are stored.
uint8_t currentSensor = 0;
// Keeps track of which sensor is active.
NewPing sonar[SONAR_NUM] = {
// Sensor object array.
NewPing(41, 42, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max
distance to ping.
NewPing(43, 44, MAX_DISTANCE),
NewPing(45, 20, MAX_DISTANCE),
NewPing(21, 22, MAX_DISTANCE),
NewPing(23, 24, MAX_DISTANCE),
NewPing(25, 26, MAX_DISTANCE),
NewPing(27, 28, MAX_DISTANCE),
NewPing(29, 30, MAX_DISTANCE),
NewPing(31, 32, MAX_DISTANCE),
NewPing(34, 33, MAX_DISTANCE),
NewPing(35, 36, MAX_DISTANCE),
NewPing(37, 38, MAX_DISTANCE),
NewPing(39, 40, MAX_DISTANCE),
NewPing(50, 51, MAX_DISTANCE),
NewPing(52, 53, MAX_DISTANCE)
};
void setup() {
Serial.begin(115200);
pingTimer[0] = millis() + 75;
// First ping starts at 75ms, gives ti
me for the Arduino to chill before starting.
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sens
or.
pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
}
void loop() {
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
if (millis() >= pingTimer[i]) {
// Is it this sensor's time to ping?
pingTimer[i] += PING_INTERVAL * SONAR_NUM; // Set next time this sensor w
ill be pinged.
if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor
ping cycle complete, do something with the results.
sonar[currentSensor].timer_stop();
// Make sure previous timer is
canceled before starting a new ping (insurance).
currentSensor = i;
// Sensor being accessed.
cm[currentSensor] = 0;
// Make distance zero in case
there's no ping echo for this sensor.
sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing con
tinues, interrupt will call echoCheck to look for echo).
}
}
// The rest of your code would go here.
}

void echoCheck() { // If ping received, set the sensor distance to array.


if (sonar[currentSensor].check_timer())
cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
}
void oneSensorCycle() { // Sensor ping cycle complete, do something with the res
ults.
for (uint8_t i = 0; i < SONAR_NUM; i++) {
Serial.print(i);
Serial.print("=");
Serial.print(cm[i]);
Serial.print("cm ");
}
Serial.println();
}

You might also like