Hi, I'm a new user of arduino and this is my first project.
I'm trying to build a robot that avoid obstacles.
I have both the Arduino Motor Shield R3 and the HC-SR04 sensor and they work fine by them selves but when I connect the sensor to the shield it doesn't work probably, it detect up to 6-7cm only or some times more than 1000cm when the sensor itself can't detect more than 500cm (look at the attachment).
this is the code that I use to test it out:
#include "ArduinoMotorShieldR3.h"
#include <NewPing.h>
#define TRIGGER_PIN 7 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 4 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
ArduinoMotorShieldR3 md;
void setup()
{
md.init();
}
void loop()
{
delay(15);
unsigned int dist= sonar.ping_cm();
if(dist != NO_ECHO && dist < 30) {
md.setSpeeds(200,0);
} else {
md.setSpeeds(200,200);
}
}
also the motors doesn't work the same, one is faster than the other.
and when I connect the battery to the arduino board(4 1.5v AA), it doesn't work the same as when it's connected by USB.
Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.
Your code will help a lot.
Sorry you have.... :o
Have you checked if the two pins used by the SR04 is not used by the motor shield?
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?