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

Поделиться
HTML-код
  • Опубликовано: 11 сен 2024
  • 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:
    / paulmcwhorter
    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:
    / paulmcwhorter
    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:
    amzn.to/2I7N4Ek
    Follow these lessons for free at our WEB site:
    toptechboy.com...
    [Disclosure of Material Connection: I am a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com. This means if you visit the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.]
    #Arduino

Комментарии • 427

  • @alexandrubitlan6836
    @alexandrubitlan6836 3 года назад +74

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

    • @Theo005-k5e
      @Theo005-k5e 2 года назад +1

      bravo mai

    • @bhushansurve3503
      @bhushansurve3503 7 месяцев назад

      I still wonder, what the heck did i do at the Uni..

  • @LakshayKaushik-bs5wh
    @LakshayKaushik-bs5wh 2 месяца назад +1

    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.

  • @tntragan
    @tntragan 2 года назад +16

    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.

    • @trumpatier
      @trumpatier 2 года назад

      I had the same problem. The absolute value function was unknown to me, but it was much more straightforward when Paul told us about it!

  • @RBlabs
    @RBlabs 2 года назад +8

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

  • @sourishthakral
    @sourishthakral Год назад +3

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

  • @robertschuldenfrei2402
    @robertschuldenfrei2402 9 месяцев назад +1

    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

  • @carlos2004
    @carlos2004 4 года назад +18

    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.

  • @rickwellens2543
    @rickwellens2543 2 года назад +1

    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.

  • @simontopley4771
    @simontopley4771 9 месяцев назад +1

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

  • @reece674
    @reece674 2 года назад +2

    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.

  • @tanvirahmed8586
    @tanvirahmed8586 2 года назад

    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.

  • @davidrender4028
    @davidrender4028 2 года назад +2

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

  • @thelocal911
    @thelocal911 Год назад +1

    A- Because I used 11 speed presets from -5 to 5 instead of incrementally increasing the analog output. Less resolution perhaps, but I think it's more functional. I'm needing to troubleshoot my code less and less with each homework assignment. I love this!

  • @dougnash6316
    @dougnash6316 4 года назад +1

    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.

  • @yuval9197
    @yuval9197 6 месяцев назад

    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.

  • @kirstinlee6838
    @kirstinlee6838 6 месяцев назад

    Thank you so much for these videos! I was able to set up my board, but watched the video to see how you programmed the buttons. I was so excited getting the fan to turn on and speed up and go backwards.

  • @MatsSjoq
    @MatsSjoq 2 года назад

    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.

  • @Mrerrol13
    @Mrerrol13 3 года назад +1

    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) ....

  • @Rahul-nw3xk
    @Rahul-nw3xk 3 года назад +1

    It took me three to four attempts but I did it. Thanks for uploading these videos these are helping me a lot when my collage is closed in lock-down due to pandemic .

  • @lorisrobots
    @lorisrobots 2 года назад

    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!

  • @jabber2824
    @jabber2824 4 месяца назад

    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.

  • @keithbrown162
    @keithbrown162 8 месяцев назад

    I completed the previously assigned double button to control the motor. One of the problems I ran into was that one of the pull up resistor buttons didn't seem to be working right. I finally figured out that by unplugging the Arduino and then plugging back in - it worked! I really relied on the serial printer to gradually develop my code and find out how it was working at each step.
    I also bought some Duck brand poster putty that worked well for sticking things down. You had previously mentioned that you didn't know it's real name. You just called it goop or something.
    Once again another excellent lesson.

  • @AlexDavis-ch1ro
    @AlexDavis-ch1ro Год назад

    Great project! I gave myself a solid B grade for getting the ideas correct, but forgetting all of the extra 'if' statements.
    I added a variable for increments, and an extra button for a STOP / RESET. Thanks again!!!

  • @haastrupadebayoibukun6817
    @haastrupadebayoibukun6817 2 года назад

    I did it. I had to break the codes into chunks in order to get the flow of the first push button. Although I didn't get to halt the motor when the second button was pressed, it went in the anticlockwise direction until the speed dropped to zero. I am obviously improving, all thank to you Mr Paul.

  • @Russell900
    @Russell900 3 года назад

    This was a tough one but I got it all done . I did it differently though, I used two variables for speed. One for forward speed and one for reverse speed. I then used four separate if statements. 1. To increase in forward direction if forward button was pressed and reverse speed is zero. 2. To increase reverse speed if reverse button is pressed and forward speed is zero. 3. to decrease forward speed if reverse button was pressed and forward speed was greater than zero. 4. to decrease reverse speed if forward button was pressed and reverse speed was greater than zero. Took me a while to figure out but satisfying to get it going when I finally did.
    Thanks again for the videos, really enjoying myself working though them.

  • @426F6F
    @426F6F Год назад

    Ohh, after watching the introduction, I realized that I misheard the instructions from the last video. I must have just been excited to start! 😅 But I ended up figuring it out!
    I set it up so that if the down button is pressed at 0, the program changes the value of a fan rotation variable to reversed, the HIGH/LOW values of the directional pins, and swaps the add/subtract function of the up and down buttons in the reversed direction. I had to make two separate functions for both buttons, one in the forward direction, and one in the reversed direction. I seriously don't know how I didn't end up getting stumped.
    You're a legend of a teacher Paul, thanks!

  • @bleujaegel
    @bleujaegel Год назад

    I was able to figure it out, but it took my a few days. My approach was completely different. Took me 158 lines, but works perfectly. Going to redo it your way to learn how to program it more efficiently. Thankfully I kept my day job...

  • @abdullahmuslim280
    @abdullahmuslim280 4 года назад

    I did the homework and it was a good one. I see that Paul did not code the buttons State ..."newButton/oldButton. I did and it just seems the right thing to do when I code now. working with the button state, I was able to get better control if the motor. I do wont to say ,it was Paul who showed me how to code the button state, Ty Paul.

  • @olegvelichko1659
    @olegvelichko1659 4 года назад +3

    Just in time! Thank You for the hard work you’re putting into maintaining this channel. This is some of the best tutorial material out there. I’ve been an arduiner for a while, and have watched many days’ (if not months’) worth of videos, and this is just PERFECT. To be honest I already know most of this and have done it, BUT!!! your way of explaining the nitty-gritty and what’s going on under the hood gives me a fresh look at the whole system. Gives me a deeper UNDERSTANDING of what’s happening, rather than just textbook “knowing”. I catch myself on the thought “Hm. This is pretty neat!” when you’re going over some of the formulas (especially in the last video talking about mapping the joystick). And I don’t “do” math.
    THANK YOU for this feeling.
    THANK YOU for this content.

  • @jackmehoff5433
    @jackmehoff5433 3 года назад +2

    I was able to do this one on my own. Instead of directly altering mSpeed with the buttons, I used the buttons and a variable I called speedVal to operate as almost like a potentiometer, incrementing from 0-1023 and then used this value to do the linear equation with a y1 value of 100 and output mSpeed from that. Some if statements later and it worked well. Not sure if it was easier or harder than the method you chose but it worked out well in the end.

  • @Mouse_007
    @Mouse_007 3 года назад

    My ability to successfully complete the homework with no bugs or glitches only proves you are a great teacher . Thank you Paul I'm so excited and my confidence just shot through the roof ! The hints at the end of the previous video helped immensely.
    I used an "if else" to turn a negative motor speed into a positive output to motor, didn't know abs().
    Also my buttons were programmed as toggles so no matter how long a button was pressed you only got plus or minus 10 per press.

  • @ErlankBru
    @ErlankBru 3 месяца назад

    I had some difficulty figuring out the main approach, but with Paul's greatest way of explaining where I was unsure, this was a very fun project I really enjoyed. To be honest I had the cat by its tail at first, thinking I will need button states, but it turned out a lot easier for me as it wasn't the case.😀 The "absolute" number part is a great addition to our skills! Thanks again Paul, you are a fantastically super awesome teacher!🏆
    I still struggle with the button state ideas, but it is something I will need to sit down with on my own and play/test it out.😇

  • @ABSP5766
    @ABSP5766 4 года назад

    I managed to solve this problem in a very similar way, but it took me quite a while and before I tried several crazy algorithms that didn't work. I think I learnt quite a lot from the whole process.

  • @davidbuckley4912
    @davidbuckley4912 Год назад

    Hi Paul. I did get it to work, including the jump to a higher value on the first button press. I did not think about the issues coming down in speed, so needed a couple of extra button presses to stop. It took me all day, and my code is a lot chunkier than yours, but it worked. All my previous efforts at coding before finding your lessons have been a complete failure, so thank you. I will try to do it again using your method, but I am very happy that I was able to get most of the way there.

  • @dommacdo
    @dommacdo 3 года назад

    I got it working, incrementing 1 by 1 not 10 by 10. It stops at 105 and jumps straight to -105 (but really, it should go to about 107 / -107) to make sure that it starts). And rather than mSpeed=0 I used
    digitalWrite(dir1, LOW);
    digitalWrite(dir2, LOW);
    between 100 and -100 included.
    Another excellent lesson, thank you!

  • @jeantorres3293
    @jeantorres3293 3 года назад

    First homework to really get me thinking, had to buy a soldering kit and learn to solder to fix my motor to check my code, in the end I got everything to work on my own, can't wait to see your solution right now!

  • @robertschuldenfrei2402
    @robertschuldenfrei2402 9 месяцев назад

    This morning I reassembled my circuit and it just decided to work :). Perhaps I just had the power supplied reversed such that the + pin was seated on the breadboard - rail. I can now do my experiments to see where the transition to 0 takes place. I do not have time to write the assignment code today (11/29/23) but will probably do it on Friday (12/01/23). I do not know if anyone is reading these comments as it has been many years since the lesson was delivered, but it is useful to me.

  • @DikHarrison
    @DikHarrison 4 года назад

    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...

  • @thebrewsterblock
    @thebrewsterblock 5 месяцев назад

    Most challenging yet. Great lesson. I looked back at your 2 button lessons and assumed I would need to track the last status of each button. I also tried to handle the going over max speed by altering the increment down to 5 instead of 10. One more bad - I tried stacking or nesting all those IFs and spent hours trying to sort all the logic and positions of the numerous "close" curly brackets for 7 ELSE statements. Kept getting errors. Stubbornness over practicality. Your algorithm was much simpler - classic KISS method. Tried but no way I would have cracked this myself. I am an old procedural flowchart, COBOL/PL/1 programmer and this is a different world. Never could handle Assembler and I'm sure C or C++ would kill me!

  • @doverivermedia3937
    @doverivermedia3937 Год назад

    Great Vid as always Paul. Thank you. I did the HWrk referring back to 27 & 29 adding the 10k pull-up resistor method (instead of the Hack). Got the motor smoothly stepping through speeds. I spend hours trying to get the reverse working in the right place and had to 'fold' but only after many, many attempts. I put in a red LED to signify hitting extremities of speed (just because I could), and did figure out the 'capping' of speed. I also did a sketch of the basic working prior. THAT is a very good tip.

  • @kaindradjoemena7789
    @kaindradjoemena7789 3 года назад

    Hello Mr. McWhorter! My name is Joe. I’m a 15 year old from Indonesia 🇮🇩. I wanted to say happy birthday 🎉. You’ve been helping me with my Arduino so much and you’ve been motivating me so much. Sorry if I’m late or too early. I just wanted to let you know that I feel so lucky to be able to learn from an excellent teacher like you. Again, happy birthday 🎉
    God bless.
    Joe

  • @stuartweldon8333
    @stuartweldon8333 3 года назад

    Definitely a F for me until you started drawing out a plan of attack, that's when I knew how to do this. Absolutely great way of teaching.

  • @dieseltinus6680
    @dieseltinus6680 3 года назад

    I gave the up/down switches a toggle function because I thought that was what you meant. Got it working flawless after a couple of hours. Give myself a A+ for that.
    It looks so easy when you do it.

    • @paulmcwhorter
      @paulmcwhorter  3 года назад

      A+

    • @carbon273
      @carbon273 3 года назад

      I did the same thing. I used a third switch as a toggle switch to change directions everytime it’s clicked. Slightly different end result but I think it allows for more control from the user.

  • @cacophonixgaul6170
    @cacophonixgaul6170 Год назад

    Thank you Paul for showing not only how to begin with program but also how to trouble shoot.

  • @zachhamersley967
    @zachhamersley967 3 года назад

    Had a hard time with the code for the homework. Didn't think to stack so many if statements together, assumed that the code was more complicated...So, a simple yet complicated project. Learned a lot from this lesson. Thanks Paul

    • @paulmcwhorter
      @paulmcwhorter  3 года назад +1

      We learn sometimes by doing it the wrong way, and then seeing a better way. Just keep trying.

    • @zachhamersley967
      @zachhamersley967 3 года назад

      @@paulmcwhorter Thanks for the words of encouragement. You dont get anywhere if you dont keep trying!

  • @PedroLuis-yp9ed
    @PedroLuis-yp9ed 3 года назад

    OMG!! I was trying to get this to work from 12am to 12pm. I managed to get this to work with nested if statement which scrambled my brain a bit trying to picture the logic but despite one minor bug it basically worked. I didn't have a cap though for 255 and a jump start to 100, but I think my code would have broke again if I tried that lol. The abs statement however I never knew about which would have made things WAY easier. I had a chunk of code just for converting the negative values for the motor. I posted what I did below. I definitely did NOT expect this to be this complicated being a simple pushbutton.
    nt speedPin=5;
    int dir1=4;
    int dir2=3;
    int pButton1=12;
    int pButton2=11;
    int buttonVal1;
    int buttonVal2;
    int dt=250;
    int j=0;
    int X=0;
    int power=50;
    void setup() {
    Serial.begin(9600);
    pinMode(speedPin, OUTPUT);
    pinMode(dir1, OUTPUT);
    pinMode(dir2, OUTPUT);
    pinMode(pButton1, INPUT);
    pinMode(pButton2, INPUT);
    }
    void loop() {
    buttonVal2=digitalRead(pButton2);
    buttonVal1=digitalRead(pButton1);
    Serial.print("B1: ");
    Serial.println(buttonVal1);
    Serial.print("B2: ");
    Serial.println(buttonVal2);
    if(j>0){
    if (buttonVal1==0){
    j=j+power;
    X=j;
    analogWrite(speedPin, X);
    Serial.print("+");
    Serial.println(power);
    digitalWrite(dir1, HIGH);
    digitalWrite(dir2, LOW);
    }
    if (buttonVal2==0){
    j=j-power;
    X=j;
    analogWrite(speedPin, X);
    Serial.print("-");
    Serial.println(power);
    digitalWrite(dir1, HIGH);
    digitalWrite(dir2, LOW);
    }
    }
    else{
    if (buttonVal1==0){
    j=j+power;
    X=(j*(-1));
    analogWrite(speedPin, X);
    Serial.print("N+");
    Serial.println(power);
    digitalWrite(dir1, LOW);
    digitalWrite(dir2, HIGH);
    }
    if (buttonVal2==0){
    j=j-power;
    X=(j*(-1));
    analogWrite(speedPin, X);
    Serial.print("N+");
    Serial.println(power);
    digitalWrite(dir1, LOW);
    digitalWrite(dir2, HIGH);
    }
    }
    Serial.print("j: ");
    Serial.println(j);
    Serial.print("X: ");
    Serial.println(X);
    Serial.println("");
    delay(dt);
    }

  • @chrisb7250
    @chrisb7250 Год назад

    Thank you for another great lesson Paul! I struggled for a little bit, trying to multiply mSpeed by -1 if the value were below zero. without looking ahead I tested my ability to find solutions by looking elsewhere on the web. Surprisingly I typed my question into Chat GPT and they gave a funky solution, but it did have the abs(___) function plugged in at the end in the analogwrite command. AHHHAAA!!! that was exactly what i needed. Now onto watching the lesson. I enjoy these so much. I do them before and after work.

  • @hughpatterson1480
    @hughpatterson1480 2 года назад +1

    Paul, you have done a better job at teaching this material than the professors I studied under at the University California. Don't get me wrong, those guys were good, but you provide a much better foundation for those students who are planning on designing and prototyping electronics. I managed to get the homework right but suspect it would have been an epic fail if I didn't flow chart things out. I learned to flow chart my projects from my wife who is a data analyst and uses them all the time. I am so glad you teach this idea! Thanks for another great lesson (which I'll pass on to my students).

  • @olegvelichko1659
    @olegvelichko1659 4 года назад +33

    And where’s the instructions for the ice coffee?! How can we follow along without the coffee?!

    • @fswarrior9510
      @fswarrior9510 3 года назад

      I dont know how to make it. He should make that tutorial.

    • @mor8266
      @mor8266 3 года назад

      shhhhhhhh

  • @LeoH3L1
    @LeoH3L1 4 года назад +1

    Done it my own way before watching the lesson, there's probably a much easier way to do it, but it works :D
    int speedPin=5;
    int dir1=4;
    int dir2=3;
    int forwardButton=7;
    int reverseButton=8;
    int forwardState;
    int reverseState;
    int forwardFlag=0;
    int reverseFlag=0;
    int forwardNumber=0;
    int reverseNumber=0;
    int forwardLimit=254;
    int reverseLimit=254;
    int mSpeed;
    int delayTime=30;
    void setup() {
    // put your setup code here, to run once:
    pinMode(speedPin,OUTPUT);
    pinMode(dir1,OUTPUT);
    pinMode(dir2,OUTPUT);
    pinMode(forwardButton,INPUT);
    pinMode(reverseButton,INPUT);
    digitalWrite(forwardButton,HIGH);
    digitalWrite(reverseButton,HIGH);
    Serial.begin(9600);
    }
    void loop() {
    // put your main code here, to run repeatedly:
    forwardState=digitalRead(forwardButton);
    reverseState=digitalRead(reverseButton);
    if((forwardState==0)&&(forwardNumber0))
    {
    digitalWrite(dir1,LOW);
    digitalWrite(dir2,HIGH);
    forwardNumber=forwardNumber-1;
    delay(delayTime);
    analogWrite(speedPin,forwardNumber);
    if(forwardNumber==0)
    {
    forwardFlag=0;
    }
    Serial.print("Forward");
    Serial.println(forwardNumber);
    }
    if((reverseState==0)&&(reverseNumber=0))
    {
    digitalWrite(dir1,HIGH);
    digitalWrite(dir2,LOW);
    reverseNumber=reverseNumber-1;
    delay(delayTime);
    analogWrite(speedPin,reverseNumber);
    if(reverseNumber==0)
    {
    reverseFlag=0;
    }
    Serial.print("Reverse");
    Serial.println(reverseNumber);
    }
    }

  • @Jim-mn7yq
    @Jim-mn7yq 4 года назад

    I did the project and enjoyed it, however I misinterpreted the specs. I thought the user would press a button and once pressed the motor would incrementally increase in one direction toward a ceiling of 255. But at any time, if the other button were pressed, the motor would slow down to a zero point and restart in the other direction, again moving to a ceiling of 255. I did find a log of special cases. For example once a button was pressed to reverse, we couldn't immediately change directions. Instead I had to decelerate the motor until it reached below 100. At that point, change directions and start the motor moving in the opposite direction with a ceiling of 255. The nice thing about it was that the user could press the button at any point and there wouldn't be a jarring change, but a gradual slowdown, then a direction change.
    Good and challenging project, Paul. Keep em coming.

  • @alanclark7711
    @alanclark7711 2 года назад

    spotted your -55 error; my motor sputters at 100 but will advance; love the lessons - best Christmas present from my son, ever. Haven't done much computer science work in over 20 years, I've retiring in 10 months, I have wanted to get into this so I'm excited!

  • @arseivan
    @arseivan Год назад

    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)

  • @ricardocarmona2196
    @ricardocarmona2196 3 года назад

    I can't tell if I can say I completed this homework on my own, this is because I made my 2 button incriminat by 15 (this means I needed to push the button many times for the fan to start spinning because there wasnt enough power in the beginning) which is what Pual said would not work. However I found it to work fine, but the values would mess up and the fan would not act as intended if someone were to play or use the hardware many times. Therefore, other then that it works fine and I am just going to pass it off as I did it by myself... Regardless, Mr Pual you are an amazing teacher and you kept me working on my project even after I was staring at my code for hours trying to understand why I couldn't make the fan spin the opposite way. So, thank you so much Pual I would not be able to do any of this without you guiding me!

  • @manuelaviles1534
    @manuelaviles1534 4 года назад

    I did it on my own, I plan the strategy while I was coding and did some investigation on how to add multiple functions to the push buttons so I added a function to increase de speed by 5 in one click and if you keep the button press for 2 seconds the speed increases gradually. Also to shut it off by pressing both buttons at the same time . Works like a charm, thanks for everything that you teach.

  • @terryhoward986
    @terryhoward986 3 года назад

    Great homework challenge, got it working before checking solution, took a few attempts to get it right, used the -255 to +255 concept but used variables throughout all formulae, no constants. so min speed is a defined value. Used 6 nested 'if' statements as well, so different approach but like others didn't know about abs() so multiplied motSpd*-1, same outcome.

  • @opalprestonshirley1700
    @opalprestonshirley1700 4 года назад +1

    Yes I did and it was a brain teaser. I forgot about a couple of special cases and for sure it didn't work as it should have. This was a good lesson need to start doing a flow chart and it should keep me on the right path, plus check my 9v battery... LOL. Thanks Paul

    • @carlos2004
      @carlos2004 4 года назад

      just borrow one of the power bricks from one of your other devices. most of them are 12 and the power board included in the kit takes 12v. i had problems running my motors on the battery, so not only because of the drain do i no longer use it. However, most of the time, i simply use the 5v straight from the computer; turns out my usb ports are capable of providing all the current i need, even when i ran dual motors.

  • @tonyperry6702
    @tonyperry6702 4 года назад

    This was a challenge. I didn't get it on my own. Not even close. However, everything you did made sense. Really liked the project.

  • @RobVollmar
    @RobVollmar Год назад

    I was able to complete the assignment. I managed the negative mSpeed variable by converting it into a positive forward motor speed or positive backward motor speed variable by multiplying the latter by -1. Your solution was more elegant but I didn't know about the abs function.

  • @Bouchop6655
    @Bouchop6655 3 года назад

    When thinking about the motor running, I didn't think about the motor having a negative No. value. The 'abs' at the end, showed the thinking behind this but left me little confused. Good lesson and i'll have a better idea of how to approach this the next time. Thanks.

  • @ledogu
    @ledogu 2 года назад

    I did not make it to the end on my own. My code was the same all the way down to the -10 / -90 portion of the code. I did not for see that. I took lesson 29, copied INT section and void set up and pasted it with Lesson 39 (DC motor set up). I figured out the first two "if" statements, then I missed the mSpeed==90 II mSpeed==-95). When I followed you in the lesson the light bulbs was coming on. It all made sense. Thank you.

  • @stevensaunders1502
    @stevensaunders1502 3 года назад

    Hi Paul, took an entirely different approach and I DID THE MATH!
    I use a counter score for the button operation between 0 and 510. I set the motor speed to 0 Between a count of 250 & 260. I reset the counter to 255 each time it went into those values, meaning it went up and down by the same amounts each time through the zero speed transition. I then use a slope calculation with my counter as the X axis and applied the formula to each case, backward or forward to get the mSpeed. I also incorperated the kick start routine for speed

  • @todddyer154
    @todddyer154 2 года назад

    You are right. This was harder then it seemed at first. I had the concept but the specific code was a little beyond me. Thanks Paul. Learned a lot from this one!

  • @kiranbs5057
    @kiranbs5057 3 года назад

    Hey Paul ...Yep I did try the Homework and I was able to control speed only in one direction ...and After watching this episode I just realized I must go until -255 and take abs(-255) . Just few more lines additions ....Yayyyy i was able to do it but ya needed your lil help to finish it . Thanks Paul

  • @ericsmith7988
    @ericsmith7988 4 года назад

    Well I did get it to work mostly. I could get it speed up with one button and slow down with the other button, but could not figure out how to reverse the motor until I watched you do it. Thank you Paul for a great lesson I will be watching this one again. I do watch all your lessons from start to finish and also go back and watch them again. Proper note taking is "key" to understanding how all of this works. Again Thank you

  • @markfuentes3666
    @markfuentes3666 4 года назад +1

    I tried. But I finally had to watch you go through the paperwork. Then I was able to do it. Thanks again.

  • @adamcurtice3584
    @adamcurtice3584 2 года назад

    This one got me. I kept trying to do too much at once. I believe I need to work on my planning stages. Break everything down into smaller pieces. Thanks for the pointers Paul.

  • @krishnapranav9123
    @krishnapranav9123 Год назад

    I did plus 50 instead of 10 and also forgot those special cases
    Taking idea from the graph you showed really helped👍

  • @thysdelport6782
    @thysdelport6782 2 года назад

    This was harder than expected but I got it working well in the end, except the dead band in the middle. My motor works at lower speed and I only had a problem between -20 and 20. Also used a button Val that was the difference between the first and second buttons. Excellent lesson and well presented - we are learning a a lot from you!

  • @sssagc
    @sssagc 3 года назад

    Yes, I did my homework, however as you guessed it the motor rotation was wobbly from 0 until it reached a predetermined value on either direction ( I did not write out the crossover value from zero). Also my motor speed was set in increments of 5 rather than 10, so the extra coding needed was left out, and the negative speed value was multiplied by( -1) instead of using "abs", since I've just come to learn that term from watching this video.
    Once again thank you Paul !!!

  • @macrony5034
    @macrony5034 3 года назад +1

    This was honestly the hardest thinking I had to do and I still got it wrong. Thanks for clearing this up for me Paul!

  • @johntello8904
    @johntello8904 3 года назад

    I managed to complete the homework completely by myself! The only difference I had with the way you did it is that I didn't tune the code so that it would omit the low motor speed values where the motor wouldn't move. Also, I noticed you used the absolute value command when using the analogwrite for negative motor speed values. I simply tacked on a negative sign on the motorspeed for my analog write and that did the trick. In hindsight, I probably didn't need to include analogwrites for each if statement, so in the future I will make sure to be more concise and efficient with my commands. Besides that, my code was able to handle the main tasks.
    Here is the code is used in my void loop:
    void loop() {
    // put your main code here, to run repeatedly:
    button1val = digitalRead(button1pin);
    button2val = digitalRead(button2pin);
    Serial.print("button1val = ");
    Serial.print(button1val);
    Serial.print(" , button2val = ");
    Serial.print(button2val);
    Serial.print(" mSpeed = ");
    Serial.println(mSpeed);
    delay(100);
    if(mSpeed>0){
    digitalWrite(directionPin1,HIGH);
    digitalWrite(directionPin2,LOW);
    analogWrite(speedPin,mSpeed);
    }
    if(mSpeed255){
    mSpeed=255;
    analogWrite(speedPin,mSpeed);
    }
    }
    if(button2val==0) {
    mSpeed=mSpeed-5;
    analogWrite(speedPin,-mSpeed);
    if(mSpeed

  • @berryblades
    @berryblades 2 года назад

    The abs function is a game changer! I made mine work with the minimum motor speed of 100 and a new value of switchDir which activated if you pressed the dec speed button at a motor speed of 100. Once this happened switchDir would change value from 0 to 1 and in doing so reversed the direction and inversed the switches. however my program had no "off" setting, the motor didn't really run at 100 so I make as an excuse but I still feel I would have lost marks for that.
    Thanks again Paul for a great lesson and another tool for the arsenal!

  • @RajatSingh-qj2qq
    @RajatSingh-qj2qq 4 года назад

    Was able to make the project work flawlessly. After watching 40 videos, I have understood how to approach the problem. I also incorporated 2 LEDs for +max and -max speed to signal the user to stop pushing the button. Also, my motors start to run at 95 so the number of if statements reduced and thus the complexity. Thanks for all the lessons, Paul! Appreciate it.
    The grade would be A or A+ if you are generous enough...haha!

  • @TheKverbeeck
    @TheKverbeeck 4 года назад +1

    I did is as follows, also had the delay time configured under the Serial Prints(fixed that) and used analogWrite (speedPin, (mSpeed * -1)); instead of analogWrite (speedPin, abs(mSpeed));
    int speedPin = 5; // EN1 on L293D chip
    int dir1 = 4; // IN1 on L293D chip
    int dir2 = 3; // IN2 on L293D chip
    int mSpeed = 0; // This is the desired motor speed (PWM), value between 0 and 255, motor starts spinning at around 75
    int upButtonPin = 8; // Controls the speed in the upward direction
    int downButtonPin = 9; // Controls the speed in the downward direction
    int upVal; // Status of Up-Button
    int downVal; // Status of Down-Button
    int dt = 250;
    String msg = "Down-button state is ";
    String msg2 = " Up-Button state is: ";
    String msg3 = " Motor Speed is: ";
    void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    pinMode(speedPin, OUTPUT);
    pinMode(dir1, OUTPUT);
    pinMode(dir2, OUTPUT);
    pinMode(upButtonPin, INPUT);
    pinMode(downButtonPin, INPUT);
    digitalWrite(upButtonPin, HIGH); // Pull-UP resistor in Arduino
    digitalWrite(downButtonPin, HIGH); // Pull-UP resistor in Arduino
    }
    void loop() {
    // put your main code here, to run repeatedly:
    upVal = digitalRead(upButtonPin);
    downVal = digitalRead(downButtonPin);
    Serial.print(msg + downVal);
    Serial.print(msg2 + upVal);
    Serial.println(msg3 + mSpeed);
    if (upVal == 0 && downVal == 1) { // When pressing the Up-Button
    mSpeed += 10;
    delay(dt); // Delay time, only when pressing the button
    if (mSpeed == 10) {
    mSpeed = 100;
    }
    if (mSpeed == -90 || mSpeed == -95) {
    // Because you are going Upwards with this button
    // 90 if mspeed did not reach 255
    // 95 if mspeed did reach 255
    mSpeed = 0;
    }
    if (mSpeed > 255) {
    mSpeed = 255;
    }
    }
    if (upVal == 1 && downVal == 0) { // When pressing the Down-Button
    mSpeed -= 10;
    delay(dt); // Delay time, only when pressing the button
    if (mSpeed == -10) {
    mSpeed = -100;
    }
    if (mSpeed == 90 || mSpeed == 95) {
    // Because you are going Downwards with this button,
    // 90 if mspeed did not reach 255
    // 95 if mspeed did reach 255
    mSpeed = 0;
    }
    if (mSpeed < -255) {
    mSpeed = -255;
    }
    }
    if (mSpeed >= 0) {
    // Otherwise motor does not know what to do @ 0
    digitalWrite (dir1, LOW); // Direction of the DC motor
    digitalWrite (dir2, HIGH); // Direction of the DC motor
    analogWrite (speedPin, mSpeed); // Speed of the DC motor
    }
    if (mSpeed < 0) {
    digitalWrite (dir1, HIGH); // Direction of the DC motor
    digitalWrite (dir2, LOW); // Direction of the DC motor
    analogWrite (speedPin, abs(mSpeed));
    // abs = absolute value of mSpeed. Speed of the DC motor
    // analogWrite (speedPin, (mSpeed * -1)); // Speed of the DC motor
    }
    // if (mSpeed == 0) {
    // digitalWrite (speedPin, LOW); // Speed of the DC motor
    // }
    }

  • @ylevite
    @ylevite Год назад

    Got it completely right, including the jump to 100. I didn't know about the ABS so my code was a looooooot less elegant :)

  • @anantnegi6187
    @anantnegi6187 Год назад

    I did it on my own... thanks Paul for these sessions

  • @user-ys6bc7yh7z
    @user-ys6bc7yh7z 9 месяцев назад

    Assignment is completed!🎉🎉 Made a lot of unnecessary code with while, combined conditions and different variables for directions. It worked, which I am really proud of. Thank you for your teachings!
    The concept of negative value is mind-blowing 😂🎉. All the best to you and your family

  • @keithdavies3334
    @keithdavies3334 4 года назад

    I got this working but starting from 0 rather than 100. I also created a variable for the speed increment. I took care of the max and min limits in a similar way to you.
    A great tutorial. Thanks again Paul, much appreciated.

  • @myste3178
    @myste3178 4 дня назад

    I made my code by doing a bunch of if statements and multiplying by *-1. Abs would've made this a lot easier.. thanks for the video!

  • @GeoGebweiler
    @GeoGebweiler 4 года назад

    Hello Paul, this problem was really more complicated than expected. I draw a Flow-Chart to manage the different if-conditions and got a similar solution like You. Additional I put three LEDs on the board - a yellow one for zero speed and two red ones to show the maximum speed in each direction. Everything works fine. I am so happy that I found Your tutorial. Now I am up to date and I really look forward to learn all about the other interesting components and sensors in the Elegoo-Kit. Many thanks for Your wonderful and extraordinary teaching. A+ for my solution and A++ for Your work !!!

  • @k3nmast3rs
    @k3nmast3rs 4 года назад

    I really enjoy and suffer and sweat this exercise, but I managed to do it by myself.
    Paul, I've learned a lot of you. ¡Thank you so much! You are an excellent teacher.
    int buttonPinUp=8;
    int buttonValUp;
    float counter;
    int buttonPinDwn=9;
    int buttonValDwn;
    int speedPin=5; //Pin PWM
    int dir1=4;
    int dir2=3;
    void setup() {
    Serial.begin(9600);
    pinMode(buttonPinUp, INPUT);
    digitalWrite(buttonPinUp, HIGH); //Pullup
    pinMode(buttonPinDwn, INPUT);
    digitalWrite(buttonPinDwn, HIGH); //Pullup
    pinMode(speedPin,OUTPUT);
    pinMode(dir1, OUTPUT);
    pinMode(dir2, OUTPUT);
    }
    void loop() {
    buttonValUp=digitalRead(buttonPinUp);
    buttonValDwn=digitalRead(buttonPinDwn);
    Serial.println(counter);
    delay(200);
    //Direction
    if(counter>0 && counter255) { //Max (+) counter Val
    counter=255;
    }
    }
    if(counter>0 && counter

  • @yovavgur-ari9580
    @yovavgur-ari9580 Год назад +1

    One of the most interesting lesson.

  • @426F6F
    @426F6F Год назад

    I made my fan have ten speeds. The initial press of the speed increase button sets the PWM to 255 to kickstart it for 50 ms, then down to 100. Each increment is 15.5 up to the total of 255. Once you pass 255, it sets the speed to zero. And if you press the decrease speed button while the speed is at zero (off) it sets the speed to 255, so you can infinitely cycle between the speeds with either the increase or decrease button!

  • @handofash
    @handofash 3 года назад +1

    I needed the first two minutes of the tutorial to put caps at the end zones, but in the end, I completed this with 7 if statements in total.

  • @bavlymaro416
    @bavlymaro416 Год назад

    I did it on my own and it took me 30min, thank you paul!

  • @Drmerlin604
    @Drmerlin604 3 года назад

    I really struggled to get this working as a homework exercise... I got it 95% right but was missing one vital piece of information that I do not remember being covered previously... the abs command! As soon as I learned this by following through, everything fell into place.

  • @aaronmooney2430
    @aaronmooney2430 3 года назад

    Completed homework on my own, but with code/math closer to the Joystick in lesson 39. I had a buttonVal that moved between 0-1023, centering at 512, then used the slope point math to calculate speed. My push buttons edited the buttonVal by factors of 64, since the total 'travel' of buttonVal was 1024.

  • @nickysoundsgood9664
    @nickysoundsgood9664 2 года назад

    Thanks Paul, after a month of learning on my spare time, I''ll be finally able to run my creations with motors

  • @geralddrake4617
    @geralddrake4617 3 года назад

    Based on lesson 39, I did get it working on my own, but there was some flaws. After watching lesson 40, I was able to correct my coding and now it works. My code is a little different, but I went about it in very similar fashion as you. At times I made it more complicated than it needed to be. Thanks for the lesson. It's great to work these out on my own. I'm learning a lot about coding which is what I wanted. Thanks big time.

  • @mattfleming4488
    @mattfleming4488 3 года назад +1

    I took a completely different approach which was way too complicated compared to yours. I tried to only stay with positive numbers, then check direction at 0 in order to go in another set of if statements for reverse motor speed. Got up to 200 lines of code for something this simple, not good. :) Thanks Paul for these great sessions, I'll keep learning and improving.

  • @Anonymous-yy5qr
    @Anonymous-yy5qr 3 года назад

    I did it on my own but my approach was a bit different... I didn't consider speed above 100... I started from 0, 17, 34, ... 255.
    Yours was a very systematic approach... Thank you very much sir❤️

  • @markmarzinzik9071
    @markmarzinzik9071 4 года назад

    Paul, I wanted to let you know on Lesson-40 I changed my code from a 20-Step Motor to a VFD Drive. I was able to achieve 310 steps by coding this way. Also I had a quite a bit of problems with power from the 9-Volt battery. As you did I found a 12-DC Transformer laying around and "It made all the difference in the world. If you would like a copy of my sketch, let me know. Thanx again for all the time you spend putting together "these excellent videos".

  • @stevesykes295
    @stevesykes295 2 года назад

    I tried and tried. My code was getting overly complicated. Thank you so much for these tutorials. I am learning so much.

  • @sanspeur68
    @sanspeur68 3 года назад

    Doing the paperwork certainly makes sense! Made everything a whole lot clearer!

  • @autocrosser10
    @autocrosser10 6 месяцев назад

    I would have gotten an F on this one. Completely underestimated the homework! I came to watch this video to see how you dealt with the negative mSpeed values but learned there was a ton more "if statements" that I didn't even consider.
    fun project either way.

  • @bearpapa1606
    @bearpapa1606 3 года назад

    I start my motor speed at 105 and make an increment of 30 every time a button is pushed, so 5 increments in total.
    I also created a "movement state" which increase by 1 every time an up button is pressed and decrease by 1 when the down button is pressed.
    when movement state is more than 0=move forward and less than 0=move backward.
    as for the speed of motor i use this code.
    analogWrite(speedPin,motspeed+(movestate*30));//add "-" before movestate on backward direction
    not perfect, but it gets the job done. thanks for the great tutorial.

  • @wonderwang1585
    @wonderwang1585 4 года назад

    I like the skill of tick start a DC motor. I had a pencil DC motor which either rotates like carzy or just still. The idea of turning motor high first and go to a slower value is very useful.

  • @Mr_Jonathan_Greer
    @Mr_Jonathan_Greer Месяц назад

    Great assignment. I ended up getting it done, but I made mine a little more difficult than it needed to be. I created a variable called mSpeedNum and replaced mSpeed with that. I then made an if statement to make mSpeed equal to mSpeedNum*-1 if mSpeedNum was negative.....your way was way simpler tojust do an analog write for each option.
    Also, I made my fan start and stop at different mSpeeds since that's how the motor works. So it would go from 0 to 130 at the first button push, then on the way back down it wouldn't go to 0 until around 100.

  • @iqbalarabian7425
    @iqbalarabian7425 3 года назад

    Yes Sir as you said it turned out be more difficult then we thought, thank you for teaching us.