Battery affects timings

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);
}
}

You would need a position sensor on the wheels or motor drive. Failing that, you could read the battery voltage and calibrate the delays to match it.

1 Like

Can you tell us more about your robot please? Which microcontroller are you using? Which motor controller? what motors?
Provide links eg

https://uk.rs-online.com/web/p/dc-motors/2389721/

Have you measured the timings or are you surmising they are different because the distance travelled is differernt?

1 Like

Please use code tags when posting. You can edit your post to add them.

Motor speed depends very strongly on battery voltage, so delays usually don't work for robot positioning. You need motor or wheel encoders for speed control, which will also give distance traveled (if the wheels don't slip).

1 Like

Thanks for replying, I'm using a arduino uno as a microcontroller and L298N DC Dual H-Bridge Stepper Motor Driver in terms of motors im using a generic DC Gear Motor 12V 399RPM. I don't know if you can access the links since im located in the Philippines but here are the links for the motor and Hbridge

https://shopee.ph/L298N-DC-Dual-H-Bridge-Stepper-Motor-Driver-Board-Module-L298-i.18252381.741424410
https://shopee.ph/DC-Gear-Motor-12V-399RPM-–-SGM25-370-i.18252381.2275225831

I did not measure the timings exactly but I can tell that they are different because the travelled distance is different.

Thanks!

he travelled distance is different. means one of TWO things:
1: the timings have changed significantly (very unlikely as its crystal controlled clock)
2: the motor speeds are different when you change the voltage. ( yes they are.)

You can get a ROUGH idea of how far its travelled if you measure the motor speed as its going.
To get an accurate position (eliminating wheel losses) you need to measure position relative to the ground (a bit like a mouse) or with GPS.

1 Like

Is there a way for me to set the motors to a specific voltage? I'm using a 3s 11.V battery. Is it possible for me run the motors on a constant voltage like they will only run if all cells are on 4.18v or so?

Thanks

Not easily. :astonished:

Not with the controller you are using. Even on a constant voltage they will run different speeds depending on coefficient of frictionn of the surface, surface roughness, gradient, ....

You need position feedback.

1 Like

Can you please recommend a controller that I can use? Or any other routes that I can go to if I cant acquire the said controller?

Several have been mentioned in the replies. Please read them.