#include <PinChangeInterrupt.h>
#include <TimerOne.h>
const int motionPin = 2; // Pin for the PIR motion sensor (INT0)
const int irPin = 3; // Pin for the IR sensor (INT1)
const int buttonPin = 4; // Pin for the pushbutton (PCINT)
const int ledPin = 12; // Pin for the LED to be triggered by all sensors
volatile bool motionDetected = false; // Volatile variable to track motion
volatile bool irDetected = false; // Volatile variable to track IR detection
volatile bool buttonPressed = false; // Volatile variable to track button press
bool motionPrint = false; // Flags to control printing
bool irPrint = false;
bool buttonPrint = false;
unsigned long lastDebounceTime = 0; // Debounce time
unsigned long debounceDelay = 50; // Debounce delay
unsigned long ledLastDetectedTime = 0; // Last time LED was triggered
unsigned long ledOffDelay = 1000; // Delay to turn off LED
void setup() {
pinMode(motionPin, INPUT);
pinMode(irPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Initialize Serial communication for UART
Serial.println("Arduino 1 ready!");
// Set up the external interrupts for motion and IR sensors
attachInterrupt(digitalPinToInterrupt(motionPin), motionISR, RISING);
attachInterrupt(digitalPinToInterrupt(irPin), irISR, RISING);
// Set up Pin Change Interrupt for the button
attachPCINT(digitalPinToPCINT(buttonPin), buttonISR, CHANGE);
// Set up TimerOne interrupt
Timer1.initialize(1000000); // Set timer to 1 second (1,000,000 microseconds)
Timer1.attachInterrupt(timerISR);
// Initial state of the LED
digitalWrite(ledPin, LOW);
}
void loop() {
// Turn off LED after delay
if ((motionDetected || irDetected || buttonPressed) && (millis() - ledLastDetectedTime >= ledOffDelay)) {
digitalWrite(ledPin, LOW); // Turn off external LED
motionDetected = false;
irDetected = false;
buttonPressed = false;
}
// Print and send motion detected message if flag is set
if (motionPrint) {
Serial.println("Motion detected");
sendSensorData("motion");
motionPrint = false;
}
// Print and send IR detected message if flag is set
if (irPrint) {
Serial.println("IR sensor triggered");
sendSensorData("ir");
irPrint = false;
}
// Print and send button pressed message if flag is set
if (buttonPrint) {
Serial.println("Button pressed");
sendSensorData("button");
buttonPrint = false;
}
delay(1000); // Delay for 1 second
}
// ISR for motion sensor
void motionISR() {
motionDetected = true;
motionPrint = true;
ledLastDetectedTime = millis();
digitalWrite(ledPin, HIGH); // Turn on external LED
}
// ISR for IR sensor
void irISR() {
irDetected = true;
irPrint = true;
ledLastDetectedTime = millis();
digitalWrite(ledPin, HIGH); // Turn on external LED
}
// ISR for pushbutton using Pin Change Interrupt
void buttonISR() {
unsigned long currentTime = millis();
if ((currentTime - lastDebounceTime) > debounceDelay) {
buttonPressed = true;
buttonPrint = true;
ledLastDetectedTime = currentTime;
digitalWrite(ledPin, HIGH); // Turn on external LED
lastDebounceTime = currentTime;
}
}
// Timer interrupt service routine (ISR)
void timerISR() {
// Toggle the built-in LED
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
Serial.println("Timer interrupt: LED toggled");
}
// Function to send sensor data to the second Arduino
void sendSensorData(const char* sensorType) {
if (strcmp(sensorType, "motion") == 0) {
Serial.println("Sending: Motion Detected");
} else if (strcmp(sensorType, "ir") == 0) {
Serial.println("Sending: IR Detected");
} else if (strcmp(sensorType, "button") == 0) {
Serial.println("Sending: Button Pressed");
}
}