This is the second part of Arduino servo motor controlling episode. In previous lesson, we discussed about how to turn servo motor to given degrees. Today we are going to learn about how to control the speed of a servo motor.
So, the circuit diagram is same as before circuit diagram.
Here is the circuit diagram.
Blow is the code.
Now you see the code above. In this code, we put for loop.
Now you are wondering what is for loop. There are loops in Arduino. We have
learnt about void loop in Arduino. But this for loop is a loop type which we
use in void loop. It makes loop. Loop means repeat again and again. For loop
makes a loop(repeat) until the condition is ok. We but for and after that the
condition in brackets. We put “ i = o” in the first part in the condition. That
means we make variable named “i” and it has a value of “0” so, int means Integers.
Integers means some numerical value. You can put any number in that “int” type
variable. But you can’t but letters like “a, b, c”. In the second part of condition,
we put “i<=180;”. It says until “i” became similar or greater than 180. That
means it stops in 180 degrees. In the last part of condition, we put code part
like “i++”. It represents add one more number to that variable. You can put “i--”
also. In “i--”, it decreases the value by 1 number. So, you have good understand
about for loop condition now. Inside the for loop, we put myservo and we said
to add “i” to it. Code structure is, in the beginning, “i” is zero and it
increase one by one degrees to 180. After “i = 180”, Servo motor reset again to
zero degrees and repeat it again. At least in the for loop, I put little bit of
delay. It makes the gap between two degrees. So can increase the delay value
and control the speed of the servo motor according to you project.
This is the code with decreasing values.
We put for loop again and it’s int variable is named as “y” you
can put any type of name to it. I recommend to you to add only English letters
to variable names because numbers can damage to code. So, this is the other side
of first for loop. First, we mentioned “y” variable’s value as “180” because that
the place that we stop in the first for loop. This for loop’s condition is “y>=0”.
It means when it similar to 0 degrees, that for loop stops. Last part of
condition the “y--” makes one number decrease from the y value. Try it yourself.
If you have any kind of question, please comment below. We
are here to reply you. You can contact us any platform you prefer from contact
us page. Good bye and see you again in another Arduino tutorial.