1

I am trying to get the Servo to rotate from 0 degrees -> 90 degrees -> 0 degrees one time when the toggle is switched on, and then reset when the toggle is switched off. Any help would be appreciated! Thank you.

#include <Servo.h>

Servo myServo;
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position
int switchPin = 7;
int servoPin = 9;
int startPosition = 0;
int endPosition = 90; 
void setup() {
  myServo.attach(servoPin);
  pinMode(switchPin, INPUT_PULLUP);
}

void loop() {
  int buttonState = digitalRead(switchPin);

  if ( buttonState == HIGH) {

      for (pos = startPosition; pos <= endPosition; pos += 1) {

        myServo.write(pos);
        delay(0);
      }

      for (pos = endPosition;  pos >= startPosition; pos -= 1) {
        myServo.write(pos);
        delay(0);
      }
  }
  if (buttonState == LOW) {
    myServo.write(startPosition);
  }
}

0 Answers0