I need an if statement to execute only once?

I am trying to create an if statement which checks the state of an LED pin.

If the LED’s state is HIGH, the servo rotates to 2000 (using the “writeMicroseconds()” function) clockwise.

If the LED’s state is LOW, the servo rotates to 1000 anticlockwise.

However, in either case, I only want the servo to rotate once. Yet since the code is in the “void loop ()” section, it repeats itself (hence the name I guess) and the servo keeps getting another command to rotate. I thought I could have gotten around it using a do-while statement but that would just loop around again :relaxed:

Here’s my attempt:

#include <Servo.h>
Servo myservo;

// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int servoPower =  9;      // the number of the servo's pin
const int ledPin = 7;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {

  pinMode(servoPower, OUTPUT);      
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, INPUT);

  myservo.attach(servoPower);

}

void loop() {

  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  if (buttonState == HIGH) {     

    ledState = digitalRead(ledPin);
    digitalWrite(ledPin, HIGH);
    
    myservo.writeMicroseconds(1800);

  } else {
    digitalWrite(ledPin, LOW); 
    writeMicroseconds(1200);   
  }
}

I don’t think that’s any use to anyone but I’ll include just to prove I tried 8)

You could just put it into setup().

Alternatively, create a boolean variable, something like bool needToDoIt = true , setting it to true. Then, in loop, you use an if (needToDoIt), to run your one-time code, and within that code, set NeedToDoIt = false

You may define a variable outside the loop() called int done=0;
Then add done=1; after servo rotation in both cases.
Then add if (done==0) to enclose both rotation cases, so the cases only run if rotation is not done.

You can generalize it into if (done<5) if you want it done say 5 times.

I have just realised I left out a hugely important bit, my sincerest apologies all.

I want the code to remain live, per say, so once it has rotated clockwise once, it MAY rotate ANTIclockwise once. After that, it may rotate clockwise again, only once, and it can keep alternating like that.

The whole system is going to be remote controlled, so upon pressing the button, it rotates clockwise. When the button is "un-pressed" it rotates anticlockwise.

Then you will have to use a state machine. Don't be scared though. The state machine will have only two state, one_way, and other_way. You only rotate clockwise if status is equal to clockwise. Then after you rotate clockwise, set status=counterclockwse. Same for rotating counter clockwise.

writing a progam which does what you describe is easy, but let's think about it :

1 - at start, the button is "released" -> nothing happens, right ? 2 - you push the button -> clockwise move, just once .... still right ? 3 - now you release the button.... nothing must happen ? 4 - you want a anticlockwise movement, OK, so you need to release the button, and, before you release it, you must push it... : a - you don't want any movement ? -> OK, but that means that you'll never be able to get 2 clockwise movements without an anticlockwise between. Is tha what you want ? b - you want to be able to move several times clockwise (or anti-clockwise) -> OK, but when do you know the anticlockwise movement must happen ?

remember your button has only 2 states, and one of them (release) is true when you don't push it .

I think you need to describe more precisely what you want ;)

Ralphie: I have just realised I left out a hugely important bit, my sincerest apologies all.

I want the code to remain live, per say, so once it has rotated clockwise once, it MAY rotate ANTIclockwise once. After that, it may rotate clockwise again, only once, and it can keep alternating like that.

The whole system is going to be remote controlled, so upon pressing the button, it rotates clockwise. When the button is "un-pressed" it rotates anticlockwise.

The code appears to do that, so perhaps you ought to explain what it's doing that's wrong. Also, have you got a conventional servo where the pulse length corresponds to a position, or one modified for "continuous rotation" where the pulse length corresponds to a speed?