mbasile:
This works for multiple feedings, but only at times where the hour just turned.. AKA 9 and 6 or 10 and 5... etc. anyway to make it feed at say 10 and 530, or is that just way over my head.....
Yes, it seems so...
You don't need seconds unless your pet needs to be fed at EXACTLY 17:30:42, so don't go there.
Stop arbitrarily deleting curly braces, they are there for a very important reason, for which you need to learn somehow.
I'm introducing a struct to your little program here, where you will use the struct to contain the Hour and the Minute of your times. Take a look and see if you understand.
struct FeedTime{
int hour, minute;
};
FeedTime amFeed = {10, 0}; // i.e. 10:00am // don't do anything silly like use two zeroes for the time here!
FeedTime pmFeed = {17,30}; // i.e. 5:30pm
if((currentTime.minute != lastMinute) && (((currentTime.hour == amFeed.hour) && (currentTime.minute == amFeed.minute)) || ((currentTime.hour == pmFeed.hour) && (currentTime.minute == pmFeed.minute))))
While I cannot test this, I would think that it works. It says in english:
Evaluate the current minute to determine if it just changed. -> e.g. if(currentTime.minute != lastMinute)
If so, evaluate to determine if it is either the amFeed time or the pmFeed time.
if either of these conditions is so, run the fedPet() function
I hope this gets you there:
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal.h>
#include <TimeAlarms.h>
#include <Time.h>
RTC_DS1307 RTC;
LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);
int motorPin = 11;
int lastHour = 24;
int lastMinute = 59;
int lastSecond = 59;
bool feedPet = false;
unsigned long feedTime = 0;
unsigned long lastPrintTime = 0;
struct FeedTime{
int hour, minute;
};
FeedTime amFeed = {10, 0}; // i.e. 10:00am // don't do anything silly like use two zeroes for the time here!
FeedTime pmFeed = {17,30}; // i.e. 5:30pm
void setup ()
{
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
lcd.begin(16, 2);
Wire.begin();
RTC.begin();
if (! RTC.isrunning())
{
lcd.println("RTC NOT Running!");
RTC.adjust(DateTime((__DATE__), (__TIME__)));
}
}
void loop ()
{
DateTime now = RTC.now();
FeedTime currentTime;
currentTime.hour = now.hour();
currentTime.minute = now.minute();
if((currentTime.minute != lastMinute) && (((currentTime.hour == amFeed.hour) && (currentTime.minute == amFeed.minute)) || ((currentTime.hour == pmFeed.hour) && (currentTime.minute == pmFeed.minute))))
{
feedTime = millis();
feedPet = true;
lastHour = currentTime.hour;
}
if (feedPet)
{
turnFeeder();
}
if (millis() - lastPrintTime > 1000UL)
{
lcd.setCursor(0, 0);
char nowDate[24] = "";
sprintf(nowDate, "DATE: %02d/%02d/%d", now.month(), now.day(), now.year());
lcd.print(nowDate);
// display the time
lcd.setCursor(0, 1);
char nowTime[24] = "";
sprintf(nowTime, "Time: %02d:%02d:%02d", now.hour(), now.minute(), now.second());
lcd.print(nowTime);
lastPrintTime = millis();
}
}
void turnFeeder(void)
{
static bool pinState = true;
if (millis() - feedTime < 2000UL) // 2 second(s)
{
if (pinState)
{
digitalWrite(motorPin, HIGH);
pinState = false;
}
}
else
{
digitalWrite(motorPin, LOW);
pinState = true;
feedPet = false;
}
}
I can't test it but you can...