#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <IRremote.h>
//#include <IRMP.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define BUTTON_PIN 2
#define LED_PIN 13 // Assuming the LED is connected to pin 13
#define irSensorPin 3
const int intervals[] = {15 * 60, 30 * 60, 45 * 60, 60 * 60}; // 15, 30, 45, 60 minutes
int selectedInterval = 0;
unsigned long lastButtonPress = 0;
const int debounceDelay = 200;
void setup() {
pinMode(BUTTON_PIN, INPUT);
digitalWrite(BUTTON_PIN, HIGH);
pinMode(LED_PIN, OUTPUT);
pinMode(irSensorPin, INPUT);
digitalWrite(LED_PIN, LOW); // Ensure the LED starts off
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display();
delay(2000);
display.clearDisplay();
updateDisplay();
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW && millis() - lastButtonPress > debounceDelay) {
lastButtonPress = millis();
selectedInterval = (selectedInterval + 1) % 4;
updateDisplay();
delay(500);
}
//unsigned long currentTime = millis();
unsigned long delayDuration = intervals[selectedInterval] * 100; // Convert seconds to milliseconds
if (digitalRead(irSensorPin) == LOW) { // Check if IR sensor is obstructed
unsigned long startTime = millis(); // Record the start time when the obstruction is detected
while (millis() - startTime <= intervals[selectedInterval]) { // Wait for 55 seconds for continuous obstruction
delay(100); // Small delay for stability
if (digitalRead(irSensorPin) == HIGH) { // If sensor not obstructed within 55 seconds
digitalWrite(LED_PIN, LOW); // Turn off the LED
break; // Exit the loop
}
}
if (millis() - startTime > intervals[selectedInterval]) { // If 55 seconds elapsed without sensor becoming unobstructed
digitalWrite(LED_PIN, HIGH); // Turn on the LED
}
} else {
digitalWrite(LED_PIN, LOW); // Turn off the LED if sensor is not obstructed
}
}
void updateDisplay() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Selected delay:");
display.setCursor(0, 20);
display.print(intervals[selectedInterval] / 60);
display.print(" minutes");
display.display();
}