Controlling continuous servo with button

Hello everyone, im hoping to get answer here about my task.

Im trying to make a continuous servo to rotate a few degree like 30 or so, and i know that the continuous servo rotate according to the delay. and im trying to control it using a button, when i push it it turn for some degree, and when pushing the button again it turn back to previous degree

i've try one with one condition to rotate to some degree but it only rotates continuously when i press it and stop when im not press it

#include <Servo.h> 
 const int servoPin = 2;  // Servo pin
 const int buttonPin = 8;  // Pushbutton pin
 int buttonState = 0; 
 int directionState = 0; 
 Servo myservo; 
 int pos = 0;
 
void setup() {
   myservo.attach(2);
   pinMode(buttonPin, INPUT);
 }
 
 void loop(){
   buttonState = digitalRead(buttonPin);
   if(buttonState == HIGH){
    myservo.write(0);
    delay(10);
    myservo.write(90);
   }
 }

im currently learning on arduino so my coding probably still a mess. thanks for your time to answer my problem

You are telling the motor to rotate for 10 milliseconds each time it sees the button is down.

Add State Change Detection if you want it to move one 'burst' each time the button is pressed. See: File -> Examples -> 02.Digital -> StateChangeDetection

Increase the delay(10) if you want it to move further on each button press.

Use a regular servo if you want it to a specific position.

oops sorry sir, what i meant is it rotates continuously when pressed, and stopped when im not pressing it, let me change it

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