Hi!
This is my first Arduino project and I'm having problems. I want to make a robot use an ultrasonic sensor to tell it to back away when it gets within 10 centimeters of something. I'm using an Arduino Uno R3, a Makeblock Me ultrasonic sensor, an OSEPP MTD-01 motor driver, and 2 DC motors. My code is a combination of the ultrasonic sensor example in the IDE and a code for what I want to do from a book. I modified it for the motor driver but I think I'm trying to use the motor driver wrong, since I couldn't find any instructions online. The robot runs fine until right after the LED is on for 5 seconds, then I start having problems. First, both motors are running backwards though they were fine during setup. Then, if I put my hand in front of the sensor, the left motor starts going forwards while the right one just stops. When I move my hand both motors start going backwards again. Here's my code:
const int pwm1 = 11;//left motor
const int in1 = 10;//left backward
const int in2 = 9;//left forward
const int pwm2 = 6;//right motor
const int in3 = 5;// right backward
const int in4 = 4;// right forward
const int led = 13;
const int pingPin = 7;
void setup() {
pinMode (pwm1, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(pwm2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(led, OUTPUT);
digitalWrite(in2, HIGH);
digitalWrite(pwm1, 100);
digitalWrite(in4, HIGH);
digitalWrite(pwm2, 100);
delay(1000);
digitalWrite(in2, LOW);
digitalWrite(pwm1, LOW);
digitalWrite(in4, LOW);
digitalWrite(pwm2, LOW);
delay(1000);
digitalWrite(in1, HIGH);
digitalWrite(pwm1, 100);
digitalWrite(in3, HIGH);
digitalWrite(pwm2, 100);
delay(1000);
digitalWrite(in1, LOW);
digitalWrite(pwm1, LOW);
digitalWrite(in3, LOW);
digitalWrite(pwm2, LOW);
digitalWrite(led, HIGH);//LED on for 5 seconds
delay(5000);
digitalWrite(led, LOW);
Serial.begin(9600);// stops doing what i want it to do here
}
void loop(){
digitalWrite(in2, HIGH);
digitalWrite(pwm1, 100);
digitalWrite(in4, HIGH);
digitalWrite(pwm2, 100);
uint8_t i;
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin,HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
if (cm >10){
digitalWrite(in2, LOW);//left motor, backwards
digitalWrite(pwm1, LOW);
digitalWrite(in1, HIGH);
digitalWrite(pwm1, 100);
//for (i=0; i<255; i++){
// delay(10);}
digitalWrite(in4, LOW);// right motor, backwards
digitalWrite(pwm2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(pwm2, 100);
for (i=0; i<255; i++){
delay(10);}
digitalWrite(in3, LOW);//start both motors forward again
digitalWrite(in4, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in1, HIGH);
delay(30);
}
else{
digitalWrite(in2, HIGH);
digitalWrite(pwm1, 100);
digitalWrite(in4, HIGH);
digitalWrite(pwm2, 100);
}
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29/ 2;}
And here's the one I modified it from:
#include <AFMotor.h>
AF_DCMotor motorleft(1);
AF.DCMotor motorright(4);
const int pingPin = 7;
void setup() {
Serial.begin(9600);
motorleft.setSpeed(50);
motorright.setSpeed(50);
}
void loop() {
uint8_t i;
long duration, inches, cm;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin,LOW);
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
if (cm>10){
motorleft.run(BACKWARD);
for (i=0; i<255; i++){
motorleft.setSpeed(i);
delay(10);}
motorright.run(BACKWARD);
for (i=0, i<255; i++){
motorright.setSpeed(i);
delay(10);
}
motorleft.run(RELEASE);
motorright.run(RELEASE);
delay(30);
}
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
I have checked my wiring. Since I'm a noob and don't really know what every single part of the code is doing, I'm probably using it wrong and making some noob mistake, though I have no idea what. Thanks in advance for any help!
-p.