MOVING A SERVO INTO THREE POSITIONS USING ONE BUTTON

Hi There, I’m very new to this so please try and be understanding.

I am simply trying to get a servo to move in to cycle through three different positions using one button.

My servo never moves past the first step in the code, I sure this is very easy for some of you to solve but I just can’t seem to see what the problem is.

Any help would be greatly appreciated.

 #include <Servo.h> 

 
 const int servoPin = 8;  
 const int buttonPin = 9;  
 int buttonState = 0;  
 int directionState = 0;  

 Servo myservo;  

 int pos = 0;  


 void setup() {
   myservo.attach(8);  
   pinMode(buttonPin, INPUT);  
 }

 void loop(){
  
   buttonState = digitalRead(buttonPin);

   if (directionState == 0){
   
    if (buttonState == HIGH) {
       directionState = 1;
        myservo.write(90);  
         delay(15); 
       }
      
  if (directionState == 1){
    
    if (buttonState == HIGH) {
       directionState = 2;      
        myservo.write(180);  
         delay(15);  
       }    
          

   } else if (directionState == 2) {
   
    if (buttonState == HIGH) {
      directionState = 0;    
      {
        myservo.write(0);  
         delay(15); 
      }
     }
   }}}

Let me guess, you press the button once and its going through all three positions.

You may want to keep track of the last button state, and compare it to the current button state. If the current button state is high and the current button state does NOT equal the last button state, directionState++. (directionState > 2, reset back to zero)

if (buttonState == HIGH) { directionState = 0; { myservo.write(0); delay(15); ** }** ** }** ** }}}** //this is unacceptable, use the format tool (Ctrl + T)

The block of code that follows if (directionState == 0){ currently encloses the code to test for the other values.

You need a sequence of if/else if statements, - or change the structure to use a switch statement.

if (directionState == 0)
{
}
else if (directionState == 1)
{
}
else if (directionState == 2)
{
}
else
{
}
switch(directionState)
{
    case 0:
        break;
    case 1:
        break;
    case 2:
        break;
    default:
}
#include <Servo.h> 


const int servoPin = 8;  
const int buttonPin = 9;  
int buttonState = 0;  
int directionState = 0;  

Servo myservo;  

int pos = 0;  


void setup() {
  myservo.attach(8);  
  pinMode(buttonPin, INPUT);  
}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (directionState == 0) {
    if (buttonState) {
      directionState = 1;
      myservo.write(90);  
      delay(15); 
    }
  }
  else if (directionState == 1) {
    if (buttonState) {
      directionState = 2;      
      myservo.write(180);  
      delay(15);  
    }
  }
  else if (directionState == 2) {
    if (buttonState) {
      directionState = 0;    
      myservo.write(0);  
      delay(15); 
    }
  }
}

Thank you all for your help, this has helped solve my problem.

ps. I will use auto format next time.

Thanks again!