Arduino Tutorial 40: Controlling DC Motor Speed and Direction with Pushbuttons

preview_player
Показать описание
You guys can help me out over at Patreon, and that will help me keep my gear updated, and help me keep this quality content coming:

You guys can help me out over at Patreon, and that will help me keep my gear updated, and help me keep this quality content coming:

In this tutorial we show how to control the speed and direction of a DC motor using two push buttons. One increments speed, and the other decrements speed. We look at the special cases needed to make things work smoothly.

You can get the kit I am using for this series at the following link:

Follow these lessons for free at our WEB site:

#Arduino
Рекомендации по теме
Комментарии
Автор

In just a week with Paul, I learned 10 times more than in a whole semester at the university.

alexandrubitlan
Автор

Finally completed after 3 hours, feeling delighted to complete 40 lessons. Today is 22/6/2024 and I target to complete the course by July 15. Once again thanks a lot for these quality lectures. May god bless you with happy and long life.

LakshayKaushik-bswh
Автор

I definitely underestimated the difficulty of this assignment. I struggled mightily. I had it working in both directions but didn't figure out how to get them both working at the same time. The absolute value is what I was missing. I was trying other shenanegins to make it work but struggled. I spent several hours and finally resigned to listening to how you did it. Well done and thanks for making it all work in the end.

tntragan
Автор

I completed 40 lectures to date on 27/Aug/2022 and now I feel so confident. thanks, Sir for this wonderful Arduino series

RBlabs
Автор

This was the only homework that I found difficult, literally it took me 2 days. But at last I did it.

sourishthakral
Автор

I am embarrassed to say I did not get it totally to work until after I saw this tutorial. I failed to make the transition work and my code was overly complex. Twenty-five years ago, I probably would have solved it. But I did learn a lot. You are a great teacher. Thank you, Bob

robertschuldenfrei
Автор

this project was definitely one of the trickier ones. i thought it would be simpler since the we had already done the LED dimming. in the end i did manage to get it working perfectly. if being graded and not looking at the serial monitor, then yes, 100. however, if looking at the serial monitor, maybe 95, because I pegged at 250/-250 instead of the full 255/-255 since i was overthinking it and was trying to add < and > equations to my project. also, before watching this video, i thought i had done it in an incorrect manner since i had 6 "if" statements in immediate succession.


EDIT: i decided to use the joystick, but keep the speed selection of this project. left flick acts as decrease button, right flick as increase button. and the switch on the joystick now resets to 0.


saying all that, these lessons really helped in my coding process and i am getting much better at the whole code writing/debugging thing. at first, i would wire the circuit and code basically the whole thing, only to then have nothing work, spend an hour or two debugging without any results, and i would eventually have to start from scratch.
now, i take tiny baby steps. for example on this project, the first step was to increase the counter by 10 with a button. then other button decrease 10. then i worked on the conditions. and finally, when the counter worked flawlessly, i added the motor commands and wired it up.

carlos
Автор

I didn't set up the start at 100 but I had it working. I did not use absolute value, I multiplied by -1. My buttons worked well before I powered the motor but while the motor was running they periodically failed, I could see it in the serial monitor so I was thinking the motor was producing some noise. These are really good learning exercises.

rickwellens
Автор

Wow, best lesson yet. It took me 3 tries just following you to get it to work, but work it does. The flowcharting really helps to understand what is required. I did Computer Science in high school in the early 70's and it bored the heck out of me but we were only doing theory at the time. Punch cards were high tech and motorcycles were the only thing that got my attention. I remember doing circles and triangles and squares and thinking WTF is this about. Now it's real! Thank you Paul.

dougnash
Автор

Same as some others, didn't anticipate how complex the options were.
Thanks as ever.

simontopley
Автор

I did the assignment. It was both complex and exciting. I am enjoying your class. Thanks, Mr. Paul. I used 255 to help the motor start at a low speed. I also programmed it in a way where you have to keep pressing it until the motor reaches your desired speed.

tanvirahmed
Автор

I solved this three different ways because I didn't listen closely enough to your instructions in the previous lesson. Once you recapped the assignment at the start of this video, I stopped and wrote another sketch. The key to doing this easily was your use of the mSpeed being negative to indicate one direction and positive to be the other direction. That is a great tool for this kind of control. I tend to overthink these projects. Thanks!

lorisrobots
Автор

BOOM! I made it!
After starting from the beginning a few times, getting dizzy a few more times and with some variations on your code, I did it!
I've been happy to see how, following your tutorials from the beginning, I'm getting "my little successes".
Thank you very much, Paul, for being the one to blame for this new hobby (although dreamt time ago) ....

Mrerrol
Автор

Dear Paul, I did it on my own, with the help of a number of old lessons. Didn't listen to careful to how you formulated the assignment, so I solved it with to linear equations. My biggest problem, witch delayed me a number of hours, was the need of a fully covering parenthesis in IF-statements with multiple conditions. Now I test programs in small portions. I'm having great fun.

MatsSjoq
Автор

I was unable to do this on my own but, your logic, process and programming was so clear and understandable. Thank you.

davidrender
Автор

Yes, I did it! I got the DC motor with the GND wire torn so I simulated it with the help of the Serial Monitor.
Made 2 mSpeed variables and many if statements and I got the perfect result.

yuval
Автор

I found this to be the most challenging episode/homework since I didn't know about abs(). I had to know if 1. speed was increasing or decreasing. 2. which direction was currently on and then setup 4 different scenarios to control the buttons. The abs() makes it somewhat easier. And I don't recall that this was EVER taught in the series! ;-) . But I got it to work. I completely agree: i had to make the logic/workflow on a piece of paper before coding.

jabber
Автор

After finally finding two of the buttons that actually worked, I got it working on my own, but the motor refused to turn in reverse. Then I watched the video and was reminded to use abs() to set speed. Thanks for this and all your videos because they are refreshing memories of things I did decades ago...

DikHarrison
Автор

Thank you for the great lesson. Finished before watching, here is the code:

void loop() {
// read buttons
int buttonDownVal = !digitalRead(buttonDownPin);
int buttonUpVal = !digitalRead(buttonUpPin);
// we are in (-160, 160) range, stop the motor, next step is 160
if (abs(mSpeed) < 160) {
mSpeed = 0;
step = 160;
} else {
step = 5;
}
// calculate next speed, based on buttons values
if (buttonDownVal || buttonUpVal) {
int nextSpeed = mSpeed - buttonDownVal * step + buttonUpVal * step;
mSpeed = abs(nextSpeed) <= 255 ? nextSpeed : mSpeed;
}
// choose direction based on the mSpeed sign
digitalWrite(directionPin1, mSpeed <= 0); //LH-cw, HL-ccw, LL-stop
digitalWrite(directionPin2, mSpeed >= 0);
analogWrite(speedPin, abs(mSpeed));
delay(dt);
}

I read the buttons, then if the absolute value of the mSpeed is less than the min speed value that starts the motor (mine was sadly 160), I stop the motor and set the step to 160, so that the next click on any button will get me to the speed of 160. Otherwise (the speed is already greater than 160) - i set the step to 5. I calculate the next speed to set, and if the absolute value of the next speed is less then or equal to 255, I set mSpeed to this new speed, otherwise I use the old speed. Finally I set the direction by determining whether the speed is negative or positive.

arseivan
Автор

This was a solid lesson in prior thinking. I honestly didnt have mine set up with the jumps at the start (keeping in mind that I am currently simulating this using 2 leds and varying their brightness because my kit doesnt have that motor controller). I also used analogWrite(speedPin, - mSpeed); for dealing with the reverse values which worked but im glad you showed us the absolute function as well because I didnt know that existed. Thanks again Paul.

reece
welcome to shbcf.ru