Question

I am using Arduino Uno to build a very simple robot which moves forward and when it senses any obejct (using distance sensor HC-SR04), it stops moving. The robot base is 4 wheeled and DC motor attached with each wheel. I am using pins 1, 2, 3, 4 for controlling motors 1, 2, 3, 4 respectively. Pins 7, 8 are used for Echo and Trig pins of distance sensor HC-SR04.

For this I have written following code

#define m1p1 1
#define m1p2 2
#define m2p1 3
#define m2p2 4

#define echoPin 7 // Echo Pin
#define trigPin 8 // Trigger Pin
#define LEDPin 13 // Onboard LED

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

void setup()
{
  Serial.begin(9600);
  pinMode(m1p1, OUTPUT);
  pinMode(m1p2, OUTPUT);
  pinMode(m2p1, OUTPUT);
  pinMode(m2p2, OUTPUT);

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(LEDPin, OUTPUT); // Use LED indicator (if required)

}

void loop()
{
 //Code to run motors forward
 digitalWrite(m1p1, LOW);
 digitalWrite(m1p2, HIGH);
 digitalWrite(m2p1, HIGH);
 digitalWrite(m2p2, LOW);

 //Code of distance sensor
 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");

  //Code to stop motors
  digitalWrite(m1p1, LOW);
  digitalWrite(m1p2, LOW);
  digitalWrite(m2p1, LOW);
  digitalWrite(m2p2, LOW);
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
   turn LED OFF to indicate successful reading. 
 */
 Serial.println(distance);
 digitalWrite(LEDPin, LOW); 
}

//Delay 50ms before next reading.
delay(50);

}

The problem is, I think, with the line which contains Serial.begin(9600), because when I remove this line then the all motors are running correctly and robot runs in forward direction, but when I write this line, then only 2 motors are running smoothly and 2 motors are running very slowly (almost it seems as they are not running) so instead of moving forward the robot continuously turns.

Can anyone tell what can be the problem and how to solve that.

Was it helpful?

Solution

The serial port uses pins 0 and 1 as stated in the first paragraph of the serial docs:

http://arduino.cc/en/reference/serial

So for your motors you should pick some other pins.

OTHER TIPS

From http://arduino.cc/en/Main/arduinoBoardUno:

In addition, some pins have specialized functions:

Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data. These pins are connected to the corresponding pins of the ATmega8U2 USB-to-TTL Serial chip.

Serial uses pins 0 and 1. Therefore, you can't use them to another purpose at same time. I suggest to you use other pins to drive your motors (e.g. 2 to 5).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top