I have this program that turn on a light when it detects th first movement from the PIR sensor after 7 PM (19:00) and it turn off the light at 2AM (02:00). Now, i'm really having problem interfacing a button that controls the light when i'm going to bed. I want to shut down the light when i go to bed and so when i press the button but the light keeps turning on. I can't see the problem there. I want the button to be able to turn off the light if i go to bed and then the system will start the loop again for the next day. The code is this:
#include <virtuabotixRTC.h>
// Pin definitions
#define PIN_CLK A5 // CLK pin of the RTC module
#define PIN_DAT A4 // DAT pin of the RTC module
#define PIN_RST 12 // RST pin of the RTC module
#define PIR_PIN 2 // PIR sensor pin
#define LED_PIN 13 // LED pin
#define BUTTON_PIN 3 // Button pin
// RTC module initialization
virtuabotixRTC myRTC(PIN_CLK, PIN_DAT, PIN_RST);
bool waitingForMovement = false; // Variable that indicates if we need to wait for the first movement
bool lightOn = false; // Variable that indicates if the LED is on
void setup() {
Serial.begin(9600); // Initialize serial monitor
pinMode(PIR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Set the button with internal pull-up
}
void loop() {
// Update time from RTC
myRTC.updateTime();
int hour = myRTC.hours;
int minute = myRTC.minutes;
int second = myRTC.seconds;
// Check if it's time to start waiting for movement
if (hour == 12 && minute == 43) {
waitingForMovement = true; // From now on, we wait for the first movement
}
// Check if it's time to turn everything off
if (hour == 12 && minute == 44 || digitalRead(BUTTON_PIN) == LOW) {
waitingForMovement = false; // Deactivate waiting
lightOn = false; // Turn off the light
digitalWrite(LED_PIN, LOW);
}
// If we are waiting for the first movement and the PIR detects it
if (waitingForMovement && digitalRead(PIR_PIN) == HIGH) {
lightOn = true; // Turn on the light
digitalWrite(LED_PIN, HIGH);
waitingForMovement = false; // No longer waiting for movement
}
/*
// Check if the button has been pressed
if (digitalRead(BUTTON_PIN) == LOW) { // The button is active with LOW
lightOn = false; // Turn off the light
digitalWrite(LED_PIN, LOW);
}
*/
// Print the status in the serial monitor
Serial.print("Time: ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.print(second);
Serial.print(" | WaitingForMovement: ");
Serial.print(waitingForMovement);
Serial.print(" | PIR: ");
Serial.print(digitalRead(PIR_PIN));
Serial.print(" | LightOn: ");
Serial.println(lightOn);
delay(1000); // Wait one second before updating again
}
i've tried chat gpt but it did not work
5 posts - 3 participants