Servo motor wont rotate at low state

hello there… I can’t run my code, I just want to run a servo motor at low state in my push button. Im using only one push button and a LED. I just want my code to run like when the LED is On the the servo rotate 180 and if Off the servo will rotate to its original position…

here’s my code below. please and thank you.

#include<Servo.h>
Servo myServo;
#define servoPin 13
int inPin = A0;         // the number of the input pin
int outPin = 10;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  myServo.attach(servoPin);
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
  myServo.write(0);
  
}

void loop()
{
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    //  myServo.write(0);
    else
      state = HIGH;
      myServo.write(180);

    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;


}

You have gone wrong with the else, it will be triggered too often.

I tried changing it but its just the same...it only moves in the high state.. :frowning:

ajperz:
I tried changing it but

we can't see your attempt

#include<Servo.h>
Servo myServo;
#define servoPin 13
int inPin = A0;         // the number of the input pin
int outPin = 10;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 1000;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  myServo.attach(servoPin);
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
}

void loop()
{
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
      myServo.write(0);
    else
      state = HIGH;
      myServo.write(180);

    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;


}

Your indentation suggests something that your cannot supply.
Try some more of these {}

I tried your suggestion but the code it ruined the code :((

#include<Servo.h>
Servo myServo;
#define servoPin 13
int inPin = A0;         // the number of the input pin
int outPin = 10;       // the number of the output pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  myServo.attach(servoPin);
  pinMode(inPin, INPUT);
  pinMode(outPin, OUTPUT);
}

void loop()
{
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
      myServo.write(0);
      }
    else
{      state = HIGH;
      myServo.write(180);

    time = millis();    
  }

  digitalWrite(outPin, state);

  previous = reading;


}

I can't disagree with you.

if (state == HIGH) {
      state = LOW;
      myServo.write(0);
      }
    else
{      
state = HIGH;
      myServo.write(180);
  }

Oh I see... Very thank you for the help mate.. SML :kissing: