Hello,
I am making a automatic fish feeder with a RTC clock module.
At the right time a servo turns and puts some food in.
I have got the main part of the code working, but when the time for feeding comes it doesn't do anything.
#include <Wire.h>
#include <RTClib.h>
long byteRead;
RTC_DS1307 RTC;
void setup () {
Serial.begin(9600);
Wire.begin();
RTC.begin();;
RTC.adjust(DateTime(__DATE__, __TIME__));
if (! RTC.isrunning()) {
Serial.println("RTC is running!");
}
}
void loop () {
DateTime now = RTC.now();
Serial.print(now.hour(), DEC);
Serial.print(now.minute(), DEC);
Serial.print(now.second(), DEC);
if (Serial.available()) {
byteRead = Serial.read();
}
if (byteRead == (8300)){ //Here is the bit that isn't working.
Serial.print("feed"); //It should print "feed" at 8:30am at the moment. In this case the first digit is the time in 24 hours,
delay(10); //the second two is the minuite, and the last one is the seconds.
} //I've changed it to the right time for testing
Serial.println();
delay(1000);
}
Your question has the great advantage that your code is short.
As @PaulS has said, why you you think the time would come from the Serial port - espcially after you have just obtained it with RTC.now().
I don't know how the hour and minute values are stored but something like this seems appropriate
if (now.hour == 8 && now.minute == 30) {
if (feedHasBeenGiven == false) {
Serial.print("feed");
feedHasBeenGiven = true;
delay(10); //the second two is the minuite, and the last one is the seconds.
}
}
if (now.hour == 8 && now.minute == 31) {
feedHasBeenGiven = false; // ready for the next time
}
Bear in mind that that time will exist for a full minute which is an awful lot of repeats of loop(). You will need a variable to record that the feed has been given
...R
Edit to add -- why do I always see the typos the instant AFTER I press send ???
The RTClib has a nice little feature to convert between real time and unix epoch time. Convert real time into epoch time so the alarm time is one long integer number in epoch time. Compare with current time. If it passed the alarm time and nothing has been done yet, then do your thing and set a flag saying it's doing it. Then when the thing completes, set the flag to not done yet again and wait for the next alarm time.
Sorry, I haven't really worded my problem very well. All I need is it to print "feed" whenever the number 8300 is printed in the serial monitor. I have never really used variables and might make a few mistakes.
Thanks heaps