My Name is Ahmad Shamshiri and I am presenting this tutorial from Canada.
Hi, welcome to another Arduino tutorial from Robojax. in this video I'm going
to show you how we can control the servo motors using these two buttons of left
and right. when you press the right it goes with 10 degrees to the right and it
presses left it goes to the left or just if you want to use just one
button you hold it it goes to the left and right and until and until you stop it.
stops between 180 you can get the Arduino code for this project by clicking at the link
of the description which will take you to www.robojax.com/learn/arduino
let's get started with this
Wiring explained
now let me explain the wiring. here this is just a shield and I use this have a
breadboard nothing else all these pins are exactly the same because this makes
it very easy to use now let me explain the wiring here this is just a shield
and I use this have a breadboard nothing else all these pins are exactly the same
because this makes it very easy to use so all the pin that I'm referring to are
at the top who has three wires here usually the dark wire is the ground the
middle one is always the positive 5 volts and this the last one which is
lighter here it's orange this is a signal that controls the servo the
middle one is 5 volt it's connected here so and it's connected to the ground at
this point and then from ground because I need multiple grounds I connected
buttons from ground this button has 2 pins and I'm using it you can use for pins
doesn't matter so I've inserted this here and this this is a RIGHT button
connected to pin number 2 here and then the same thing ground is here and this
side is connected to pin 12 so 2 and 12 this is a RIGHT and that's the LEFT this
will work the same thing on the other one but I have to put a lot of wire on a
separate breadboard so this makes it very easy and these two sides are
connected here and this is connected to the ground
Code for single push button explained
let me explain the code for the first one that just you press a button and it
goes to the left and right so the first line here is including the servo.h
file this is part of Arduino once you solar do you know it it's already
included you don't have to include anything we are initializing an object
for the servo and then we are setting that angle they start initial angle at
zero and then angle step this is a step how fast you want when you press a
button it should go but now it's at 10 (degrees) when you press the button it goes 10 20
30 40 and so forth until up to 180 degree and this is a setup() inside the setup we are
initializing the serial monitor with 9600 baud and that is this value and it
must match otherwise you will not be able to see the data and then we use
this object with a method attached 9 servo is connected to pen 9
which I showed you so pin 9 will be controlled by the servo and then here
we use pinMode number 2 as a pull-up this is when whenever you use a
push button if you don't want to use a resistor this is a best way internal
resistor is used. you set this as an input and also you say PULLUP when you
say a pull up, it uses 20 kilo ohm resistor and it will be connected to the
positive from that point. from pin 2 so this is makes your life easy you so just
makes a easy not to use extra resistor. and this is just a text this
text that is printed inside the loop from here to here we just check to see
if reading ping number 2 the push button is connected to pin number 2 and we say if
the button is low remember I should told you that it's always HIGH when it's not
pressed when you press it it goes to LOW so we check using digitalread read
number 2 while this is equal LOW what this will mean as long as you are pressing or
the button is as long it's true then do all of this up
to here and we are just incrementing adding the step which is 10 plus
whatever was here let's say this was 0 10 plus 0 is 10 now this will be 10 here
we check it if angle is less than 0 because we are not allowed to have less
than 0 and we are not allowed to have over 180
it says if this angle is less than 0 or this 2 pipe or vertical line mean OR all
it's bigger than that then it says subtract so now this is subtracting
that's why when you keep the button pressed it goes either left or right
because when the value goes beyond 180 it goes to the other direction and then
whatever the angle we set it goes to the servo and move servo to that
location as long as your hand is pressed and then we wait 50 millisecond and the
loop continues like that
Demonstration:Single push button switch
this is now demonstration of the code so when I press this with a single push button
when I press it on here it print the angles servo moves at any moment it
tells you if you want to be able to see it make it a little slower 500
millisecond so this time it goes 10 but I'm holding it as long as you hold it so
this is now 90 and it goes to this zero and that's 180
Code explained
This is the code for the dual button here you're using the salvo header which is
part of the library as before I explained it and this we are creating an
object we are setting initial angle 90 degrees is if you are using a forest
steering for other purpose at the middle but you can change it for whatever value
you want and a step is five degrees you can increase or decrease that if you
want higher resolution make it one degree so when you press it it goes
every time with one or if you want to increase it five ten or twenty whatever
angle you want and here we have defined left pin 12
and then for the right pin 2. so we are connecting the button one of the wires
of the button here to the twelve and then two so the one that is connected to
pin 12 is the left button and for the right button is number two and then from
here to here the setup 9600 bodies for the serial monitor when we are attaching
pin number nine which is connected to the silver and then here we said left
input pull-up so this eliminates the use of extra resistor so the button when
it's not pressed always its pull up to the positive which you mean it's always
HIGH and the right is also always HIGH with pull-up and then we write the angle
we are setting the initial angle that you put because this is one time the set
up is running only one time so we move the servo to that angle an
print this text. then here the loop is the important part that does most of the
job. we have a while loop from here to here and this one when you press
it to the right it moves the servo to the right regardless of wherever it is
so we check using digitalRead(Right this RIGHT means pin 2 if it's equal low
remember the pull-up resistor is keeping it HIGH when you press it it goes to LOW so
this two equal sign means compare one equal sign mean assign so here we check
it and we see a anger is bigger than zero if it's less
than 180 then we say get the angle whatever it is subtract the angle
because we we want the angle to be within that range so we are moving it to
the right and then if the angle is less than zero because there's sometimes
subtracted that becomes smaller so we fix it here and then if it's not less
than zero then here we say write the angle whatever is the angle they say if
the angle was 180 and we are subtracting it it become 170 and then we print this
on the screen did this value and then we wait hundred millisecond that's the
while loop this continues as long as you're pressing the right button now for
the left button same thing if it's LOW so the difference is that we are
checking if it's big bigger than 0 or 180 and here is a plus sign so we are
constantly incrementing it because the left is here
because the angle 180 is on the left side so we are incrementing it and here
we are checking it if it's over 180 we are fixing it here and then other than
that we are just putting the angle whatever angle is just the result of
this we will move the servo to that location and loop continuous as
long as you press the button but when you press the button it will not go
above 180 because this is fixing it
Demonstration
there's a demonstration of this load the code you see the servo will move to the
middle now I'm demonstrating it so by pressing the right button so the
increment the increment is 5 so when I press it it goes to the right until
reaches zero after zero when you press it nothing happens and then so this is
zero and 180 is there that step that I put with a black tape so now it's I 180
and at any moment you can move it and it also shows you the exact location of the
servo. I've used I've used 100 millisecond here but
if you want it slower just increase this value because that's the time and if you
want it faster make it smaller thank you for watching so this was an
example of how you can control a servo with the left and right with two buttons
please subscribe so you can get updates of my upcoming videos with you if you
have any question or comments please post it at the comment section below
I'll try to answer and reply take care
Typed and corrected on Mar 08, 2020 by Ahmad Shamshiri.
The least you can do for me is to not skip the ads and keep watching it and thumb up👍 the video
If you haven't subscribed yet, please click on Subscribe button now. I appreciate it.