#include <Servo.h>
static constexpr int threshold = 700;
static constexpr int hist = 50;
Servo myservo;
void setup()
{
Serial.begin(9600);
myservo.attach(4);
myservo.write(60);
}
static bool alight = false;
static bool dir = false;
void loop()
{
int val = analogRead(A3);
Serial.println(val);
if (alight) {
alight = val > threshold - hist;
} else {
alight = val > threshold + hist;
if (alight) {
myservo.write((dir = !dir) ? 0 : 120);
delay(1000);
myservo.write(60);
}
}
delay(100);
}