# Arduino Car - Speed, Time, And Direction Control

Facts: I have a car which is based on a Arduino Mega 2560, is a 4WD (all 4 wheels are connected to a motor), runs on a 12.60-12.65V battery, motors are controlled by a L298N, has an LCD, motors recieve 10.15V-10.25V (transistors suck up about 1.4V).

Problem: This might be a little hard to understand, so don't be frustrated if you don't get it the first time. Now, the problem is that I have programmed it to run at full speed (255), and it is going crazy fast, in fact WAY to fast. So I want to slow it down. But, I also need to insert a delay for how long it needs to perform the actions I need it to perform. I already have the correct delay timings. I need to reduce the speed, but increase time. SPEED: LOWER. TIME: HIGHER. But its not like I can just do it. I need to do the math. But I DON"T KNOW THE FORMULA. I know what I need to know. Just tell me what the formula is, and make an example with 1 as the speed. Don't worry about the delays in "void setup()". Here is the code:

const int IN1 = 51;
const int IN2 = 50;
const int IN3 = 53;
const int IN4 = 52;

const int ENA = 13;
const int ENB = 12;

void Turn_Right() {
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
digitalWrite (IN3, LOW);
digitalWrite (IN4, LOW);
analogWrite (ENA, 255);
analogWrite (ENB, 0);
delay(330);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}

void Turn_Left() {
digitalWrite (IN1, LOW);
digitalWrite (IN2, LOW);
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
analogWrite (ENA, 0);
analogWrite (ENB, 255);
delay(330);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}

void Move_Forward() {
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
analogWrite (ENA, 255);
analogWrite (ENB, 255);
delay(382);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}

void setup() {
delay(4000);
Move_Forward();
Turn_Right();
Move_Forward();
Move_Forward();
Move_Forward();
Move_Forward();
Turn_Left();
Move_Forward();
Move_Forward();
Turn_Left();
Move_Forward();
Move_Forward();
delay(30000);
Move_Forward();
Move_Forward();
Move_Forward();
Move_Forward();
Turn_Left();
Move_Forward();
Move_Forward();
delay(40000);
Turn_Left();
Turn_Left();
Move_Forward();
Move_Forward();
Turn_Right();
Move_Forward();
Move_Forward();
Move_Forward();
Move_Forward();
Move_Forward();
Move_Forward();
Turn_Left();
Move_Forward();
Move_Forward();
Turn_Right();
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}

void loop() {

}

Wrong989:

Lol !

Problem: This might be a little hard to understand, so don’t be frustrated if you don’t get it the first time.

Ok, I’ll try real hard.

I need to reduce the speed, but increase time.

Now then, IF you write 255 for full speed and when you change it to half that (127) the speed was exactly half, then you would have to double the time.

So your math would be like:

``````long percentSpeed= 50;    // 0 is off, 100 is full speed. Example: 50 is half speed

long fullSpeed= 255;

long timeAtFullSpeed= 330;

int newSpeed, newTime;

// calculate newSpeed and newTime based on the percentSpeed setting:
//
newSpeed= (fullSpeed * percentSpeed) / 100;

// we can only calculate the time for a minimum of 5% speed
// (if speed was zero, it would take forever!)

if( percentSpeed <= 5 ){
newTime= 30000;
}else{
newTime= (timeAtFullSpeed * 100) / percentSpeed;
}
``````

…but your motors are unlikely to work exactly like that. Try it anyway.

Yours,
TonyWilk

Who was it that said all threads with the word Urgent should be pinned to the top of the second page?

Wrong989:

Why should we attend to your problem ahead of other people who are ahead of you in the queue.

You did not even take the trouble to make it easy for people to help you by posting your code using the code button </>. See How to use the Forum

And this is ridiculous - even if you don’t want an answer quickly.

Problem: This might be a little hard to understand, so don’t be frustrated if you don’t get it the first time.

If you know it is hard to understand then re-write it so that it is easy to understand.

Now I will see if there is someone more reasonable whom I can help.

…R

Thanks, all of you.

Wrong989:
Thanks, all of you.

Aww Shucks. T'were nothin'

...R

Wrong989:

Facts: I have a car which is based on a Arduino Mega 2560, is a 4WD (all 4 wheels are connected to a motor), runs on a 12.60-12.65V battery, motors are controlled by a L298N, has an LCD, motors recieve 10.15V-10.25V (transistors suck up about 1.4V).

Problem: This might be a little hard to understand, so don't be frustrated if you don't get it the first time. Now, the problem is that I have programmed it to run at full speed (255), and it is going crazy fast, in fact WAY to fast. So I want to slow it down. But, I also need to insert a delay for how long it needs to perform the actions I need it to perform. I already have the correct delay timings. I need to reduce the speed, but increase time. SPEED: LOWER. TIME: HIGHER. But its not like I can just do it. I need to do the math. But I DON"T KNOW THE FORMULA. I know what I need to know. Just tell me what the formula is, and make an example with 1 as the speed. Don't worry about the delays in "void setup()". Here is the code:

const int IN1 = 51;
const int IN2 = 50;
const int IN3 = 53;
const int IN4 = 52;

const int ENA = 13;
const int ENB = 12;

void Turn_Right() {
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
digitalWrite (IN3, LOW);
digitalWrite (IN4, LOW);
analogWrite (ENA, 255);
analogWrite (ENB, 0);
delay(330);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}

void Turn_Left() {
digitalWrite (IN1, LOW);
digitalWrite (IN2, LOW);
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
analogWrite (ENA, 0);
analogWrite (ENB, 255);
delay(330);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}

void Move_Forward() {
digitalWrite (IN1, HIGH);
digitalWrite (IN2, LOW);
digitalWrite (IN3, HIGH);
digitalWrite (IN4, LOW);
analogWrite (ENA, 255);
analogWrite (ENB, 255);
delay(382);
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}

void setup() {
delay(4000);
Move_Forward();
Turn_Right();
Move_Forward();
Move_Forward();
Move_Forward();
Move_Forward();
Turn_Left();
Move_Forward();
Move_Forward();
Turn_Left();
Move_Forward();
Move_Forward();
delay(30000);
Move_Forward();
Move_Forward();
Move_Forward();
Move_Forward();
Turn_Left();
Move_Forward();
Move_Forward();
delay(40000);
Turn_Left();
Turn_Left();
Move_Forward();
Move_Forward();
Turn_Right();
Move_Forward();
Move_Forward();
Move_Forward();
Move_Forward();
Move_Forward();
Move_Forward();
Turn_Left();
Move_Forward();
Move_Forward();
Turn_Right();
analogWrite(ENA, 0);
analogWrite(ENB, 0);
}

void loop() {

}

Im trying to do something similar actually, but without supplying power to the wheels. I just need to control their direction from a hydraulic actuator and an electric pump thats supplied from a 12 V DC battery. Do you mind explaining how you have accomplished your turning mechanism? Im completely new to all of this.

@Jtfox12, I'm not sure that this is the star you should be hitching your wagon to. Have you read this Thread carefully?

And the code in the Original Post is certainly not a useful example for anyone doing a senior college project.

...R

@Jtfox12, would love to help you. But please tell the specifications of your car, like the circuit schematic. That is for further help. Anyway, my car is a 4WD, so here's how it works:

MoA is in the top right corner, and MoB is in the top left corner, vice versa for the bottom (C+BottomRight D+BottomLeft). Now, I am using a L298N, which has 2 motor outputs, but I have 4 motors, so I did MoA+MoC >> Output 1, and MoB+MoC >> Output 2. To turn right, Out1 goes low, Out2 goes high. Vice versa for left.

Personally, if it turns the correct amount at a remotely livable speed, I would not touch the code that turns it. Trying to get what appears to be tank steering correct based entirely on time delay is a ball of wax I wouldn't touch with a 39 and a half-foot-pole.

@TonyWilk gave you about the best starting point for straight line speed. Whatever you divide your speed by, multiply your time by. Tweak from there.

Thanks, SteevyT. My car is going at a "not livable" speed, because it is way too fast, precisely 1.595811518324607 meters in 1 second (1000 MS).

Wrong989:
Thanks, SteevyT. My car is going at a "not livable" speed, because it is way too fast, precisely 1.595811518324607 meters in 1 second (1000 MS).

How did you possibly measure to that level of accuracy? Or have you not studied significant digits yet?

Delta_G:
How did you possibly measure to that level of accuracy? Or have you not studied significant digits yet?

Wow, even my Angstrom Calipers aren’t that good. 'E must 'ave one of those Proton Tape Measures

Yours,
TonyWilk

I used a math formula I learned in school. Checked it with a speed calculator.

OK, So you need to study something called significant digits

You can't just say that you know a number to fifteen decimal places just because your calculator can go to that level of accuracy. It matters how many digits of accuracy each of the measurements you made have and how the math puts them together. It's called propagation of error. In order to get the value you gave you would have to have measured the position and timing of the car to an impossible level of accuracy. That number needs to be rounded off a little.

I know what significant digits are, please dont try to give me a math lesson.

Wrong989:
I know what significant digits are, please dont try to give me a math lesson.

Ah, so "precisely 1.595811518324607 meters" was just a joke then ?