// final Seat Occupancy Switch project, i.e. reading lamp
// uses 5v buck converter attached to 12v input from 12v ad/dc converter to power Nano at 5v and LED light panel at 12 volts
// and MOSFET attached to car seat pressure switch to trigger on/off lights when chair is occupied
const byte SwitchPin = 3; // now just a momentary switch that'll be replaced with the seat occupany sensor
const byte LedReadingLightPin = A0; // This triggers the Mosfet to turn on the reading light
const int NanoLed = 13; // this is the builtin LED on the Arduino Nano board
const byte seatOccupied = LOW;
const byte seatFree = HIGH;
const byte OFF = LOW;
const byte ON = HIGH;
unsigned long firstClosing = 0;
byte SwitchState = seatFree; // variable for reading the pushbutton status
void setup() {
pinMode(LedReadingLightPin, OUTPUT);
pinMode(SwitchPin, INPUT_PULLUP);
}
void loop() {
SwitchState = digitalRead(SwitchPin);
if (SwitchState == seatOccupied) {
if (firstClosing == 0) { // if no time is stored yet indicated through value 0
firstClosing = millis(); // store snapshot of time in the moment of first close
}
digitalWrite(LedReadingLightPin, ON);
}
// check if more than 3 seconds have passed by since the moment
// the contact was closed for the first time
if (millis() - firstClosing > 3000) {
if (SwitchState == seatFree) {
digitalWrite(LedReadingLightPin, OFF);
firstClosing = 0; // reset timing variable to zero
}
}
}