hi everyone.
I’m fresh to all of this and this is my first project. I’m a noob at everything from programming to circuits so its been a steep learning curve but i’m getting there… I think.
I’ve just put together my obstacle avoiding robot. bit of a DIY job but it works (mechanically).
I’ve spent the day writing some code for it. i basically put the hole code together from scratch from what I’d learned today and from examples online. I uploaded the code and it verified ok but my robot seems to be doing some random and not so smooth movements.
first , one motor seems to be getting more power than the other. and even though i think i wrote the code for both wheels to go forward it seem that they are spinning in different directions. (which could mean its not resetting from the obstacle detected actions). my guess is ive missed a line of crucial code somehwere.
can i get some tips on how to improve on what code I’ve wrote and if I’m doing it right?
I’m running on a tamiya twin motor gear box, arduino uno r3 and an r3 arduino motor shield.
thanks in advance for the help!
this is everything on my code
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;
#define trigPin 4 // change to 4!!!//
#define echoPin 8 // change to pin 8 on robot to avoid pin clash!!! //
#define pwmA 3 // motor on //
#define pwmB 11 // motor on //
#define brakeA 9
#define brakeB 8
#define dirA 12 //motor control speed pin //
#define dirB 13 //motor control speed pin//
void setup() {
Serial.begin(9600);
pinMode(dirA, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(dirB, OUTPUT);
pinMode(brakeB, OUTPUT);
pinMode(trigPin, OUTPUT);// set the trig pin to output (Send sound waves)
pinMode(echoPin, INPUT);// set the echo pin to input (recieve sound waves)
}
void loop() {
Serial.begin(9600);
long duration, distance; // start the scan
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1; // convert the distance to centimeters.
if (distance > 25) {
delayMicroseconds(1000);
digitalWrite(dirA, LOW); // motor A setup / changing all highs to lows//
digitalWrite(brakeA, LOW);
analogWrite(pwmA, 150);
digitalWrite(dirB, LOW); //motor B setup//
digitalWrite(brakeB, LOW);
analogWrite(pwmB, 150);
}
else if (distance < 25);
{
/*if there’s an obstacle 25 centimers, ahead, do the following: */
delayMicroseconds(400);
digitalWrite(dirA, HIGH); // motor A setup//
digitalWrite(brakeA, LOW);
analogWrite(pwmA, 150);
digitalWrite(dirB, LOW);
digitalWrite(brakeB, LOW);
analogWrite(pwmB, 150);
}
}