#include <Arduino.h>
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2/archive/refs/heads/master.zip
#include <Servo.h>
Bounce2::Button button = Bounce2::Button();
const int buttonPin = 4;
const int servo1Pin = 9;
const int servo2Pin = 10;
const int ledPin = LED_BUILTIN;
byte step = 1;
const int courseTime = 10;
const int time = (courseTime * 300) / 180;
Servo myservo1;
Servo myservo2;
void setup()
{
Serial.begin(9600);
button.attach(buttonPin, INPUT_PULLUP);
button.interval(5); // interval im ms
button.setPressedState(LOW);
myservo1.write(0); //starts motor at 0 degress
myservo2.write(0);
myservo1.attach(servo1Pin);
myservo2.attach(servo2Pin);
}
void loop()
{
button.update();
if (button.pressed())
{
if (step == 0)
{
step = 1;
moveServo();
}
else
{
step = 0;
moveServo();
}
}
}
void moveServo()
{
Serial.print("step = ");
Serial.println(step);
switch (step)
{
case 0:
for (int pos = 0; pos < 120; pos++)
{
myservo1.write(pos);
myservo2.write(pos);
}
break;
case 1:
for (int pos = 120; pos > 0; pos--)
{
myservo1.write(pos);
myservo2.write(pos);
}
break;
default:
break;
}
}