Hi friends,
(please forgive me if I break any of the forum norms as this is my first post )
I am trying to encode a RTC(DS1307) into a project of mine that consists of a light sensor and a servo motor. The code right now is
#include <Servo.h>. // include Servo library
Servo Switch; // create a Servo object called Switch because it controls the basement switch
const int SensorPin = A0; // Sensor pin
int sensor; // Sensor reading
const int threshold = 500; // threshold to turn Servo
int servoAngle = 0; // variable to set the angle for servo
void setup() {
// put your setup code here, to run once:
Switch.attach(9); // attachs the servo on pin 9 to the myServo object
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
sensor = analogRead(SensorPin); // Stores light sensor value in a variable
Serial.println(SensorPin); // prints the analog representation of the light sensor
servoAngle = map(SensorPin, 0, 1023, 0, 179); // Scales light sensor value to the Servo
if (sensor < threshold) { // when light is on
Switch.write(180); // tell servo to move 180 degrees(hit switch)
delay(1000); // wait for 1 second
Switch.write(0); // tell servo to move back to 0 degrees
delay(1000); // wait for 1 second
}
}
However while this works and all, I need the loop part to run only when it is 9:00 pm. In other words I want the code to be something like "If it is 9:00 pm check if the light sensor detects light. If light is detected run the if code in my sketch". I am asking you guys because I do not know a lot of C++, only Python. I will add a schematic as soon as I make one. Also, I have tried many youtube tutorials and set and read the time on the DS1307 RTC. It matches the real time well enough.
We can't see which RTC library you're using. If you can read the time you should be able to extract the hour (either as part of the library (e.g. rtc.hour() or extract it from the displayed text representation of the time (less preferred in my opinion).
Do you have any reservation to use DS3231 RTC which has alarm feature. You can prgram the RTC to genetae alarm at your desired time to interrupt the UNO R3 (?) and carry out the task.
lastRunDay = -1
loop forever:
read current time from RTC
if hour == 9 and minute == 0:
if currentDay != lastRunDay:
run cycle once
lastRunDay = currentDay
wait a short time
Here it is in C++ Arduino style:
int lastRunDay = -1;
void loop()
{
DateTime now = rtc.now();
if (now.hour() == 9 && now.minute() == 0)
{
if (now.day() != lastRunDay)
{
runCycle();
lastRunDay = now.day();
}
}
delay(1000);
}
This exact "time" (9h, 0m) lasts for one minute, so runCycle(); will be called many times. If you want to continually call runCycle() and lastRunDay = now.day(); ignore the rest of my post...
If you want to call runCycle() and lastRunDay = now.day(); one time... add a "run_one_time" flag, to ensure runCycle(); and lastRunDay = now.day(); are run only one time...
bool run_one_time = 0;
void setup() {
// setup code
}
void loop() {
DateTime now = rtc.now();
if (now.hour() == 9 && now.minute() == 0) {
if (now.day() != lastRunDay) {
//////////////////////////////
if (run_one_time == 0) { // flag is clear, meaning "ok to proceed"
run_one_time = 1; // set flag to stop multiple calls...
runCycle();
lastRunDay = now.day();
} // run_one_time
//////////////////////////////
} // lastRunDay
} // now.minute
//////////////////////////////
else {
run_one_time = 0; // clear flag when time != 9:00 to allow one-time calls
}
//////////////////////////////
Hi all, I tried the first code by @sterretje and it worked and i tested it. It works fine and does what I want it to do. I will try your other ideas and experiment with it. @GolamMostafa i do not have a DS3231 RTC, only DS1307.
It would be better to start a new topic if you run into a problem adding those two devices. You can always provide a link back if you think it matters, but I doubt it will make a difference.