const int inputPin = 2; // Pin for input signals
const int outputPin = 3; // Pin for output signal
const int pinRED = 4;
const int pinGREEN = 6;
const int pinBLUE = 7;
const int pinRGBAnode = 5;
volatile int inputCount = 0; // Counter for input signals
unsigned long firstInputTime = 0; // Time when the first input is received
const unsigned long inputTimeout = 7000; // 7 seconds timeout
const unsigned long outputDuration = 5000; // 5 seconds output duration
bool outputActive = false; // Flag to track output state
unsigned long outputStartTime = 0; // Time when the output was activated
bool pin2Locked = false; // Flag to track if pin 2 is locked
unsigned long lockStartTime = 0; // Time when pin 2 was locked
//const unsigned long lockDuration = 15 * 60 * 1000; // 15 minutes in milliseconds
const unsigned long lockDuration = 20000; // 20 Secconds in milliseconds for TESTING
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 150; // the debounce time; increase if the output flickers
void setup() {
pinMode(inputPin, INPUT);
pinMode(outputPin, OUTPUT);
pinMode(pinRED, OUTPUT);
pinMode(pinGREEN, OUTPUT);
pinMode(pinBLUE, OUTPUT);
pinMode(pinRGBAnode, OUTPUT);
digitalWrite(outputPin, LOW); // Ensure output is initially LOW
digitalWrite(pinRED, HIGH);
digitalWrite(pinGREEN, HIGH);
digitalWrite(pinBLUE, HIGH);
digitalWrite(pinRGBAnode, HIGH);
Serial.begin(9600); // Initialize serial communication for debugging
attachInterrupt(digitalPinToInterrupt(inputPin), countInput, CHANGE); // Attach interrupt for input signals
}
void loop() {
unsigned long currentTime = millis();
if (outputActive) {
// Check if the output duration has expired
if (currentTime - outputStartTime >= outputDuration) {
Serial.println("Output duration expired, deactivating output.");
deactivateOutput();
// Lock pin 2 LOW after successful output
lockPin2(currentTime);
}
} else {
// Check if pin 2 is locked and if the lock duration has elapsed
if (pin2Locked && (currentTime - lockStartTime >= lockDuration)) {
Serial.println("15-minute lock on pin 2 completed.");
unlockPin2();
}
// Check if the 7-second timer has expired
if (!pin2Locked && firstInputTime != 0 && (currentTime - firstInputTime >= inputTimeout)) {
Serial.println("7 seconds passed without 5 inputs, resetting counter.");
resetCounter();
}
// Check if the counter has reached 5
if (!pin2Locked && inputCount >= 5) {
Serial.println("5 inputs received within 7 seconds, activating output.");
activateOutput();
}
}
colorCounter();
}
void countInput() {
unsigned long currentTime = millis();
// Debounce the input
if ((currentTime - lastDebounceTime) > debounceDelay) {
if (!pin2Locked && digitalRead(inputPin) == HIGH) { // Check if pin 2 is not locked
if (inputCount == 0) {
firstInputTime = currentTime; // Start the timer when the first input is received
Serial.println("First input received, starting 7-second timer.");
}
inputCount++;
Serial.print("Input count: ");
Serial.println(inputCount);
lastDebounceTime = currentTime; // Reset the debounce timer
}
}
}
void activateOutput() {
digitalWrite(outputPin, HIGH); // Set the output pin HIGH
outputActive = true;
outputStartTime = millis(); // Record the time when output was activated
Serial.println("Output activated.");
}
void deactivateOutput() {
digitalWrite(outputPin, LOW); // Set the output pin LOW
outputActive = false;
resetCounter();
Serial.println("Output deactivated.");
}
void resetCounter() {
inputCount = 0;
firstInputTime = 0;
Serial.println("Counter reset.");
}
void lockPin2(unsigned long startTime) {
pin2Locked = true;
lockStartTime = startTime;
digitalWrite(inputPin, LOW); // Lock pin 2 LOW
Serial.println("Horn LOCKED for 15 minutes.");
}
void unlockPin2() {
pin2Locked = false;
Serial.println("Horn Now Unlocked !");
}
void colorCounter() {
switch(inputCount) {
case 0:
digitalWrite(pinRED, HIGH);
digitalWrite(pinGREEN, HIGH);
digitalWrite(pinBLUE, HIGH);
break;
case 1:
digitalWrite(pinRED, HIGH);
digitalWrite(pinGREEN, HIGH);
digitalWrite(pinBLUE, LOW);
break;
case 2:
digitalWrite(pinRED, HIGH);
digitalWrite(pinGREEN, LOW);
digitalWrite(pinBLUE, LOW);
break;
case 3:
digitalWrite(pinRED, HIGH);
digitalWrite(pinGREEN, LOW);
digitalWrite(pinBLUE, HIGH);
break;
case 4:
digitalWrite(pinRED, LOW);
digitalWrite(pinGREEN, LOW);
digitalWrite(pinBLUE, HIGH);
default:
digitalWrite(pinRED, LOW);
digitalWrite(pinGREEN, HIGH);
digitalWrite(pinBLUE, HIGH);
break;
}
}