// Pin definitions
const int sensorIRPin = 12; // Sensor IR
const int startButtonPin = 11; // Tombol mulai
const int stopButtonPin = 10; // Tombol stop
const int led1Pin = 2; // LED1
const int led2Pin = 3; // LED2
const int buzzerPin = 4; // Buzzer
// Variables
bool isSystemActive = false; // Status sistem aktif/tidak
bool isSensorTriggered = false; // Status sensor IR
unsigned long lastSensorTrigger = 0; // Waktu terakhir sensor terdeteksi
unsigned long buzzerStartTime = 0; // Waktu buzzer mulai
bool isBuzzerActive = false; // Status buzzer
void setup() {
// Initialize button pins - sensorIR as INPUT (active HIGH), others with pull-up
pinMode(sensorIRPin, INPUT); // Sensor IR active HIGH
pinMode(startButtonPin, INPUT_PULLUP);
pinMode(stopButtonPin, INPUT_PULLUP);
// Initialize output pins
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Turn off all outputs initially
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(buzzerPin, LOW);
}
void loop() {
// Read input states (buttons inverted because of pull-up, sensor direct reading)
bool startPressed = !digitalRead(startButtonPin);
bool stopPressed = !digitalRead(stopButtonPin);
bool sensorState = digitalRead(sensorIRPin); // Direct reading for active HIGH
// Start button pressed
if (startPressed && !isSystemActive) {
isSystemActive = true;
digitalWrite(led1Pin, HIGH); // Turn on LED1
lastSensorTrigger = millis(); // Start timing
}
// System is active
if (isSystemActive && !isBuzzerActive) {
// Check Sensor IR
if (sensorState && !isSensorTriggered) {
digitalWrite(led2Pin, HIGH);
lastSensorTrigger = millis(); // Reset timer
isSensorTriggered = true;
}
else if (!sensorState && isSensorTriggered) {
digitalWrite(led2Pin, LOW);
isSensorTriggered = false;
}
// Check if 2 minutes have passed without sensor trigger
if (millis() - lastSensorTrigger >= 60000) { // 2 minutes = 120000 ms
digitalWrite(buzzerPin, HIGH);
isBuzzerActive = true;
buzzerStartTime = millis();
}
}
// Buzzer is active
if (isBuzzerActive) {
// Check if stop button pressed or 1 minute passed
if (stopPressed || (millis() - buzzerStartTime >= 60000)) { // 1 minute = 60000 ms
// Reset system
digitalWrite(buzzerPin, LOW);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
isSystemActive = false;
isBuzzerActive = false;
}
}
}