Hi! I’m trying to build a 4wd obstacle avoiding robot using a sr04 ultrasonic sensor, 4 dc geared motors and a Adafruit motor shield(All being cheap Chinese copies). The motors have been noise suppressed by three .1uf capacitors for each motor. When I use Newping library, the sr04 reads fine, with following code:
#include <NewPing.h>
#include <AFMotor.h>
#define TRIGGER_PIN 4 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 300 //
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
void setup() {
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println(“cm”);
}
but if i include
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
AF_DCMotor motor3(3);
AF_DCMotor motor4(4);
statements to the above code and not even run the motors, the sr04 sensor reads only 0’s(probably erroneosly).It’s all the same if I use pin d12 and d11 as trig and echo repectively. I’ve tested motors separately and they work fine. Please help.