I want to make a code that makes motors vibrating by two sonar sensors. The sensor will be located in the right and left side. Each side can read the distance they are toward. 1 Motor vibrates in 300cm, 2 motors vibrate in 200cm , 4 motors vibrate in 100 cm.
here are my codes
#include <NewPing.h>
#define SONAR_NUM 2 // Number of sensors.
#define MAX_DISTANCE 400 // 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 int cm[SONAR_NUM]; // Where the ping distances are stored.
uint8_t currentSensor = 0; // Keeps track of which sensor is active.
int motor_1m1 = 10;
int motor_2m1 = 9;
int motor_3m1 = 8;
int motor_4m1 = 7;
int motor_1m2 = 14;
int motor_2m2 = 13;
int motor_3m2 = 12;
int motor_4m2 = 11;
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),
};
void setup() {
Serial.begin(115200);
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[i] = pingTimer[i - 1] + PING_INTERVAL;
pinMode(motor_1m1, OUTPUT);
pinMode(motor_2m1, OUTPUT);
pinMode(motor_3m1, OUTPUT);
pinMode(motor_4m1, OUTPUT);
pinMode(motor_1m2, OUTPUT);
pinMode(motor_2m2, OUTPUT);
pinMode(motor_3m2, OUTPUT);
pinMode(motor_4m2, OUTPUT);
}
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).
}
if(cm[SONAR_NUM] >= 300){
digitalWrite(motor_1m1, LOW);
digitalWrite(motor_2m1, LOW);
digitalWrite(motor_3m1, LOW);
digitalWrite(motor_4m1, LOW);
digitalWrite(motor_1m2, LOW);
digitalWrite(motor_2m2, LOW);
digitalWrite(motor_3m2, LOW);
digitalWrite(motor_4m2, LOW);
}
else if(cm[SONAR_NUM] >=200 && cm[SONAR_NUM] < 300){
digitalWrite(motor_1m1, LOW);
digitalWrite(motor_2m1, LOW);
digitalWrite(motor_3m1, LOW);
digitalWrite(motor_4m1, HIGH);
digitalWrite(motor_1m2, LOW);
digitalWrite(motor_2m2, LOW);
digitalWrite(motor_3m2, LOW);
digitalWrite(motor_4m2, HIGH);
}
else if(cm[SONAR_NUM] >=100 && cm[SONAR_NUM] < 200){
digitalWrite(motor_1m1, LOW);
digitalWrite(motor_2m1, LOW);
digitalWrite(motor_3m1, HIGH);
digitalWrite(motor_4m1, HIGH);
digitalWrite(motor_1m2, LOW);
digitalWrite(motor_2m2, LOW);
digitalWrite(motor_3m2, HIGH);
digitalWrite(motor_4m2, HIGH);
}
else if(cm[SONAR_NUM] >=0 && cm[SONAR_NUM] < 100){
digitalWrite(motor_1m1, HIGH);
digitalWrite(motor_2m1, HIGH);
digitalWrite(motor_3m1, HIGH);
digitalWrite(motor_4m1, HIGH);
digitalWrite(motor_1m2, HIGH);
digitalWrite(motor_2m2, HIGH);
digitalWrite(motor_3m2, HIGH);
digitalWrite(motor_4m2, HIGH);
}
}
// Other code that *DOESN'T* analyze ping results can 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 results.
// The following code would be replaced with your code that does something with the ping results.
for (uint8_t i = 0; i < SONAR_NUM; i++) {
Serial.print(i);
Serial.print("=");
Serial.print(cm[i]);
Serial.print("cm ");
}
Serial.println();
}
It can read the distances by 2sensors but motors doesn't work.
plz help me.