in some applications you need to control a servo either with a push button or with
arrival of some kind of signal or detection of voltage temperature pressure any other value or
parameter as long as you can detect it and get the value based on that value you can send this
server to the left or right or stop it at any angle that you wish I will show you how you
can control a servo using two buttons one is left this left button will move the servo to the left
and until it reaches 180 degrees and this is for the right when you press it,
it moves the servo to the right of this lift it goes to the left until it reaches 180 degrees
and you press the right and if you hold it it will reach to zero and stop
here is the wiring for the servo with two push button the wiring of the servo has not changed
it's the same as a single push button on this side we have two two push buttons
the left push button this pin have been connected to the ground and then we used another jumper to
connect the second push button here to connect it to the ground in here and the right side of this
push button have been connected to pin two and the right side of the last one have been connected
to pin three the wiring for the servo is the same for the push button here I have used this
this wire connected to the ground and then from the same ground I have put this
red wire to this point where the left pin of push button is connected so this
push button is connected to the ground via this red and black and this is also so both
of these have been connected to the ground and the right pin of this one is connected
to pin pen two and the right wire of the this push button have been connected to pin three
let me explain the code now we include the servo as usual for every other servo video
that I shown you this library must be included otherwise it will not work we create an instance
of the class servo and then we define self open at pin 9 where the server is connected this pin
is one of the pulse width modulation pin and the angle is the initial angle 90 degrees angle step
is 5 degrees so 10 or 20 whatever you like and we are defining left and right these are the
pin where the push button will be connected left pin will be connected to pin two and right pin
will be connected to pin three inside the setup this code will run once we initialize the serial
monitor we attach the servo pin using the servo dot attached to the servo and then
for the left and right we use input pull up using the pin mode this eliminates using of
extra resistor which I have explained in the previous videos and then we set the angle this
sends the servo to the initial angle and after that this is just a text for on the screen
using while we check the right so we are running two while loop one while loop for left and here
and one while loop for the right we check if the pin is low when you press a button
the status of the pin will be low so this pin will be if it is low then we know that the left button
the right button have been pressed and using this method we keep the angle between 0 to 180
and also we check if the angle is smaller than zero we set
angle to zero this portion is if the servo angle is equal to zero it will not do anything
other than that we print this we first we move the servo to the angle that have been calculated in
here and after that we print the text we print the angle and we print the text degrees this is and
this portion will be executed as long as the right button is pressed now we
wait hundred millisecond and the loop continues if the left button is pressed
we do exactly the same except here you see this is bigger than zero and also this angle
sets it at 180 because when you push the button to the left it must go up to 180 and here when you
press the button continuously and it reaches to the final it must go to the zero in this
case we are just sending the servo to the right angle and printing this text including the angle
and the while loop continues and then if there is no button is pushed then the main loop
will take over and the loop runs up until again some push button is pushed
and here is a demonstration of controlling the servo with two push button with Arduino
and you will see the angle that the servo is moved will be displayed in the serial monitor
this is a left push button when I press it that side is my reference and it moves to the left
as you can see the angle is displayed here until it reaches 180 180 the last angle is here and
now if I press this
if I hold it it goes and stop it doesn't move
if I wanted with this every time as you can see it goes five degrees
you can change the angle from the code but if you hold it it just goes until it reaches 180 and stop
this goes to the left the left button and that's the right it goes to the right
so you can send signal where other means let's say motion sensor detects something
and when a signal goes high you can control this to move
right and the other sensor detects something else and you can send this to the left