360 servo and button

Hello I got a little stuck with the code.
I want that when the button is run, the servo rotates (5s) and inserts and waits for you to release the button and then rotates to the other side (5s) and waits for you to press the button again.

My problem is that it doesn't wait but it rotates all the time the direction is correct depending on the condition of the button.

Thans for your time.

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Your topic was MOVED to its current forum category as it is more suitable than the original

Once you've tested the return value of a digitalRead to see if it is HIGH, and it isn't, you don't need to test it again to see if it is LOW; a simple "else" will suffice.

All these will help:

Using millis for timing
Demonstration for several things at the same time
Finite state machine tutorial

I think the problem is that you wrote:

If the button reads HIGH, forward for five seconds.
If the button reads LOW, backward for five seconds.

Since the button always reads either HIGH or LOW you never stop moving. In your description, you say that after the five seconds you want to wait until the button is no longer HIGH or no longer LOW.

In the "if (buttonState == HIGH)" part you can add this line to wait until the button is released:
while (digitalRead(buttonPin) == HIGH){}
And in the "if (buttonState == LOW)" part you can add this line to wait until the button is presed:
while (digitalRead(buttonPin) == LOW){}
Put those lines after the servo.writeMicroseconds(1500); lines.

it is working!!!!

Thank you so much.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.