Simple Autonomous Mobile Robot

Hello I am building a project here with a mobile robot. I have attached an image to see how it looks like. Um my problem is that when I get the values from my distance sensor I have an integer, after that 4 zeros, then an integer similar to the first etc…I have no idea why some zeros keeps appearing. Notice that when I try to use only my sonar its working perfectly. The part of code that moves forward or backward the mobile is doing somehting…but i dont know what. Anyone with some ideas of what is happening? How can i make this better etc… :slight_smile:


Code:

/*
* ArduMobile - An Autonomous Nagivating Mobile
*
* Developed by Vasilis Tzivaras
* contact me at vtzivaras(at)gmail.com
*
* Arduino UNO R3
* HC-SR04 Ultrasonic Sensor
* Motor Shield v2
*/

#define LMDirection 12                  // Hight for forward and LOW for backward
#define RMDirection 13

#define LMSpeed 3
#define RMSpeed 11

#define LMBrake 9
#define RMBrake 8

#define frontSensor 10

#define echoPin 4
#define trigPin 5

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration=0, distance=0; // Duration used to calculate distance

void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 
 pinMode(LMDirection, OUTPUT);
 pinMode(RMDirection, OUTPUT);

 pinMode(LMBrake, OUTPUT);
 pinMode(RMBrake, OUTPUT);
}

void getDistance() {
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 

 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);

 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;

 if (distance >= maximumRange || distance <= minimumRange){
 //Serial.println("-1");
                
 }else {
 //Serial.println(distance);
               
 }
}

void forward(int sp) {
 //Left motor forward @ speed
 digitalWrite(LMDirection, HIGH);        //Establishes forward direction of motor1
 digitalWrite(LMBrake, LOW);   //Disengage the Brake for Channel A
 analogWrite(LMSpeed, sp);            //Spins the motor on Channel A at half speed

 //Right motor forward @ speed
 digitalWrite(RMDirection, HIGH);        //Establishes forward direction of motor2
 digitalWrite(RMBrake, LOW);   //Disengage the Brake for Channel B
 analogWrite(RMSpeed, sp);   //Spins the motor on Channel B at full speed
}

void backward(int sp) {
 //Motor ! backward @ half speed
 digitalWrite(LMDirection, LOW);         //Establishes backward direction of Channel B
 digitalWrite(LMBrake, LOW);             //Disengage the Brake for Channel B
 analogWrite(LMSpeed, sp);            //Spins the motor on Channel B at half speed
 
 //Motor B backward @ half speed
 digitalWrite(RMDirection, LOW);         //Establishes backward direction of Channel B
 digitalWrite(RMBrake, LOW);             //Disengage the Brake for Channel B
 analogWrite(RMSpeed, sp);            //Spins the motor on Channel B at half speed
}

void turn() {
 //Motor A forward @ full speed
 digitalWrite(12, LOW);  //Establishes backward direction of Channel 1 A
 digitalWrite(9, LOW);   //Disengage the Brake for Channel A
 analogWrite(3, 123);    //Spins the motor on Channel A at half speed

 //Motor B backward @ half speed
 digitalWrite(13, LOW);  //Establishes backward direction of Channel B
 digitalWrite(8, LOW);   //Disengage the Brake for Channel B
 analogWrite(11, 123);    //Spins the motor on Channel B at half speed
}

void printStatus() {
 Serial.print("Distance: ");
        Serial.print(distance);
        Serial.print("\tDirection");
        Serial.print("-");
        Serial.println();
}

void loop() {
        printStatus();
        getDistance();
     
 if(distance < 15) {
                Serial.println("Moving backward...");
                backward(255);
 }else {
                Serial.println("Moving forward...");
                forward(255);
        }
}

thank you !

If the sonar works fine by itself, consider electrical interference from other components on the robot like motors etc...

shall I use another VCC and GND then? :confused: this should work?

In general if you haven't taken precautions to reduce interference from motors and
power electronics, or shield all your sensor cables, you will see issues. Never power sensors
and motors from the same source, that is always a big nest of problems.

yeah, different power source, capacitors, diodes, etc... The easiest thing to try is add diodes on the motors and not use the VCC on the arduino to see if that works.

Um I have no diodes only 220 ohm resistors...so I guess I have to buy some... :confused: ok

thanks guys, I needed to use a different gnd for the sonar and it is working fine. thank you.