Hi
@jorpec thanks again. I'll try the setting one motor higher trick. I am using pins that do work with analogWrite() .
-p.
Wow I really have to stop getting on the forum at midnight
. @jorpec I just looked at your other topic concerning using an IR sensor to let the robot "see" things off to the side and not hit them and I think I'll have to add a servo or something to turn the Ultrasonic sensor to "see" more, something like "check right, left and forwards an if there's nothing within 20 cm go forwards." Of course that is a lot simpler than what I'll actually have to program it to do but you probably get the idea. Since I haven't tried to code this yet I don't know if I'll have to ask the community for help but I'm just putting this information out there in case anyone wonders where that new snippet of code came from. Again sorry if I'm incoherent, it's late.
-p.
My robot has a servo attached to the ultrasonic sensor, but only moves left or right when it "see" something, in your case if the servo moving left and right constantly i guess it will "see" obstacles on a wider angle
Hi
I changed all pwm2 values from 100 to 125 and that solved the motor speed difference problem.
I also just changed the time the LED was on during setup from 5 seconds to 2 seconds. New 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(in1, LOW);//added2
analogWrite(pwm1, 0);//added3, is this necessary? speed control
digitalWrite(in2, HIGH);//left motor forward
analogWrite(pwm1, 100);//speed control
digitalWrite(in3, LOW);//added2
analogWrite(pwm2, 0);//added3, is this necessary? speed control
digitalWrite(in4, HIGH);//right motor forward
analogWrite(pwm2, 125);//speed control
delay(1000);
digitalWrite(in2, LOW);
analogWrite(pwm1, 0);//speed control
digitalWrite(in4, LOW);
analogWrite(pwm2, 0);//speed control
delay(1000);
digitalWrite(in1, HIGH);// left motor backwards
analogWrite(pwm1, 100);//speed control
digitalWrite(in3, HIGH);//right motor backwards
analogWrite(pwm2, 125);//speed control
delay(1000);
digitalWrite(in1, LOW);
analogWrite(pwm1, 0);//speed control
digitalWrite(in3, LOW);
analogWrite(pwm2, 0);//speed control
digitalWrite(led, HIGH);//led on for five seconds
delay(2000);//changed from 5 sec to 2 sec
digitalWrite(led, LOW);// led off
Serial.begin(9600);// stops doing what i want it to do here
}
void loop(){
digitalWrite(in1, LOW);//added
analogWrite(pwm1, 0);//added, speed control
digitalWrite(in3, LOW);//added
analogWrite(pwm2, 0);//added, speed control
digitalWrite(in2, HIGH);//left motor forwards
analogWrite(pwm1, 100);//speed control
digitalWrite(in4, HIGH);//right motor forwards
analogWrite(pwm2, 125);//speed control
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 >20){
digitalWrite(in1,LOW);//added
digitalWrite(in2, LOW);//left motor, backwards
analogWrite(pwm1, 0);//speed control
digitalWrite(in1, HIGH);
analogWrite(pwm1, 100);//speed control
//for (i=0; i<255; i++){ i can't even remember why i commented this out. any ideas?
// delay(10);}
digitalWrite(in3,LOW);//added
digitalWrite(in4, LOW);// right motor, backwards
analogWrite(pwm2, 0);//speed control
digitalWrite(in3, HIGH);
analogWrite(pwm2, 125);//speed control
for (i=0; i<255; i++){
delay(10);}
digitalWrite(in3, LOW);//start both motors forward again
analogWrite(pwm2, 0);// added, is this one of the parts you were talking about? speed control
digitalWrite(in4, HIGH);
analogWrite(pwm2, 125);//added, speed control
digitalWrite(in2, LOW);
analogWrite(pwm1,0);//added, speed control
digitalWrite(in1, HIGH);
analogWrite(pwm1, 100);//added, speed control
delay(30);
}
else{
digitalWrite(in1, LOW);//added
analogWrite(pwm1, 0);//added, speed control
digitalWrite(in2, HIGH);//keep going straight
analogWrite(pwm1, 100);//speed control
digitalWrite(in3, LOW);//added
analogWrite(pwm2, 0);//added, speed control
digitalWrite(in4, HIGH);
analogWrite(pwm2, 125);//speed control
}
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29/ 2;}
-p.
Hi
Sorry for not getting around to this sooner but I am going to declare this solved. I realized as I learned more about Arduino that I don't know what the code I was basing mine off of was trying to do, because it literally makes the robot back off and then run into the wall. Or at least my changed up one did, probably a part I missed. I will keep working on my project, incorporating turning and probably more sensors. So the issues this topic was on are solved (that's not saying that there won't be more) though my robot is far from done! Thanks everyone for your time and help!
-p.