Hi
I'm having a different problem using Ultrasonic Sensor SRF05 and the Arduno Motor Shield. Both are working fine when used separately, but once their are both supposed to work together the Motorshield stay quiet (not light and no motor sound) although the Arduino sketch is still running.
I checked for the Pins => the Ultrasonic Module is wired to A4 and A5 finally (for now at least), so that here is no conflict with MotorShield
I checked for the power => MotorShield is plugged on batteries NI-MH 800mA 14.4V, Arduino is plugged on USB cable and the Vin pin of the MotorShield is cut (bended to be not connected for now), it seems to be no power drop off as the sketch still runs
What is funny is that when Echo is not connected, things work but once I connect it the motorShield stops.
Similarly, when Trigger is not connected, things work for 1 second then stop.
The code still works fine, I check it on monitor
Any idea, thanks.
PS: Here is the whole code in case I miss something
#include <NewPing.h> // bibliothèque de gestion des delays
const int VMax=160;
volatile int distance=0;
#define PinEcho A4 // broche Echo sur pin Digital 7
#define PinTrigger A5 // broche Trigger sur pin Digital 6
#define distanceMax 100 // définition de la distance maximale de captation en centimètres.
NewPing sonar(PinTrigger, PinEcho, distanceMax); // initialisation de la fonction de sonar de notre bibliothèque
int pingLap = 50; // temps entre chaque pulsation en millisecondes.
long pingTimer; // minuterie entre chaque pulsation.
boolean tourne=false;
long randNumber;
void setup() {
Serial.begin(115200);
Serial.flush();
pinMode(3,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pingTimer = millis(); // initialisation de la minuterie.
}
void loop() {
if (true) { // to skip the sensor or to activate it
if (millis() >= pingTimer)
{
Serial.print("Ping ");
pingTimer += pingLap;
sonar.ping_timer(echoSonar); // fonction de vérification des conditions.
Serial.println(distance);
if (distance<20) {
if (!(tourne)) {
Serial.println("dist < 20");
randNumber = random(0,2);
tourne=true;
}
else {
Serial.println("Tourne");
deplace((randNumber*2-1)*90,50);
}
}
else {
Serial.println("dist > 20");
deplace(0,100);
tourne=false;
}
}
}
else {
deplace(0,100);
Serial.println("Tout droit");
}
}
void deplace(int angle,int vitesse) {
// angle en degrée entre -180 et +180
// vitesse en %
int droite;
int gauche;
// verification que l'angle est bien compris entre -180 et +180
while (angle < -180)
angle = angle + 360;
while (angle > 180)
angle = angle - 360;
// les configurations moteur focntion de l'angle
if ((angle >= 0) & (angle <= 90)) {
droite = VMax;
gauche = map(angle,0,90,VMax,-VMax);
}
else {
if ((angle >= 90) & (angle <= 180)) {
gauche = -VMax;
droite = map(angle,90,180,VMax,-VMax);
}
else {
if ((angle >= -180) & (angle <= -90)) {
droite = -VMax;
gauche = map(angle,-180,-90,-VMax,VMax);
}
else {
if ((angle >= -90) & (angle <= 0)) {
gauche = VMax;
droite = map(angle,-90,0,-VMax,VMax);
}
}
}
}
droite = droite*vitesse/100;
gauche = gauche*vitesse/100;
if (droite>0)
digitalWrite(12,HIGH);
else
digitalWrite(12,LOW);
if (gauche>0)
digitalWrite(13,HIGH);
else
digitalWrite(13,LOW);
analogWrite(3,abs(droite));
analogWrite(11,abs(gauche));
Serial.print(angle);
Serial.print(" + ");
Serial.print(vitesse);
Serial.print(" = ");
Serial.print(droite);
Serial.print(" + ");
Serial.println(gauche);
}
void echoSonar() {
distance=distanceMax;
if (sonar.check_timer()) // fonction de vérification de la minuterie.
{
distance=sonar.convert_cm(sonar.ping_result);
}
}