Trailer alarm system

#include "ArduinoGraphics.h"
#include "Arduino_LED_Matrix.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27, 16, 2); 

const int ledPin = 13;       
const int piezoSensor = A0;  
const int threshold = 100;   // Threshold for motion detection
const int buzzer = 12;
const int switchPin = 11;
const int switchPin2 = 10;

int sensorReading = 0;  
int switchState = 0;
int previousSwitchState = LOW;
int switchState2 = 0;
unsigned long switchStartTime = 0;
bool switchWasHigh = false;
bool alarmActive = false;
bool matrixDisplayed = false; // To ensure matrix message only appears once

void setup() {
  lcd.init();
  lcd.backlight();
  pinMode(switchPin, INPUT_PULLUP);
  pinMode(switchPin2, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buzzer, OUTPUT);
  Serial.begin(115200);  // Start Serial for debugging
 

  // Default LCD message
  lcd.setCursor(0, 0);
  lcd.print("System Ready");
}

void loop() {
  switchState2 = digitalRead(switchPin2);
  switchState = digitalRead(switchPin);

  // ---- Door Closed ----
  if (switchState2 == LOW && switchState == LOW) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Door Closed        ");
   
    return;
  }

  // ---- Door Open & Alarm Countdown ----
  if (switchState2 == HIGH && switchState == HIGH) {
    if (!switchWasHigh) {  
      switchStartTime = millis();
      switchWasHigh = true;
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Door Open");

      for (int i = 10; i >= 0; i--) {
        if (digitalRead(switchPin) == LOW || digitalRead(switchPin2) == LOW) {
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Door Closed");
          
          return;
        }
        lcd.setCursor(0, 1);
        lcd.print("Alarm in ");
        lcd.print(i);
        lcd.print("  ");
        delay(1000);
      }
      
    }
  }

  // ---- Trigger Buzzer After Countdown ----
  if (millis() - switchStartTime >= 10000 && switchState2 == HIGH) {
    digitalWrite(buzzer, HIGH);
  }

  // ---- Cancel Alarm if Switch is Flipped ----
  if (switchState2 == LOW || switchState == LOW) {
    digitalWrite(buzzer, LOW);
    switchWasHigh = false;
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Door Closed        ");
    
    if (switchState2 == HIGH && switchState == LOW) {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Door Open");
      lcd.setCursor(0, 1);
      lcd.print("Alarm Cancelled    ");
      delay(1000);
      lcd.setCursor(0,1);
      lcd.print("                    ");
    }
    
    
  }

  // ---- Motion Detection ----
  switchState = digitalRead(switchPin);
  sensorReading = analogRead(piezoSensor);

  // Print sensor reading to serial monitor for debugging
  Serial.print("Piezo Sensor Reading: ");
  Serial.println(sensorReading);  // Output sensor reading to serial monitor

  // If the switch is ON (i.e., alarm system is activated)
  if (switchState == HIGH) {
    if (previousSwitchState == LOW) {
      alarmActive = true;
      previousSwitchState = HIGH;
      
    }

    // If piezo sensor reading exceeds the threshold
    if (sensorReading >= threshold) {
      digitalWrite(ledPin, HIGH);  // Turn on LED
      digitalWrite(buzzer, HIGH);  // Turn on Buzzer
      lcd.setCursor(0, 1);
      lcd.print("Motion Detected ");
      delay(5000);  // Keep the LED and buzzer on for 5 seconds
      digitalWrite(ledPin, LOW);  // Turn off LED
      digitalWrite(buzzer, LOW);  // Turn off Buzzer
      lcd.setCursor(0, 1);
      lcd.print("                ");  // Clear motion detected message
    }
  } else if (previousSwitchState == HIGH) {
    previousSwitchState = LOW;
    alarmActive = false;
    
    
  }



  if (switchState2 == HIGH) {
    lcd.clear();
   lcd.setCursor(0, 0);
   lcd.print("Door Open       ");
  } 

}

And the 1.000.000 $ question is ... ?

Welcome!
Purple!
What is the question. Where is the annotated schematic? Where is the description of what it is? Without enough information you will get a simple answer "purple".

Purple is the wrong answer!

The correct is answer is 42 :slight_smile:

Is that before or after the hitch?

Let's ask the mice.

To eat or not to eat, that is the question ...