Hey, I'm pretty new at using arduino i'm currently doing a project where in my robot runs in a field avoiding some obstacles. The said obstacles are placed in a permanent position. I tried programming it like this (code below) and it appears to be that every time I charge my robot back up to 100% it affects the timing that I set. Is there a way that the delays or the motor speed will be constant even if I charge the battery? Thanks!
//defining pins//
int ENA = 9;
int ENB = 10;
int MOTOR_A1 = 2;
int MOTOR_A2 = 3;
int MOTOR_B1 = 4 ;
int MOTOR_B2 = 5;
void setup() {
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(MOTOR_A1, OUTPUT);
pinMode(MOTOR_A2, OUTPUT);
pinMode(MOTOR_B1, OUTPUT);
pinMode(MOTOR_B2, OUTPUT);
}
void forward() //code to make the robot forward at max speed//
{
analogWrite(ENA, 248); // set right motors speed
analogWrite(ENB, 255); // set left motors speed
//run right motors clockwise
digitalWrite(MOTOR_A1, LOW);
digitalWrite(MOTOR_A2, HIGH);
//run left motors clockwise
digitalWrite(MOTOR_B1, HIGH);
digitalWrite(MOTOR_B2, LOW);
}
void left () //code to make the robot turn left at max speed//
{
analogWrite(ENA, 248); // set right motors speed
analogWrite(ENB, 255); // set left motors speed
//run left motors anti-clockwise
digitalWrite(MOTOR_A1, LOW);
digitalWrite(MOTOR_A2, HIGH);
//run right motors clockwise
digitalWrite(MOTOR_B1, LOW);
digitalWrite(MOTOR_B2, HIGH);
}
void right () //code to make the robot turn right at max speed//
{
analogWrite(ENA, 248); // set right motors speed
analogWrite(ENB, 255); // set left motors speed
//run right motors anti-clockwise
digitalWrite(MOTOR_A1, HIGH);
digitalWrite(MOTOR_A2, LOW);
//run left motors clockwise
digitalWrite(MOTOR_B1, HIGH);
digitalWrite(MOTOR_B2, LOW);
}
void loop() //initial code of trying to traverse the field//
{
forward();
delay(610);
left();
delay(210);
forward();
delay(230);
left();
delay(140);
forward();
delay(275);
right();
delay(245);
forward();
delay(245);
left();
delay(245);
forward();
delay(100);
left();
delay(250);
right();
delay(250);
forward();
delay(70);
right();
delay(460);
}
}