Hello!
Any light on this subject would be very much appreciated.
The code below works as intended. Vibration motors 1, 2 and 3 are turned on from sound feedback at sensors
1, 2 and 3 respectively. If else statements allow motors to turn on at discrete distance intervals and decreasing in speed
as the detected object is further away. But, each feed back and motor run occurs each 3 seconds.
I cannot figure out why the long 3 seconds delay between each motor run.
Please help.
#include <SoftwareSerial.h> // to read data from range finders PINS 3, 5 and 7
#include
void setup()
{
Serial.begin(9600);
}
void loop()
{
const int pingsensor1Pin = 2;
const int pingsensor2Pin = 4;
const int pingsensor3Pin = 6;
const int echosensor1Pin = 3;
const int echosensor2Pin = 5;
const int echosensor3Pin = 7;
int inches;
long duration1, duration2, duration3, distance; // Duration used to calculate distance
const int motor1Pin = 9;
const int motor2Pin = 10;
const int motor3Pin = 11;
/* This Code uses 3 sensors fired nearly simultaneously and powers respective vibration motors 1, 2 and 3
When reflected back sound is detected and motor speed is inversely proportional to obstacle distance.
Sensor 1 has pin 2 for trigger and pin 3 for Signal. Sensor 2 has pin 4 for trigger and pin 5 for Signal.
Sensor 3 has pin 6 for trigger and pin 7 for Signal.
Output motor 1 on pin 9. Output motor 2 on pin 10. Output motor 3 on pin 11. */
pinMode(pingsensor1Pin, OUTPUT); // preparing sensor1 pin 2 as output for pulses
digitalWrite(pingsensor1Pin, LOW);
delayMicroseconds(2);
digitalWrite(pingsensor1Pin, HIGH); //fire the 8 sound pulses
delayMicroseconds(10);
digitalWrite(pingsensor1Pin, LOW);
pinMode(pingsensor2Pin, OUTPUT); // preparing sensor2 pin 4 as output for pulses
digitalWrite(pingsensor2Pin, LOW);
delayMicroseconds(2);
digitalWrite(pingsensor2Pin, HIGH); //fire the 8 sound pulses
delayMicroseconds(10);
digitalWrite(pingsensor2Pin, LOW);
pinMode(pingsensor3Pin, OUTPUT); // preparing sensor3 pin 6 as output for pulses
digitalWrite(pingsensor3Pin, LOW);
delayMicroseconds(2);
digitalWrite(pingsensor3Pin, HIGH); //fire the 8 sound pulses
delayMicroseconds(10);
digitalWrite(pingsensor3Pin, LOW);
pinMode(echosensor1Pin, INPUT); // listen for sound wave reflected back on pin 3
duration1 = pulseIn(echosensor1Pin, HIGH);
pinMode(echosensor2Pin, INPUT); //listen for sound wave reflected back on pin 5
duration2 = pulseIn(echosensor2Pin, HIGH);
pinMode(echosensor3Pin, INPUT); //listen for sound wave reflected back on pin 7
duration3 = pulseIn(echosensor3Pin, HIGH);
inches = (duration1/2) / 74; // convert time_echo from sensor 1 to object distance in inches
if (inches > 2 && inches <= 16)
{analogWrite(motor1Pin, 150);
delay (500);
analogWrite (motor1Pin, 0);}
else if (inches > 16 && inches <= 36)
{ analogWrite (motor1Pin, 143);
delay (150);
analogWrite (motor1Pin, 0);}
else if (inches > 36 && inches <= 56)
{ analogWrite (motor1Pin, 120);
delay (150);
analogWrite(motor1Pin, 0);}
else if (inches > 56 && inches <= 76)
{ analogWrite (motor1Pin, 100);
delay (150);
analogWrite (motor1Pin, 0);}
else if (inches > 76 && inches <= 96)
{ analogWrite (motor1Pin, 80);
delay (150);
analogWrite (motor1Pin, 0);}
else if (inches > 96 && inches <= 116)
{ analogWrite (motor1Pin, 60);
delay (150);
analogWrite (motor1Pin, 0);}
inches = (duration2/2) / 74; // convert time_echo from sensor 2 to object distance in inches
if (inches > 2 && inches <= 16)
{ analogWrite (motor2Pin, 150);
delay (500);
analogWrite (motor2Pin, 0);}
else if (inches > 16 && inches <= 36)
{ analogWrite (motor2Pin, 143);
delay (150);
analogWrite (motor2Pin, 0);}
else if (inches > 36 && inches <= 56)
{ analogWrite (motor2Pin, 120);
delay (150);
analogWrite (motor2Pin, 0);}
else if (inches > 56 && inches <= 76)
{ analogWrite (motor2Pin, 100);
delay (150);
analogWrite (motor2Pin, 0);}
else if (inches > 76 && inches <= 96)
{ analogWrite (motor2Pin, 80);
delay (150);
analogWrite (motor2Pin, 0);}
else if (inches > 96 && inches <= 116)
{ analogWrite (motor2Pin, 60);
delay (150);
analogWrite (motor2Pin, 0);}
inches = (duration3/2) / 74; // convert time_echo to object distance in inches
if (inches > 2 && inches <= 16)
{ analogWrite (motor3Pin, 150);
delay (500);
analogWrite (motor3Pin, 0);}
else if (inches > 16 && inches <= 36)
{ analogWrite (motor3Pin, 143);
delay (150);
analogWrite (motor3Pin, 0);}
else if (inches > 36 && inches <= 56)
{ analogWrite (motor3Pin, 123);
delay (150);
analogWrite (motor3Pin, 0);}
else if (inches > 56 && inches <= 76)
{ analogWrite (motor3Pin, 100);
delay (150);
analogWrite (motor3Pin, 0);}
else if (inches > 76 && inches <= 96)
{ analogWrite (motor3Pin, 80);
delay (150);
analogWrite (motor3Pin, 0);}
else if (inches > 96 && inches <= 116)
{ analogWrite (motor3Pin, 60);
delay (150);
analogWrite (motor3Pin, 0);}
}