Hello,
My DFRobotShop Rover keeps automatically restarting followed by a malfunction in which it repeatedly goes in a circle. This occurs about 1-5 seconds after I turn it on.
The rover is supposed to run autonomously by means of the HC-SR04 ultrasonic sensor. As of now, this is the only additional module I have attached.
Pins for this are as follows:
Pin 10 - Trigger
Pin 11 - Echo
Common GND
Vcc to the boards 5VI ran the full speed test and it seems to work fine.
Has anyone else ran into this problem?
This is the code I have wrote for it:
#include <LiquidCrystal.h>
#include <Ultrasonic.h>
LiquidCrystal lcd(11, 10, 9, 4, 3, 2);
Ultrasonic ultrasonic(10,11); //Pin 10 is Trig. Pin 11 is Echo
const int M1Speed = 6; //M1 Speed Control
const int M2Speed = 5; //M2 Speed Control
const int M1Direction = 8; //M1 Direction Control
const int M2Direction = 7; //M2 Direction Control
int steeringMotorSpeed = 200;
int avoidDistance = 20;
int endAvoidDistance = 50;
int goToReverseDistance = 20;
int reverseTime = 1000;
int fullThrottleMinDistance = 50;
int lowSpeed = 0;
int fullThrottleSpeed = 255;
int cruiseSpeed = 200;
int avoidSpeed = 185;
int reverseSpeed = 175;
int sensorDelay = 200; //
int avoidDirection = 1;
int distance; //averaged distance
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
void setup() {
Serial.begin(9600);
pinMode(M1Speed, OUTPUT);
pinMode(M2Speed, OUTPUT);
pinMode(M1Direction, OUTPUT);
pinMode(M2Direction, OUTPUT);
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
distance = (ultrasonic.Ranging(CM));
Serial.println(distance);
if (distance <= goToReverseDistance){
reverseMode ();
}else if (distance > goToReverseDistance && distance <= avoidDistance){
avoidMode();
}else if (distance > avoidDistance && distance < fullThrottleMinDistance){
cruiseMode();
}else if (distance >= fullThrottleMinDistance){
fullThrottleMode();
}
delay(sensorDelay);
}
void reverseMode(){
avoidDirection = avoidDirection * -1;
digitalWrite(M1Direction, HIGH);
digitalWrite(M2Direction, LOW);
analogWrite(M2Speed, reverseSpeed);
analogWrite(M1Speed, reverseSpeed);
delay(sensorDelay);
}
void avoidMode(){
distance = (ultrasonic.Ranging(CM));
digitalWrite(M1Direction, LOW); //Turn one way
digitalWrite(M2Direction, LOW);
analogWrite(M1Speed, fullThrottleSpeed);
analogWrite(M2Speed, avoidSpeed);
delay(sensorDelay);
}
void cruiseMode(){
digitalWrite(M1Direction, LOW);
digitalWrite(M2Direction, LOW);
analogWrite(M1Speed, cruiseSpeed);
analogWrite(M2Speed, cruiseSpeed);
delay(sensorDelay);
}
void fullThrottleMode(){
digitalWrite(M1Direction, LOW);
digitalWrite(M2Direction, LOW);
analogWrite(M1Speed, fullThrottleSpeed);
analogWrite(M2Speed, fullThrottleSpeed);
delay(sensorDelay);
}
Note: Inside this code there is a little from the smoother code in the setup function