Arduino Basics - Lesson 12 - Using buttons to control a Servo motor

Поделиться
HTML-код
  • Опубликовано: 4 окт 2024

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

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

    This is exactly what I needed to control the cueing and auto lift for my new turntable build. Thank you.

  • @grantscott1686
    @grantscott1686 3 года назад +3

    This video is incredible! I was wanting to learn how to do exactly this for months now and this video helped me out so much, thank you!

  • @Dilpreet_Singh5911
    @Dilpreet_Singh5911 3 года назад +3

    Thanks sir I really appreciate that clear explanation.Your channel should get more attention!!!!!!

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

      I appreciate that! Hopefully someday it will :)

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

    This explanation was perfect. I was even able to modify the loop function to incorporate 3 buttons, and also create a loop that moves the Servo to a specified angle, thanks for your help! Here's the code I wrote under the loop function:
    void loop() {
    int button1State = digitalRead(button1);
    int button2State = digitalRead(button2);
    int button3State = digitalRead(button3);
    if(button1State == HIGH && servoPosition = 0)
    {
    myservo.write(servoPosition);
    servoPosition = 0;
    delay(100);
    }

    else if(button3State == HIGH && servoPosition >=0)
    {
    myservo.write(servoPosition);
    servoPosition = 180;
    delay(100);
    }

    }
    The only problem I have with this is that regardless of which button you press first, you have to press the second button that sets it back to angle '0' right after, otherwise the Arduino won't move the servo anywhere. Will have to play around with the if and else if statements a little more, none the less, great lesson!

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

      Nicely done! With your code the way it is. Once you press button3 you’re stuck at 180 until you press button1. As the other two buttons have if statement conditions that can’t run at a position of 180.
      I hope that helps a bit. Keep it up :)

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

      You just saved my semester with this code, literally. Thank you so much

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

    Thanks for the tutorial. I subscribed

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

    Amazing tutorial!!! when I want to make a project I search on youtube do the wiring and code it, but sometimes some videos have a link to copy the code, but I love this one that doesn't need to copy paste a code, and its easy what mistake I made and it was a short coding so it's simple for me! (but sometimes when the video has a link in the desc for me it means that the code is too long). But amazing simple project for beginners

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

    Very good tutorial, instant subbed.

  • @timberhoff
    @timberhoff 10 месяцев назад

    deserves a subscription!!

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

    Nice tutorial Keep up!

  • @umitaktan923
    @umitaktan923 5 дней назад

    THANKS

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

    Thank You!

  • @joescopo8933
    @joescopo8933 21 день назад

    Can you do pinMode INPUT_PULLUP for the buttons instead of using resistors?

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

    Why do we need resitors? And do those kind of buttons always need voltage, ground and a pin? Even if I use it on a Ada fruit soundboard for example?

  • @MarcSundermeyer
    @MarcSundermeyer 2 месяца назад

    I am trying to find a configuration and code that only uses one button. On press it rotates, but on release it returns to the home position.

  • @dragoOgnjenovic
    @dragoOgnjenovic 2 месяца назад

    Your teaching is great, but I have 0 skills (too old) for writing a code. I can develop the application and hardware, but not write the code. I really wish I can!
    I am wondering if you can help me with very short Arduino code for Servo motor?

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

    Hello, I would like to build a similar circuit for a model car (mechanical transmission). It should be operated with 2 buttons (up/down). Each time you press the button, the servo should adjust by 10 degrees (or another defined number of degrees).
    Neutral = 0 position
    1st gear = one down
    Neutral = on up
    2nd gear = one up
    3rd gear = one up
    4th gear = one up
    The entire transmission should then be switched up (UP) and down (down) using 2 buttons.
    It would be great if the “UP” button also had the function if you switched it to its 0 position (neutral) for 3 seconds.
    I would be very grateful if you could help me with this.
    Thank you.
    Greetings Hans

  • @samarsamir1489
    @samarsamir1489 13 часов назад

    mines did not work i did the same coding same curcuit please help

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

    OK, i HAVE BEEN ABLE TO DO THIS, HOWEVER, WHAT IF i WANT TO HOLD THE SERVO, IN EACH POSITION? SAY, i PUSH THE LEFT BUTTON, AND, THE SERVO MOVES TO 60 DEGREES, STOPS, AND HOLDS POSITION, UNTIL COMMANDED TO MOVE, BY THE RIGHT BUTTON? SAME THING WITH THE RIGHT BUTTON. I WANT IT TO MOVE TO 160 DEGREES POSITION, STOP AND HOLD THERE, UNTIL COMMANDED TO MOVE.
    HOW WOULD YOU CODE THAT? THANKS!!!

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

    omg thank u very much

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

    Really loved your video. Great teaching style. I understand the coding. Have typed in code, checked and checked, but when I compile, after : else if(button2State == HIGH) && servoPosition >=0 //else if we are over 0 button 2 is on" get "expected ";" before "{" token. Can't see the error!. Frustrating

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

      Can you paste the whole if/else if blocks of code here or email them to me codewithconner@gmail.com

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

      @@CodeWithConner here it is
      if(buttonState == HIGH) && servoPosition =0 //else if we are over 0 button 2 is on
      {
      Servo1.write(servoPosition); //write current position
      servoPosition--; //subtract 1 to position
      delay(5); //delay 5 ms
      }

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

    Hey is it possible to duplicate the code in order to control multiple servos with multiple buttons?

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

    tried to compile sketch and this is coming up Compilation error: 'Servo1write' was not declared in this scope
    i have tried fixing it with no luck . help please.

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

    is it possible to make servo move in loop fron 0 to 180 and when i click button the servo stops at its current position? or not possible without position feedback? thank you

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

    Can you please tell me the code for when i pressed the two buttons together then the servo goes to 0 ??? i loved the tutorial from your video...keep up the good work!!!

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

      Email codewithconner@gmail.com and I can send you solution code.

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

    Code didn't work for me.
    #include
    int buttonPin = 4;
    int button2Pin = 7;
    int servoPin = 3;
    Servo Servo1;
    int servoPosition = 0;
    void setup()
    {
    // put your setup code here, to run once:
    Servo1.attach(servopin);
    Servo1.write(servoPosition);
    }
    void loop() {
    // put your main code here, to run repeatedly:
    int buttonState = digitalRead(buttonPin);
    int button2State = digitalRead(button2Pin);
    Error massage says ServoPin was not declared in this scope

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

      When you declared servoPin you used a capital P but when you access it later you used a lower case p. That’s why it can’t find it.

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

    hello sir Conner... i have followed the code you have written and i modified some of it for the application of my project.. I've built a pingpong robot and i planned to use a servo motor on the panning. i want to control the servo with 6 buttons each have different angles to perform on the servo. The first time i uploaded the code to my arduino it worked very well, but later i have encountered a problem.. when i turned on the arduino the program will automatically run, executing the program on either of the buttons. please note that the buttons are not yet pressed.. please help me sir...

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

      Shoot me an email to codewithconner@gmail.com with your code and I can have a look for you. No promises but I'll try and help.

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

    I have an issue with my circuit and im hoping you could help. I have two servos connected to a button and i want the servos to start at zero to move to 45 degrees when button is pushed (not held), and when the button is pressed again, servos move back to 0. where in my code is it incorrect ?
    #include
    int buttonPin =2;
    int servoPin =3;
    int servo2Pin =5;
    Servo servo1;
    Servo servo2;
    int servoPosition =0;
    int servoIncrement =4.5;
    void setup() {
    servo1.attach(servoPin);
    servo2.attach(servo2Pin);
    servo1.write(servoPosition);
    servo2.write(servoPosition);
    }
    void loop() {
    int buttonState =digitalRead(buttonPin);
    if (buttonState==HIGH&&servoPosition=0)
    {servoPosition-=servoIncrement;
    servo1.write(0);
    servo2.write(0);
    delay(10);
    }
    }

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

    Hello sir , my question is .. when servo move to 1°-180° and i want it delay for 5s .. after that automove back to 180°-1° .. each button has a same instruction but the different is value of angle movement servo.. so how the code sir? Please reply n help me to solve this problem. Tq

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

      If the only difference between the button is the speed of the movement, basically the first if statement would do a full sweep from 0 - 180, delay 5, then sweep back to 0. The "sweep" would be a for loop that changes the servo angle and writes it however many times you want. The finer/smaller the angle adjustment each time the slower/smoother the movement will be.

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

    How to write many angles settings for each buttons? Plss help

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

    hey hey, I used your code and it works perfect! but I want my servo to turn slower. How do you have to change this code so that the servo will turn slower, because it is going quite fast now?

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

      The easiest way would be to just increase the length of the delay inside the if/else statement in your loop function where you increment/decrement the servoposition variable.

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

    Conor , I would like to use a PCA9865 , have you any videos on how this would work rather than a direct feed form ardiuno board ...i want to move the servo to preset positions depending on a feed from a toggle switch ..

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

      So I haven't done a project video with a servo breakout board like this, but they are awesome. This site has a ton of good info on setting one up though: learn.adafruit.com/16-channel-pwm-servo-driver?view=all

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

    hey ive got a question... Great video btw:
    I want to control my servo so when i press a buttondown it rotates 180*. But then when i stop pressing it, it goes back to 0*.
    i tried altering your code but i cant figure it out

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

      Shoot me an email: codewithconner@gmail.com with your code and I'll see if I can help

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

    How would the code be different if I wanted to press a button and the servo rotates so that I don't have to keep holding the button?

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

      Basically, in the code below, where we increment servoposition by 1, you would instead send it all the way to position 180. Or if you want it to sweep the other way in the else statement, send it to 0. We were iterating by sending it one degree at a time. Be sure to increase the delay if you do this otherwise you'll get a LOT of HIGH reads on the button before you actually let go of it.
      if(buttonState == HIGH && servoPosition

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

    can i use this without a resistor

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

      You COULD but you'll likely get erratic button behavior. Read this for a bit more insight: create.arduino.cc/projecthub/muhammad-aqib/arduino-button-tutorial-using-arduino-digitalread-function-08adb5

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

    So, what if I want to use 3 buttons with 0, 45, 90 degrees each button, sorry if my question seems lazy to try, just curious and currently I don't have an arduino

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

      This would be pretty straight forward. When a buttons read value is high, set the servo to the desired angle. Repeat for multiple buttons.

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

      @@CodeWithConner thanks for the answer man, appreciate it 🔥

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

    EVEN BETTER... HAVE A SPDT SWITCH, AND USE THE SWITCH TO DETERMINE THE DIRECTION OF THE SERVO, AND A PUSH BUTTON, TO TELL IT TO MOVE TO THAT POSITION, WHEN COMMANDED. HOW WOULD YOU DO THAT? i HAVE TRIED SEVERAL WAYS, AND CANNOT GET IT TO STAY IN EITHER POSITION. IT JUST CYCLES BACK AND FORTH, NEVER STOPS... I AM FRUSTRATED. I LEARN BETTER WHEN SHOWN HOW TO DO SOMETHING, AND THEN I CAN APPLY IT, TO OTHER SITUATIONS THAT I AM WORKING WITH AT A LATER DATE.
    THANKS AGAIN!!!

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

      So really the idea here is that whatever is in your loop() function directly will just keep happening over and over again. The key to all of this is the if statements in the loop function. There are a couple reasons this can get messed up, for example depending on how you are detecting your button state. Even though we might think we aren't holding a button down, the Arduino will recognize it several thousand times in a second potentially. Having a delay built in anytime it detects a button push helps prevent it re-running the code more than once per button press. But, if you have no motor movement in your main loop() function, but an if statement for a button that just moves it a certain amount in a certain direction and doesn't do any kind of looping or incremental change this should be doable.
      something like: if(buttonstate==HIGH){Servo1.write(120); delay(1000);}
      would just move the motor to 120 and do nothing else.

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

    Hello guys , i have a great problem and i Hope you would help me please. I want to pilot a servo moteur with a signal input 5 volt like your button system ( in réality i want to pilote the servo with a signal of a VHF REMOTE CONTROL whose work on 12 volt and reduct to 5 volt with a convert to control in finely an excavator machine....when i push on a button of the VHF REMOTE control , i want it's do the same like your button program.... Which modification on your programme i 'll have to do for it's work . Thanks you for your compréhension , and i live in France ...maybe i'm lucky if you speak french .thanks you .

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

      I'm not sure about the VHF remote input source, but in theory as long as you can determine when the button you want is being pressed on the remote, you can then put that into an if statement and within the statement, set the angle change for the servo motor. Essentially replacing the buttonread == HIGH from the button version with whatever input check there is for your remote.

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

    What software do you use for your wiring diagrams?

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

      Most of them I do with tinkercad circuits. But some I had to use fritzing.

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

    i eally don't know where is the mistakemaking thnk why wouldn't you JUST POST THE CODE!!!
    #include
    int buttonPin = 4;
    int button2Pin = 7;
    int servoPin = 3
    Servo ;
    int ServoPosition = 0;
    void setup()
    {
    Servo1.attach(servoPin);
    Servo1.write (servoPosition);
    Serial. begin (9600);
    }
    void loop()
    {
    int burtonState = digitalRead(buttonPin);
    int button2State = digitalRead(button2Pin);

    if(buttonState == HIGH && servoPosition

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

      Feel free to share if you want. I don’t post the code because I think there is better learning in following along and in the act of typing out the code and the brackets and all of it yourself.
      To each their own.

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

    do you have a copy of the code to post for this?

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

      Send me an email. codewithconner@gmail.com and I'll send it to you.

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

      hey can you send me the code :)

  • @notveryfrenchtoast
    @notveryfrenchtoast 28 дней назад

    doesnt work

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

    The word (Servo1) didn't activate durring writing the code broo !!