#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define bulb 19
#define pir 23
#define SCREEN_WIDTH 128 // OLED width, in pixels
#define SCREEN_HEIGHT 64 // OLED height, in pixels
#define button 18
// create an OLED display object connected to I2C
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
long t1, t2;
void setup() {
Serial.begin(9600);
pinMode(bulb, OUTPUT);
pinMode(button, INPUT);
pinMode(pir, INPUT);
// 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(1); // set text size
oled.setTextColor(WHITE); // set text color
oled.setCursor(0, 2); // set position to display (x,y)
oled.println("Robotronix"); // set text
oled.display(); // display on OLED
t1=millis();
digitalWrite(bulb, HIGH);
}
void loop() {
t2=millis();
long diff=t2-t1;
int button_value=digitalRead(button);
Serial.println(button_value);
if (button_value==1)
{
digitalWrite(bulb, HIGH);
}
int pir_value=digitalRead(pir);
if (diff>5000 && pir_value==0)
{
digitalWrite(bulb, LOW);
t1=t2;
}
if (pir_value==1)
{
digitalWrite(bulb, HIGH);
}
if (pir_value==1)
{
oled.clearDisplay();
oled.setCursor(0, 2); // set position to display (x,y)
oled.println("Motion Detected"); // set text
oled.display();
}
else{
oled.clearDisplay();
oled.setCursor(0, 2); // set position to display (x,y)
oled.println("No Motion"); // set text
oled.display();
}
delay(500);
}