I have prepared a sketch where the first multiple sensors code was copied from forum individual response
and added my code to run my motors but the help feedback was shortsighted as no enough information was provided as what the code functions (unknown to me) do. Complicated use of pingTimer as to set next time sensors will be pinged.
code does not make sense and is not even correct as lots of terms need be declared per my compiler feedback.
See code below. I spent an entire day to try to figure this out and I gave up.
Any info as to help to correct compiling errors would be very much appreciated.
#include <NewPing.h>
#define MAX_DISTANCE 280 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 200 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).
uint8_t currentSensor = 0; // Keeps track of which sensor is active.
int CM;
unsigned long inches =0;
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);
int SONAR_NUM = 3;
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.
unsigned long inches =0;
pingTimer[0] = millis() + 75; // First ping starts at 75ms, gives time for the Arduino to chill before starting.
for (uint8_t i = 1; i < SONAR_NUM; i++) // Set the starting time for each sensor.
pingTimer = pingTimer[i - 1] + PING_INTERVAL;
}
void loop() {
- int currentSensor;*
- int sonar[3] 0,0,0;*
- for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.*
_ if (millis() >= pingTimer*) { // Is it this sensor's time to ping?_
pingTimer += 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.
int CM[3];_
int US_ROUNDTRIP_CM;*
* 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._
* int CM;*
* inches = cm[0]/2.54;*
* const int motor1Pin = 9;*
* if (inches > 2 && inches <= 16)*
* {analogWrite(motor1Pin, 150);*
* delay (150);*
* analogWrite (motor1Pin, 0);}*
* else if (inches > 12 && inches <= 32)*
* { analogWrite (motor1Pin, 143);*
* delay (150);*
* analogWrite (motor1Pin, 0);}*
* else if (inches > 32 && inches <= 52)*
* { analogWrite (motor1Pin, 120);*
* delay (150);*
* analogWrite(motor1Pin, 0);}*
* else if (inches > 52 && inches <= 71)*
* { analogWrite (motor1Pin, 100);*
* delay (150);*
* analogWrite (motor1Pin, 0);}*
* else if (inches > 71 && inches <= 91)*
* { analogWrite (motor1Pin, 80);*
* delay (150);*
* analogWrite (motor1Pin, 0);}*
* else if (inches > 91 && inches <= 110)*
* { analogWrite (motor1Pin, 60);*
* delay (150);*
* analogWrite (motor1Pin, 0);}*
* else if (inches > 110 && inches <= 130)*
* { analogWrite (motor1Pin, 40);*
* delay (150);*
* analogWrite (motor1Pin, 0);}*