in some applications you need to control a servo motor based on a signal the signal could be a
status of some value change or status of a push button so this is a push button that I’m using to
press and move the servo and as long as it is pushed
the servo will go within the boundary of 0 to 180 degrees and so this has a lot of application
depending on the type of application that you use this code will be very useful
here is the wiring for the servo with the push button the same as before the yellow
have been connected to nine the middle pin is connected to the five volts the red one and then
the darker color is connected to the ground that was for the servo for the push button
on one side have been connected to the ground and the other side have been connected to pin two
here for the servo with the orange I put yellow I’ve been connected to pin nine that's
a pulse width modulation pin the middle pin is vcc via red have been connected to 5 volts
I’ve used another brand connected to the ground here is my push button my push button has two
as before I mentioned if you have if you have this type of push button you can use it but
make sure that once you connect it this way if it doesn't work rotate it 90 degrees should work
so I have two wires here for the push button
one wire is connected to pin two the other is connected it's connected to ground
now let me explain the code before we have to include the servo.h which I have explained it in a
previous video and then we use the class server we create an instance of it when we call it my servo
this line defines pin 9 as a servo pin and this line defines push button pin at pin 2.
the angle is the initial angle that when you start it the servo will go to value is the
step of angle when you push the button it jumps continuously or every time you push
it it goes 10 if you want it five or lower or maybe higher just change it accordingly
inside the setup we defined the serial monitor with 9600 bot and then we use the servo.attach
servoPin this is this has been defined here and we def we attach the servoPin 9 to the servo and
then we also define the push button pin we are using input pull up to avoid using extra resistor
and then we print this text on the screen this portion of the code will run only once
inside the loop this portion will run continuously the first line it says while as I’ve explained it
is will run as long as this is true which means as long as the result of digital read push button
this will read the push button pin 2 the result and we'll compare it with low as long as this
is true as long as this is low or as long as the button is pushed then this code will be executed
up to here you see this square that's end of while now let's see what happens first we get
the angle this is 90 degrees that angle and then plus the angle step 90 plus 10 we make it 900
and then the job of this portion is as explained before to keep the servo between 0 and 180
and then we use my servo dot right angle the angle that we have we will write it here
and because this code runs only when the button is pushed then we have to also display
what angle the servo is moving to so this will move and also the same angle will be
printed on the serial monitor at the end also we put degree so the user knows its degree
and then we wait hundred millisecond and the wire continues inside the loop this loop will
not be executed until you release a button when the button is released this will be false
and while loop will exit and it will come back and continuously will check the push button
as soon as you push it it will react and this code will be executed whatever the while loop
here is a demonstration of controlling a servo motor with a push button
the serial monitor is also open the code is running
when I press the button it shows the angle here
and as long as I press it it goes between 0 to 180 and returns back
it reaches to 180 and it goes back every time I press it it jumps to the step if I hold it
it's just continuously going between 0 and 180
and if I release it at any moment it shows the angle that it was released at
now this is around 20 and it goes 180 on the other side
now let's set this to 5 degrees
then it means that the resolution have increased which means every step that
this was before taking a 10 10 degrees now it will go by 5 degrees that could have been it
now as you can see everything I push it it jumps and this jump also refers to that step of five
now it jumps every time with 5 degrees