OK, so here is how I do it. I tried to simplify my ino files down to the autofeeder stuff. Also, you'll see I slowly added more options so there are 4 now:
1. feed once a day
2. feed twice a day
3. feed every hour during the day
4. feed on command with a memory location read to trigger the event.
Here is the relevant main code...
Code: Select all
// Vacation autofeeder memory locations
#define Vacation 100
#define VacationEnable Vacation //autofeeder on=1
#define VacationStartHr Vacation+1 //1st hour
#define VacationStartMin Vacation+2 //1st minute
#define VacationStart2ndHr Vacation+3 //2nd hour
#define VacationStart2ndMin Vacation+4 //second minute
#define FastFeed Vacation+5 //Fast feeder timer
#define VacationPulseFeed Vacation+6 //pulse signal from the internal memory function
byte Feed = 0;
void setup()
{
}
void loop()
{
//Autofeeder Mode
if ( (InternalMemory.read(VacationEnable) > 0) || (InternalMemory.read(VacationEnable) <= 2) ) // check for vacation mode 1 time a day
{
// Check if the current time is equal to the start time.
if (NumMins(hour(), minute()) == (NumMins(InternalMemory.read(VacationStartHr), InternalMemory.read(VacationStartMin))))
{
if (second() == 0)
{
Feed = 1; // Turn on feeder and feedmode
ReefAngel.FeedingModeStart();
}
}
else Feed = 0;
}
if ( InternalMemory.read(VacationEnable) == 2 ) // check for vacation mode 2 times a day
{
// Check if the current time is equal to the start time.
if (NumMins(hour(), minute()) == (NumMins(InternalMemory.read(VacationStart2ndHr), InternalMemory.read(VacationStart2ndMin))))
{
if (second() == 0)
{
Feed = 1; // Turn on feeder and feedmode
ReefAngel.FeedingModeStart();
}
}
else Feed = 0;
}
if ( InternalMemory.read(VacationEnable) == 3 ) // check for vacation mode hourly
{
if ((hour() >= 10) || (hour() <= 19)) //from 10a-7p
{
if (minute() ==30 && second() == 0) //once an hour on the half hour
{
Feed = 1; // Turn on feeder and feedmode
ReefAngel.Timer[FEEDING_TIMER].SetInterval(InternalMemory.read(FastFeed)); //short feed timer
ReefAngel.FeedingModeStart();
ReefAngel.Timer[FEEDING_TIMER].SetInterval(InternalMemory.FeedingTimer_read()); //reset original feed timer
}
else Feed = 0;
}
}
if ( InternalMemory.read(VacationPulseFeed) == 1 ) // feeder trigger for remote feeding
{
Feed = 1; // Turn on feeder and feedmode
ReefAngel.Timer[FEEDING_TIMER].SetInterval(InternalMemory.read(FastFeed)); //short feed timer
ReefAngel.FeedingModeStart();
ReefAngel.Timer[FEEDING_TIMER].SetInterval(InternalMemory.FeedingTimer_read()); //reset original feed timer
InternalMemory.write(VacationPulseFeed,0); //reset variable to 0 to mimic a pulse or button release
}
else Feed = 0;
//I/O module communication
Wire.beginTransmission(7); // transmit to device #7 (IO Module)
Wire.write('$'); // send the $$$
Wire.write('$');
Wire.write('$');
Wire.write(Feed);
Wire.endTransmission(); // stop transmitting
and here is the relevant IO code....
Code: Select all
#include <Wire.h>
#include <avr/wdt.h>
byte Feeder=0; //autofeeder signal
void setup()
{
Wire.begin(7);
Wire.onReceive(receiveEvent);
pinMode(10,OUTPUT); //autofeeder pin
digitalWrite(10,LOW);
}
void loop()
{
//feeder
if (Feeder == 0)
{
digitalWrite(10, LOW);
}
else if (Feeder == 1)
{
digitalWrite(10, HIGH);
}
}
void receiveEvent(int howMany)
{
if (howMany==4) // Our custom protocol is 4 bytes
{
byte cmd1,cmd2,cmd3,cmd4;
cmd1=Wire.read();
cmd2=Wire.read();
cmd3=Wire.read();
cmd4=Wire.read();
if (cmd1=='$' && cmd2=='$' && cmd3=='$') // the first 3 bytes of the custom protocol are $$$ to ensure it's coming from RA
{
Feeder=cmd4;
}
}
else
for (int a=0;a<howMany;a++)
Wire.read(); // if the number of bytes is not 4, discard everything
}
Hope that helps.
-Jon