#include <IRremote.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int PIR_PIN = 23;
int IR_RECEIVE_PIN = 13;
int LED_PIN = 4;
int brightness = 0; // start with 0 brightness
int fadeAmount = 1; // set how much to increase or decrease the brightness each time (lower value will make the fade smoother)
int ledState = LOW; // the initial state of the LED
unsigned long motionStartTime = 0;
int pirState = LOW; // the initial state of the PIR sensor
int textY = 0;
const int maxMessages = 8;
String messages[maxMessages] = { "", "", "", "", "", "", "", "" };
int currentMessage = 0;
bool isMotionSensorActive = false;
bool isPressed = true;
bool motionSensorModeButtonState = false;
bool motionSensorActive = false; // Add this variable at the top of the file
IRrecv irrecv(IR_RECEIVE_PIN);
decode_results results;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(PIR_PIN, INPUT);
digitalWrite(LED_PIN, LOW);
Serial.begin(115200);
irrecv.enableIRIn();
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
for (;;);
}
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
Serial.println("boot here");
switch (results.value) {
case 0xF0DB1AE0: // Power button
motionSensorModeButtonState = !motionSensorModeButtonState;
if (motionSensorModeButtonState) {
Serial.println("Motion Sensor Mode ON");
delay(100);
display.clearDisplay();
display.setTextSize(0.5);
display.setTextColor(WHITE);
display.setCursor(0, 0);
// Display static text
display.println("Motion Sensor Mode ON");
display.display();
delay(100);
display.clearDisplay();
} else {
Serial.println("Motion Sensor Mode OFF");
analogWrite(LED_PIN, 0);
ledState = LOW;
brightness = 0;
fadeAmount = 1;
motionStartTime = 0;
display.clearDisplay();
display.setTextSize(0.5);
display.setTextColor(WHITE);
display.setCursor(0, 0);
// Display static text
display.println("Motion Sensor Mode OFF");
display.display();
}
break;
case 0x6B7832FF: // button 1 (alternative code)
if (motionSensorModeButtonState) {
motionSensorModeButtonState = false;
Serial.println("Cancelling Motion Sensor Mode");
}
ledState = !ledState;
if (ledState) {
display.clearDisplay();
display.setTextSize(0.5);
display.setTextColor(WHITE);
display.setCursor(0, 0);
Serial.println("Fading ON LED");
display.println("Fading ON LED");
display.display();
fadeOn();
display.clearDisplay();
display.display();
ledState = HIGH;
} else {
display.clearDisplay();
display.setTextSize(0.5);
display.setTextColor(WHITE);
display.setCursor(0, 0);
Serial.println("Fading OFF LED");
display.println("Fading OFF LED");
display.display();
fadeOff();
display.clearDisplay();
display.display();
}
break;
default:
break;
}
irrecv.resume();
//display.setCursor(0,0);
}
if (motionSensorModeButtonState) {
pirState = digitalRead(PIR_PIN);
if (pirState == HIGH) {
if (ledState == LOW) {
fadeOn();
ledState = HIGH;
}
messages[currentMessage] = "Motion detected!";
Serial.println(messages[currentMessage]);
} else if (pirState == LOW) {
if (ledState == HIGH) {
fadeOff();
if (brightness <= 0) {
analogWrite(LED_PIN, 0);
ledState = LOW;
}
}
messages[currentMessage] = "No motion detected!";
Serial.println(messages[currentMessage]);
}
display.clearDisplay(); // Clear the OLED display
display.setTextSize(0.5);
display.setTextColor(WHITE);
for (int i = maxMessages - 1 ; i >= 0; i--) {
display.setCursor(0, (maxMessages - 1 - i) * 8); // Set cursor to the current line
display.println(messages[(currentMessage - i + maxMessages) % maxMessages]);
}
display.display();
currentMessage = (currentMessage + 1) % maxMessages; // Update the current message index
delay(10); // Add a delay to control the update speed
}
}
void fadeOn() {
while (brightness < 127) {
analogWrite(LED_PIN, brightness); // set the LED strip brightness
brightness = brightness + fadeAmount; // increase the brightness
delay(3);
//Serial.println(brightness);
}
}
void fadeOff() {
while (brightness >= 0) {
analogWrite(LED_PIN, brightness); // set the LED strip brightness
brightness = brightness - fadeAmount; // increase the brightness
delay(3);
//Serial.println(brightness);
}
brightness = 0;
//Serial.println(brightness);
}
// void motionSensorMode() {
// isMotionSensorActive = true;
// while (isMotionSensorActive) {
// pirState = digitalRead(PIR_PIN);
// if (pirState == HIGH) {
// Serial.println("Motion detected!");
// if (ledState == LOW) {
// fadeOn();
// ledState = HIGH;
// }
// } else if (pirState == LOW) {
// Serial.println("No motion detected!");
// if (ledState == HIGH) {
// fadeOff();
// if (brightness <= 0) {
// analogWrite(LED_PIN, 0);
// ledState = LOW;
// }
// }
// }
// delay(10);
// // Check for remote button press to toggle the motion sensor mode
// if (irrecv.decode(&results)) {
// if (results.value == 0x3D9AE3F7) {
// Serial.println("Toggling Motion Sensor Mode");
// isMotionSensorActive = !isMotionSensorActive;
// motionSensorModeButtonState = !motionSensorModeButtonState;
// }
// irrecv.resume();
// }
// }
// // Reset LED and brightness values when exiting motion sensor mode
// analogWrite(LED_PIN, 0);
// ledState = LOW;
// brightness = 0;
// fadeAmount = 1;
// motionStartTime = 0;
// }