#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Define the pin numbers for the LED and PIR sensor
const int ledpin = 2; // Pin number for the LED
const int PIRinput = 5; // Pin number for the PIR sensor
// Variable to store the current state of the PIR sensor
int pirState = LOW;
// Setup function: runs once when the device starts up
void setup() {
// Set the LED pin as an output
pinMode(ledpin, OUTPUT);
// Set the PIR sensor pin as an input
pinMode(PIRinput, INPUT);
// Begin serial communication at a baud rate of 9600
Serial.begin(9600);
// initialize OLED display with I2C address 0x3C
if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(2000); // wait two seconds for initializing
oled.clearDisplay(); // clear display
oled.setTextSize(2); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 2); // set position to display (x,y)
oled.println("INTI"); // set text
oled.setTextSize(1); // set text size
oled.println("");
oled.println("International"); // set text
oled.println("");
oled.println("University"); // set text
oled.display(); // display on OLED
}
// Loop function: runs repeatedly as long as the device is powered on
void loop() {
// Read the state of the PIR sensor
const int IP = digitalRead(PIRinput);
// Delay for stability
delay(100);
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(4, 2); // set position to display (x,y)
// If motion is detected by the PIR sensor
if (IP == 1) {
// Turn on the LED
digitalWrite(ledpin, HIGH);
// If this is the first time motion is detected since the last reset
if (pirState == LOW) {
// Print "Motion detected!" to the serial monitor
Serial.println("Motion detected!");
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(4, 2); // set position to display (x,y)
oled.println("Motion detected"); // set text
oled.display(); // display on OLED
// Update pirState to indicate motion detection
pirState = HIGH;
}
// Delay for 1 second
delay(1000);
} else {
// If no motion is detected by the PIR sensor
// Turn off the LED
digitalWrite(ledpin, LOW);
// If motion was detected previously
if (pirState == HIGH) {
// Print "Motion ended!" to the serial monitor
Serial.println("Motion ended!");
oled.clearDisplay(); // clear display
oled.setTextSize(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(4, 2); // set position to display (x,y)
oled.println("Motion Ended"); // set text
oled.display(); // display on OLED
// Update pirState to indicate no motion
pirState = LOW;
}
// Delay for 1 second
delay(1000);
}
}