#include <Servo.h>
#include <Toggle.h>
const byte servoPin = 3;
const byte buttonPin = 12;
enum {SERVO_0, SERVO_90} servoPosition = SERVO_0;
Toggle button;
Servo servo;
void moveTo0() {
servo.write(0);
servoPosition = SERVO_0;
}
void moveTo90() {
servo.write(90);
servoPosition = SERVO_90;
}
void setup() {
button.begin(buttonPin);
servo.attach(servoPin);
moveTo0();
}
void loop() {
button.poll();
if (button.onPress()) { // we got a new button press
switch (servoPosition) { // handle the change based on current position
case SERVO_0: moveTo90(); break; // we were at 0°, move to 90°
case SERVO_90: moveTo0(); break; // we were at 90°, move to 0°
}
}
}