Hello. I am using an Arduino Uno and a L298N board to control 2 small DC motors. I want them to independently accelerate to top speed, maintain for a random time, decelerate to stop, pause for an random time then repeat in the opposite direction. The code I have so far does part of the job, but I can't work out how to write the code so that at any point in time the motors could be both moving in the same direction, or opposite directions, or one/ both stopped etc....
The motors need to turn a largish wooden "fan" which has a fair bit of inertia to get going.
I've also read that "block" 9V batteries are not a good option for this project. Is that true?
Would really appreciate anyone who could get in touch and advise as I really don't know what I'm doing!!!
thanks so much. Julius
Hi!
Maybe this video can be of some help --> Controlling DC Motors with the L298N H Bridge and Arduino - YouTube
Otherwise, post your actual code using this button -->
and specify exactly what you need it to do.
It depends what you mean by "block". That could mean almost any battery with a cuboid shape!
If you mean a PP3 size 9V battery, like from a smoke alarm, then there are almost no Arduino projects for which they are suitable. Anything involving motors, definitely no.
What voltage range can your motors run with?
What other types of batteries do you use in your household? It's almost 2022 and we should all be using rechargeable batteries whenever we can.
L298 controllers are an old design with a high voltage drop, making them a poor choice for battery powered projects.
As for controlling the motors independently, the uses you describe are absolutely possible. It's just a matter of coding.
To operate the motors independently you need to code in a state-machine style so that you don't use blocking operations such as delay(). Have a look at how BlinkWithoutDelay example works to see how this is done, and maybe read up about state machines and event-driven coding.
The standard small alkaline PP3 sized 9V batteries are good for low currents only, perhaps 50mA to 100mA tops. Motors take 0.5 to several amps typically, so a no go.
However there are some lithium "9V" batteries that are really a 2S LiPo pack inside a PP3-format case - these are really 7.4V but can handle much higher currents (and need a special charger...).
It would really help if you posted that code.
thanks for the YT recommend - this was where I pulled the code from (and tweaked it a bit before I hit the wall).
thanks for the info. That would explain how i've already burned through one PP3 9V battery just while trying to get the coping right!
These are the motors I'm trying to control:
here is the code I have so far:
it does turn the motors but they are controlled together - so they both start and accelerate and stop together. I'd like them to act individually so they both spin independently (and somewhat randomly).
// motor A
int enA = 10;
int in1 = 8;
int in2 = 7;
//MOTOR B
int enB = 9;
int in3 = 6;
int in4 = 5;
void setup()
{
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void demoTwo()
// turn one way
// accelerate
{
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
for (int i = 0; i < random(120,250); i++)
{
analogWrite(enA, i);
analogWrite(enB, i);
delay(random(50,100));
// decelarate
}
for (int i = random(120,250); i >= 0; --i)
{
analogWrite(enA, i);
analogWrite(enB, i);
delay(random(200,400));
}
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,LOW);
}
void demoThree()
// turn other way
{
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
for (int i = 0; i <= 250; i++)
{
analogWrite(enA, i);
analogWrite(enB, i);
delay(random(200,300));
}
for (int i = 250; i >= 0; --i)
{
analogWrite(enA, i);
analogWrite(enB, i);
delay(random(200,400));
}
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,LOW);
}
void loop()
{
demoTwo();
delay(random(4000));
demoThree();
delay(random(4000));
}
3V motors? Where are you getting the 3V supply to power them?
As expected, delay() within for() loops. Classic "blocking" code, which prevents the Arduino from doing multiple things concurrently. It's time to take that next step in the level of your programming skills!
It's a little like learning to prepare a meal with several courses, each course consisting of several elements like cooking vegetables, making a sauce, baking bread... You already know all the steps to cook each element by itself, but now you need to plan and sequence the execution of all those steps so that each course comes together at the right time, and do it all by yourself.
Check your PMs.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.