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.
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.
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.