#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int PIR = 2;
int sensor = 0;
int buzzer = 3;
int led = 4;
// Bitmap love 16x16
const unsigned char heartBitmap[] PROGMEM = {
0b00001100, 0b00110000,
0b00011110, 0b01111000,
0b00111111, 0b11111100,
0b01111111, 0b11111110,
0b01111111, 0b11111110,
0b01111111, 0b11111110,
0b00111111, 0b11111100,
0b00011111, 0b11111000,
0b00001111, 0b11110000,
0b00000111, 0b11100000,
0b00000011, 0b11000000,
0b00000001, 0b10000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000
};
void setup() {
pinMode(PIR, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(led, OUTPUT);
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("Gagal inisialisasi OLED");
for (;;);
}
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.clearDisplay();
}
void loop() {
sensor = digitalRead(PIR);
display.clearDisplay();
if (sensor == HIGH) {
Serial.println("TERDETEKSI!");
tone(buzzer, 1000);
digitalWrite(led, HIGH);
display.setTextSize(2);
displayTextCenter("WARNING!!!", 20);
display.display();
delay(1000);
} else {
Serial.println("TIDAK TERDETEKSI");
noTone(buzzer);
digitalWrite(led, LOW);
display.setTextSize(1);
displayTextCenter("Safe", 15);
display.drawBitmap((SCREEN_WIDTH - 16) / 2, 30, heartBitmap, 16, 16, SSD1306_WHITE);
display.display();
delay(1000);
}
}
void displayTextCenter(String text, int y) {
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(text, 0, y, &x1, &y1, &w, &h);
int x = (SCREEN_WIDTH - w) / 2;
display.setCursor(x, y);
display.println(text);
}