Hello;
I have downloaded the NewPing software into the Arduino Libraries on my computer.
The example listed for the 15 sensors code from Mr. Eckel was not able to be verified and is non-compilable.
Similar errors than my code here listed. Can anyone see what the problem is?
Thank you;
Pierre Salsiccia, e-Mail: pierresalsiccia@yahoo.com
#include <NewPing.h>
#include <stdint.h>
#define SONAR_NUM 3 // number of sensors
#define MAX_DISTANCE 280 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned long cm[SONAR_NUM]; // Where the ping distances are stored.
uint8_t currentSensor = 0; // Keeps track of which sensor is active.
NewPing sonar(trigger_pin, echo_pin, max_in_distance);
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(2, 3, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(4, 5, MAX_DISTANCE),
NewPing(6, 7, MAX_DISTANCE)};
void setup() {
Serial.begin(115200);}
pingTimer[0] = millis() + 75;
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL; }
unsigned long distance=0.0;
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 will 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 continues, interrupt will call echoCheck to look for echo).
}}
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 results.
// The following code would be replaced with your code that does something with the ping results.
distance = cm[0] /2.54; } // converts cm to inches
{const int motor1Pin = 9;
if (distance > 2 && distance <= 16)
{analogWrite(motor1Pin, 150);
delay (150);
analogWrite (motor1Pin, 0);}
else if (distance > 12 && distance <= 32)
{ analogWrite (motor1Pin, 143);
delay (150);
analogWrite (motor1Pin, 0);}
else if (distance > 32 && distance <= 52)
{ analogWrite (motor1Pin, 120);
delay (150);
analogWrite(motor1Pin, 0);}
else if (distance > 52 && distance <= 71)
{ analogWrite (motor1Pin, 100);
delay (150);
analogWrite (motor1Pin, 0);}
else if (distance > 71 && distance <= 91)
{ analogWrite (motor1Pin, 80);
delay (150);
analogWrite (motor1Pin, 0);}
else if (distance > 91 && distance <= 110)
{ analogWrite (motor1Pin, 60);
delay (150);
analogWrite (motor1Pin, 0);}
// Repeating the above process for sensor 2
distance = cm[1] /2.54
const int motor2Pin = 10;
if (distance > 2 && distance <= 16)
{ analogWrite (motor2Pin, 150);
delay (150);
analogWrite (motor2Pin, 0);}
else if (distance > 12 && distance <= 32)
{ analogWrite (motor2Pin, 143);
delay (150);
analogWrite (motor2Pin, 0);}
else if (distance > 32 && distance <= 52)
{ analogWrite (motor2Pin, 120);
delay (150);
analogWrite (motor2Pin, 0);}
else if (distance > 52 && distance <= 71)
{ analogWrite (motor2Pin, 100);
delay (150);
analogWrite (motor2Pin, 0);}
else if (distance > 71 && distance <= 91)
{ analogWrite (motor2Pin, 80);
delay (150);
analogWrite (motor2Pin, 0);}
else if (distance > 91 && distance <= 110)
{ analogWrite (motor2Pin, 60);
delay (150);
analogWrite (motor2Pin, 0);}
// Repeating the above process for sensor 3
distance = cm[2] /2.54;
const int motor3Pin = 11;
if (distance > 2 && distance <= 16)
{ analogWrite (motor3Pin, 150);
delay (500);
analogWrite (motor3Pin, 0);}
else if (distance > 12 && distance <= 32)
{ analogWrite (motor3Pin, 143);
delay (150);
analogWrite (motor3Pin, 0);}
else if (distance > 32 && distance <= 52)
{ analogWrite (motor3Pin, 123);
delay (150);
analogWrite (motor3Pin, 0);}
else if (distance > 52 && distance <= 71)
{ analogWrite (motor3Pin, 100);
delay (150);
analogWrite (motor3Pin, 0);}
else if (distance > 71 && distance <= 91)
{ analogWrite (motor3Pin, 80);
delay (150);
analogWrite (motor3Pin, 0);}
else if (distance > 91 && distance <= 110)
{ analogWrite (motor3Pin, 60);
delay (150);
analogWrite (motor3Pin, 0);}
}}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.