Hello!
I am new to programming. How can i get 2 second delay on servo after I press the button? If i press button servo moves, but how can I make it move after 2 seconds?
Thanks for everyone!
Here is existing code:
Your question and your code has the right size to introduce you as a beginner with programming-technique
Your code is executing a certain sequence of steps
Check if the button is pressed
if button is pressed calculate new servo-angle
wait 2 seconds before moving the servo
move servo to new position
if step 4 has finished go on with step 1 again
This could be seen as 4 different modes of operation
mode 1. Check if the button is pressed
mode 2. if button is pressed calculate new servo-angle
mode 3. wait 2 seconds before moving the servo
mode 4. move servo to new position
if mode 4 has finished go on with mode 1 again
in the programming-world the term "state" is established for this kind of operationmodes
So in programming terms
state 1. Check if the button is pressed
state 2. if button is pressed calculate new servo-angle
state 3. wait 2 seconds before moving the servo
state 4. move servo to new position
if state 4 has finished go on with state 1 again
this is how the code in the linked WOKWI-Simulation works
Now I'm curious what questions you have about this demo-code
Here is a variant of the code above which has an additional button for some kind of singlestep-mode
This means at certain lines of the code you have to press the yellow button to make the code proceed the next step (the next state)
This shall make visible how the code is jumping in and jumping out of function myServoStateMachine()
Hi again!
I have one question more.
If i press button servo moves and moves automatically back, but how can I make it move 2 seconds after pressing a button. Like servo started at 0* i press button it ends in 90* and comes back to 0*. How can I make it move 2 seconds after pressing button?