const int servoPin = 2;
const int buttonPin = 3;
const int LEDPin = 4;
#include <Servo.h>
Servo visorServo;
void setup() {
visorServo.write(0); // Initial position
visorServo.attach(servoPin);
pinMode(buttonPin, INPUT_PULLUP); // Connect button between pin and GROUND. LOW when pushed.
pinMode(LEDPin, OUTPUT);
digitalWrite(LEDPin, LOW); // LED off
}
void loop() {
static unsigned long lastPushedTime = 0;
static boolean visorClosed = true;
static boolean lastButtonState = HIGH;
boolean newButtonState = digitalRead(buttonPin);
// If the button is down and it has been a while since it was last down...
if (newButtonState == LOW && lastButtonState == HIGH && millis() - lastPushedTime > 100) {
lastPushedTime = millis();
if (visorClosed) {
visorServo.write(180); // Open visor
visorClosed = false;
digitalWrite(LEDPin, HIGH); // Turn on light
}
else { // Viso is open
visorServo.write(0); // Close visor
visorClosed = true;
digitalWrite(LEDPin, LOW); // Turn off light
}
}
lastButtonState = newButtonState;
}