Code: Select all
// Autogenerated file by RAGen (v1.2.1.158), (03/08/2012 22:54)
#include <ReefAngel_Features.h>
#include <Globals.h>
#include <RA_Wifi.h>
#include <Wire.h>
#include <OneWire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <InternalEEPROM.h>
#include <RA_NokiaLCD.h>
#include <RA_ATO.h>
#include <RA_Joystick.h>
#include <LED.h>
#include <RA_TempSensor.h>
#include <Relay.h>
#include <RA_PWM.h>
#include <Timer.h>
#include <Memory.h>
#include <InternalEEPROM.h>
#include <RA_Colors.h>
#include <RA_CustomColors.h>
#include <Salinity.h>
#include <RF.h>
#include <IO.h>
#include <ORP.h>
#include <ReefAngel.h>
#include <avr/pgmspace.h>>
//---------------------------------------------------- PWM Cloud ------------------------------------------------------------------
byte ActinicPWMValue=0;
byte DaylightPWMValue=0;
boolean ForceCloud=false;
//
//*********************************************************************************************************************************
//--------------------------------------------------- Start of Global Variables ---------------------------------------------------
// This is just how we are going to reference the PWM expansion ports within the code.
// You can change the labels if you would like, just as long as they are changed all throughout the code too.
#define LEDWhiteBlue0
#define LEDWhiteBlue1 1
#define LEDWhiteBlue2 2
#define Mixed LED 3
#define LEDMoonlights 4
#define LEDPWM5 5
// Initial values to all 6 PWM channels at startup. They will always be 0.
byte PWMChannel[]={
0,0,0,0,0,0};
//MoonPhase Value Setting
byte MoonPWMValue;
// Globals Needed for Params on Custom Main
byte x,y;
char text[10];
int v;
// Globals Needed for RF Mode on Custom Main
byte vtechmode;
boolean bFeeding=false;
//*********************************************************************************************************************************
//------------------------------------------------------ Custom Menu Code ---------------------------------------------------------
prog_char menu1_label[] PROGMEM = "Feeding";
prog_char menu2_label[] PROGMEM = "Water Change";
prog_char menu3_label[] PROGMEM = "ATO Clear";
prog_char menu4_label[] PROGMEM = "Overheat Clear";
prog_char menu5_label[] PROGMEM = "PH Calibration";
prog_char menu6_label[] PROGMEM = "Salinity Calibration";
prog_char menu7_label[] PROGMEM = "Vortech Setup";
prog_char menu8_label[] PROGMEM = "Date / Time";
prog_char menu9_label[] PROGMEM = "Version";
void MenuEntry1()
{
ReefAngel.FeedingModeStart();
}
void MenuEntry2()
{
ReefAngel.WaterChangeModeStart();
}
void MenuEntry3()
{
ReefAngel.ATOClear();
ReefAngel.DisplayMenuEntry("Clear ATO Timeout");
}
void MenuEntry4()
{
ReefAngel.OverheatClear();
ReefAngel.DisplayMenuEntry("Clear Overheat");
}
void MenuEntry5()
{
ReefAngel.SetupCalibratePH();
ReefAngel.DisplayedMenu = ALT_SCREEN_MODE;
}
void MenuEntry6()
{
ReefAngel.SetupCalibrateSalinity();
ReefAngel.DisplayedMenu = ALT_SCREEN_MODE;
}
void MenuEntry7()
{
ReefAngel.SetupCalibrateSalinity();
ReefAngel.DisplayedMenu = ALT_SCREEN_MODE;
}
void MenuEntry8()
{
ReefAngel.SetupDateTime();
ReefAngel.DisplayedMenu = ALT_SCREEN_MODE;
}
void MenuEntry9()
{
ReefAngel.DisplayVersion();
}
//------------------------------------------------------ Grouped Menu Entries ----------------------------------------------------
PROGMEM const char *menu_items[] = {
menu1_label, menu2_label, menu3_label,
menu4_label, menu5_label, menu6_label,
menu7_label,menu8_label,menu9_label
};
//---------------------------------------------------------- Custom Main ----------------------------------------------------------
void DrawCustomMain()
{
//Top Banner
ReefAngel.LCD.DrawText(COLOR_BLACK, COLOR_SKYBLUE, 9, 2, " Drop Reef ");
// Display T1 Header Text
ReefAngel.LCD.DrawText(COLOR_CORNFLOWERBLUE,255,8,14,"Sump");
// Display the T1 Temp Value
char text[7];
ConvertNumToString(text, ReefAngel.Params.Temp[T1_PROBE], 10);
ReefAngel.LCD.Clear(255, 4, 21, 37, 37);
ReefAngel.LCD.DrawLargeText(COLOR_CORNFLOWERBLUE, 255, 4, 24, text, Num8x16);
pingSerial();
// Display the T2 Header Text
ReefAngel.LCD.DrawText(COLOR_CRIMSON,255,52,14,"Room");
// Display the T2 Temp Value
ConvertNumToString(text, ReefAngel.Params.Temp[T2_PROBE], 10);
ReefAngel.LCD.Clear(255, 52, 21, 75, 37);
ReefAngel.LCD.DrawLargeText(COLOR_CRIMSON, 255, 52, 24, text, Num8x16);
pingSerial();
// Display pH Header Text
ReefAngel.LCD.DrawText(COLOR_INDIGO,255,108,14,"pH");
// Display pH Value
ConvertNumToString(text, ReefAngel.Params.PH, 100);
ReefAngel.LCD.Clear(255, 94, 21, 106, 37);
ReefAngel.LCD.DrawLargeText(COLOR_INDIGO, 255, 94, 24, text, Num8x16);
pingSerial();
// Display Vortech MP40wES Mode Header Text
ReefAngel.LCD.Clear(DefaultFGColor,5,39,127,39);
ReefAngel.LCD.DrawText(0,255,18,42,"EcoSmart Vortech");
// Display EcoSmart Mode Value
ReefAngel.LCD.Clear(255, 1, 49, 128, 64);
if (vtechmode == 0) ReefAngel.LCD.DrawLargeText(COLOR_LIMEGREEN,255,35,50,"Constant");
else if(vtechmode == 1) ReefAngel.LCD.DrawLargeText(COLOR_GOLD,255,42,50,"Lagoon");
else if (vtechmode == 2) ReefAngel.LCD.DrawLargeText(COLOR_GOLD,255,25,50,"Reef Crest");
else if (vtechmode == 3) ReefAngel.LCD.DrawLargeText(COLOR_CORNFLOWERBLUE,255,22,50,"Short Pulse");
else if (vtechmode == 4) ReefAngel.LCD.DrawLargeText(COLOR_PINK,255,25,50,"Long Pulse");
else if (vtechmode == 5) ReefAngel.LCD.DrawLargeText(COLOR_MAGENTA,255,8,50,"Nutrient Trnsp.");
else if (vtechmode == 6) ReefAngel.LCD.DrawLargeText(COLOR_MAGENTA,255,23,50,"Tidal Swell");
else if (vtechmode == 9) ReefAngel.LCD.DrawLargeText(COLOR_WHITE,0,45,50,"Night");
ReefAngel.LCD.DrawText(0,255,40,68,"Salinity");
ConvertNumToString(text, ReefAngel.Params.Salinity, 100);
ReefAngel.LCD.DrawLargeText(COLOR_LIMEGREEN, 255, 31, 78, text, Num8x8);
pingSerial();
// Display Main Relay Box
byte TempRelay = ReefAngel.Relay.RelayData;
TempRelay &= ReefAngel.Relay.RelayMaskOff;
TempRelay |= ReefAngel.Relay.RelayMaskOn;
ReefAngel.LCD.DrawOutletBox(13, 97, TempRelay);
pingSerial();
// Display Expansion Relay Box 1
TempRelay = ReefAngel.Relay.RelayDataE[0];
TempRelay &= ReefAngel.Relay.RelayMaskOffE[0];
TempRelay |= ReefAngel.Relay.RelayMaskOnE[0];
ReefAngel.LCD.DrawOutletBox(13, 109, TempRelay);
pingSerial();
//Draw Date & Time
ReefAngel.LCD.DrawDate(6, 123);
pingSerial();
}
void DrawCustomGraph() // Not Used
{
}
//*********************************************************************************************************************************
//-------------------------------------------------------- Begin Setup ------------------------------------------------------------
void setup()
{
ReefAngel.Init(); //Initialize controller
ReefAngel.InitMenu(pgm_read_word(&(menu_items[0])),SIZE(menu_items));
ReefAngel.SetTemperatureUnit(1);
ReefAngel.FeedingModePorts = 0;
ReefAngel.WaterChangePorts = Port1Bit | Port2Bit | Port3Bit | Port6Bit;
ReefAngel.OverheatShutoffPorts = Port6Bit;
ReefAngel.LightsOnPorts = 0;
//---------------------------------------------------- Ports that are always on ----------------------------------------------------
ReefAngel.Relay.On(Port1);
ReefAngel.Relay.On(Port4);
ReefAngel.Relay.On(Port7);
ReefAngel.Relay.On(Port8);
ReefAngel.Relay.On(Box1_Port1);
ReefAngel.Relay.On(Box1_Port2);
ReefAngel.Relay.On(Box1_Port6);
ReefAngel.Relay.On(Box1_Port7);
ReefAngel.Relay.On(Box1_Port8);
//--------------------------------------------------------- RF Module Setup -------------------------------------------------------
ReefAngel.RF.SetMode(Slave_Start,0,0);
/*
If you get a compile error similar to this:
'class ReefAngelClass' has no member named 'RF'
Please make sure that you enabled RF Expansion on your features file.
Open RAGen and make sure you have RF Expansion checked under the Features tab.
Or, you can manually edit the file.
The file is located at "Documents\Arduino\libraries\ReefAngel_Features.h" file and has to include this line in it:
#define RFEXPANSION
*/
InternalMemory.RFMode_write(0);
InternalMemory.RFSpeed_write(128);
InternalMemory.RFDuration_write(10);
}
//*********************************************************************************************************************************
//--------------------------------------------------------- Begin Loop ------------------------------------------------------------
void loop()
{
//------------------------------------------------------ Specific functions -------------------------------------------------------
ReefAngel.Relay.DelayedOn(Port2, 1);
ReefAngel.StandardATO(Port3);
ReefAngel.MoonLights(Port5);
ReefAngel.StandardLights(Box1_Port6);
ReefAngel.StandardLights(Box1_Port7,120);
ReefAngel.MHLights(Box1_Port8);
ReefAngel.StandardHeater(Port6);
ReefAngel.Portal("JNieuwenhuizen");
ReefAngel.ShowInterface();
ReefAngel.Refresh();
//------------------------------------------------ Start PWM Expansion Code for Slope ----------------------------------------------
PWMChannel[LEDWhiteBlue]=PWMSlope(
InternalMemory.StdLightsOnHour_read(),
InternalMemory.StdLightsOnMinute_read(),
InternalMemory.StdLightsOffHour_read(),
InternalMemory.StdLightsOffMinute_read(),
InternalMemory.PWMSlopeStartD_read(),
InternalMemory.PWMSlopeEndD_read(),
InternalMemory.PWMSlopeDurationD_read(),
0);
PWMChannel[Mixed LED]=PWMSlope(
InternalMemory.StdLightsOnHour_read(),
InternalMemory.StdLightsOnMinute_read(),
InternalMemory.StdLightsOffHour_read(),
InternalMemory.StdLightsOffMinute_read(),
InternalMemory.PWMSlopeStartA_read(),
InternalMemory.PWMSlopeEndA_read(),
InternalMemory.PWMSlopeDurationA_read(),
0);
ReefAngel.PWM.SetChannel(0,PWMChannel[LEDWhiteBlue]);
ReefAngel.PWM.SetChannel(1,PWMChannel[LEDWhiteBlue1]);
ReefAngel.PWM.SetChannel(2,PWMChannel[LEDWhiteBlue2]);
ReefAngel.PWM.SetChannel(3,PWMChannel[Mixed LED]);
ReefAngel.PWM.SetChannel(4,MoonPhase());
ReefAngel.ShowInterface();
MoonPWMValue=MoonPhase();
if (hour()==22 || hour()==6)
MoonPWMValue=PWMSlope(22,00,7,00,0,MoonPhase(),60,0);
if (ReefAngel.PWM.ExpansionChannel[0]) MoonPWMValue=0;
ReefAngel.PWM.SetChannel(4,MoonPWMValue);
// PWMChannel[LEDWhiteBlue1]=PWMSlope(15,0,21,30,15,45,90,PWMChannel[LEDWhiteBlue1]);
PWMChannel[LEDWhiteBlue]=PWMSlope(8,0,22,0,0,90,300,PWMChannel[LEDWhiteBlue]);
PWMChannel[LEDWhiteBlue1]=PWMSlope(8,30,21,30,0,90,270,PWMChannel[LEDWhiteBlue1]);
PWMChannel[LEDWhiteBlue2]=PWMSlope(9,0,21,0,0,90,240,PWMChannel[LEDWhiteBlue2]);
PWMChannel[Mixed LED]=PWMSlope(12,0,18,0,0,90,60,PWMChannel[Mixed LED]);
// In the example above, we are starting the slope at 8:00am with 0% and going up to 90% within 300 minutes, which would be 5:00pm.
// Then it would stay at 90% from 13:00pm to 300 minutes prior to 10:00pm, which would be 5:00pm.
// Then from 8:00pm, it would start sloping down from 45% all the way back to 15% within 90 minutes, which would be 9:30pm.
//CheckCloud(); (May be used at a later date with cloud/storm effects)
if (hour()>=22 || hour()<8)
{
ReefAngel.PWM.Set(MoonPhase()); //Moon phase schedule between 10:00pm - 7:00am
}
else
ReefAngel.PWM.Expansion(LEDWhiteBlue,int(PWMChannel[LEDWhiteBlue]));
ReefAngel.PWM.Expansion(LEDWhiteBlue1,int(PWMChannel[LEDWhiteBlue1]));
ReefAngel.PWM.Expansion(LEDWhiteBlue2,int(PWMChannel[LEDWhiteBlue2]));
ReefAngel.PWM.Expansion(Mixed LED,int(PWMChannel[Mixed LED]));
ReefAngel.ShowInterface();
//------------------------------------------------- End PWM Expansion Code for Slope ----------------------------------------------
//------------------------------------------------ Start Time-of-Day Based Functions ----------------------------------------------
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ These are the mode numbers for the RF Expansion Module for reference ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//#define Constant 0
//#define Random1 1 // Lagoonal
//#define Random2 2 // Reef Crest
//#define ShortWave 3
//#define LongWave 4
//#define Smart_NTM 5 // Nutrient Transport Mode
//#define Smart_TSM 6 // Tidal Swell Mode
//#define Feeding_Start 7
//#define Feeding_Stop 8
//#define Night 9
//#define Slave_Start 97
//#define Slave_Stop 98
//#define None 99
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//------------------------------------------------ Start Feeding Mode Schedule ----------------------------------------------------
// if the hour is 7pm, minute is 30 and seconds is 0
// start the feeding mode
if ( ((hour() == 17) &&
(minute() == 30) &&
(second() == 0) ) )
{
ReefAngel.FeedingModeStart();
vtechmode = InternalMemory.RFMode_read();
}
//-------------------------------------------------------- Start RF Daytime Control -----------------------------------------------
if (hour() >=8 && hour() <= 22)
{
if (ReefAngel.DisplayedMenu==FEEDING_MODE) bFeeding=true;
if (ReefAngel.DisplayedMenu==DEFAULT_MENU && bFeeding )
{
bFeeding=false;
ReefAngel.RF.UseMemory=false;
ReefAngel.RF.SetMode(Smart_NTM,155,5);
ReefAngel.Timer[4].SetInterval(1800); // Timer for 30min
ReefAngel.Timer[4].Start();
vtechmode = 5;
}
if (ReefAngel.DisplayedMenu==DEFAULT_MENU && ReefAngel.Timer[4].IsTriggered())
{
ReefAngel.RF.UseMemory=true;
vtechmode = InternalMemory.RFMode_read();
}
}
//-------------------------------------------------------- Start RF Nightmode Control ---------------------------------------------
if (hour()>=22 || hour()<8) // Defining "Nightmode" hours for VorTech = between 10 PM and 8 AM
{
ReefAngel.RF.UseMemory=false;
ReefAngel.RF.SetMode(Night,15,0);
vtechmode = 9;
}
else
{
ReefAngel.RF.SetMode(Feeding_Stop,0,0); //Temp fix for coming out of Night mode
ReefAngel.RF.UseMemory=true;
vtechmode = InternalMemory.RFMode_read();
}
// Cloud & Lightning effects - Calculate your regular sunrise/sunset PWM value
// From Roberto - THANKS ROBERTO!
// byte PWMSlope(byte startHour, byte startMinute, byte endHour, byte endMinute, byte startPWM, byte endPWM, byte Duration, byte oldValue)
if (hour()>=22 || hour()<8)
{
ReefAngel.PWM.SetActinic(MoonPhase()); //Moon phase schedule between 10:00pm - 7:00am
}
else
{
ActinicPWMValue=PWMSlope(12,00,19,0,0,90,150,ActinicPWMValue); //Actinic turn on at 7am, off at 10am, and ramp up 0% to 50% PWM in 60 minutes
//ActinicPWMValue=PWMSlope(16,00,19,0,0,50,60,ActinicPWMValue); //Actinic turn on at 4pm, off at 7pm, and ramp up 0% to 50% PWM in 60 minutes
ReefAngel.PWM.SetActinic(ActinicPWMValue);
}
DaylightPWMValue=PWMSlope(8,00,22,0,0,90,240,DaylightPWMValue); //Daylight turn on at 9am, off at 5pm, and ramp up 0% to 50% PWM in 60 minutes
CheckCloud();
ReefAngel.PWM.SetDaylight(DaylightPWMValue);
}
//*********************************************************************************************************************************
// Random Cloud/Thunderstorm effects function
// From Roberto - THANKS ROBERTO!
void CheckCloud()
{
// ------------------------------------------------------------
// Change the values below to customize your cloud/storm effect
// Frequency in days based on the day of the month - number 2 means every 2 days, for example (day 2,4,6 etc)
// For testing purposes, you can use 1 and cause the cloud to occur everyday
#define Clouds_Every_5_Days 1
// Percentage chance of a cloud happening today
// For testing purposes, you can use 100 and cause the cloud to have 100% chance of happening
#define Cloud_Chance_per_Day 25
// Minimum number of minutes for cloud duration. Don't use max duration of less than 6
#define Min_Cloud_Duration 7
// Maximum number of minutes for the cloud duration. Don't use max duration of more than 255
#define Max_Cloud_Duration 15
// Minimum number of clouds that can happen per day
#define Min_Clouds_per_Day 3
// Maximum number of clouds that can happen per day
#define Max_Clouds_per_Day 6
// Only start the cloud effect after this setting
// In this example, start could after 11:30am
#define Start_Cloud_After NumMins(11,30)
// Always end the cloud effect before this setting
// In this example, end could before 4:30pm
#define End_Cloud_Before NumMins(20,0)
// Percentage chance of a lightning happen for every cloud
// For testing purposes, you can use 100 and cause the lightning to have 100% chance of happening
#define Lightning_Change_per_Cloud 25
// Note: Make sure to choose correct values that will work within your PWMSLope settings.
// For example, in our case, we could have a max of 5 clouds per day and they could last for 50 minutes.
// Which could mean 250 minutes of clouds. We need to make sure the PWMSlope can accomodate 250 minutes of effects or unforseen result could happen.
// Also, make sure that you can fit double those minutes between Start_Cloud_After and End_Cloud_Before.
// In our example, we have 510 minutes between Start_Cloud_After and End_Cloud_Before, so double the 250 minutes (or 500 minutes) can fit in that 510 minutes window.
// It's a tight fit, but it did.
//#define printdebug // Uncomment this for debug print on Serial Monitor window
#define forcecloudcalculation // Uncomment this to force the cloud calculation to happen in the boot process.
// Change the values above to customize your cloud/storm effect
// ------------------------------------------------------------
// Do not change anything below here
static byte cloudchance=255;
static byte cloudduration=0;
static int cloudstart=0;
static byte numclouds=0;
static byte lightningchance=0;
static byte cloudindex=0;
static byte lightningstatus=0;
static int LastNumMins=0;
// Every day at midnight, we check for chance of cloud happening today
if (hour()==0 && minute()==0 && second()==0) cloudchance=255;
#ifdef forcecloudcalculation
if (cloudchance==255)
#else
if (hour()==0 && minute()==0 && second()==1 && cloudchance==255)
#endif
{
//Pick a random number between 0 and 99
cloudchance=random(26);
// if picked number is greater than Cloud_Chance_per_Day, we will not have clouds today
if (cloudchance>Cloud_Chance_per_Day) cloudchance=0;
// Check if today is day for clouds.
if ((day()%Clouds_Every_5_Days)!=0) cloudchance=0;
// If we have cloud today
if (cloudchance)
{
// pick a random number for number of clouds between Min_Clouds_per_Day and Max_Clouds_per_Day
numclouds=random(Min_Clouds_per_Day,Max_Clouds_per_Day);
// pick the time that the first cloud will start
// the range is calculated between Start_Cloud_After and the even distribuition of clouds on this day.
cloudstart=random(Start_Cloud_After,Start_Cloud_After+((End_Cloud_Before-Start_Cloud_After)/(numclouds*2)));
// pick a random number for the cloud duration of first cloud.
cloudduration=random(Min_Cloud_Duration,Max_Cloud_Duration);
//Pick a random number between 0 and 99
lightningchance=random(36);
// if picked number is greater than Lightning_Change_per_Cloud, we will not have lightning today
if (lightningchance>Lightning_Change_per_Cloud) lightningchance=0;
}
}
// Now that we have all the parameters for the cloud, let's create the effect
if (cloudchance)
{
//is it time for cloud yet?
if (NumMins(hour(),minute())>=cloudstart && NumMins(hour(),minute())<(cloudstart+cloudduration))
{
DaylightPWMValue=ReversePWMSlope(cloudstart,cloudstart+cloudduration,DaylightPWMValue,0,180);
if (lightningchance && (NumMins(hour(),minute())==(cloudstart+(cloudduration/2))) && second()<5)
{
if (random(100)<20) lightningstatus=1;
else lightningstatus=0;
if (lightningstatus)
{
DaylightPWMValue=100;
ActinicPWMValue=100;
}
else
{
DaylightPWMValue=0;
ActinicPWMValue=0;
}
delay(1);
}
}
if (NumMins(hour(),minute())>(cloudstart+cloudduration))
{
cloudindex++;
if (cloudindex < numclouds)
{
cloudstart=random(Start_Cloud_After+(((End_Cloud_Before-Start_Cloud_After)/(numclouds*2))*cloudindex*2),(Start_Cloud_After+(((End_Cloud_Before-Start_Cloud_After)/(numclouds*2))*cloudindex*2))+((End_Cloud_Before-Start_Cloud_After)/(numclouds*2)));
// pick a random number for the cloud duration of first cloud.
cloudduration=random(Min_Cloud_Duration,Max_Cloud_Duration);
//Pick a random number between 0 and 99
lightningchance=random(54);
// if picked number is greater than Lightning_Change_per_Cloud, we will not have lightning today
if (lightningchance>Lightning_Change_per_Cloud) lightningchance=0;
}
}
}
byte ReversePWMSlope(long cstart,long cend,byte PWMStart,byte PWMEnd, byte clength)
{
long n=elapsedSecsToday(now());
cstart*=60;
cend*=60;
if (n<cstart) return PWMStart;
if (n>=cstart && n<=(cstart+clength)) return map(n,cstart,cstart+clength,PWMStart,PWMEnd);
if (n>(cstart+clength) && n<(cend-clength)) return PWMEnd;
if (n>=(cend-clength) && n<=cend) return map(n,cend-clength,cend,PWMEnd,PWMStart);
if (n>cend) return PWMStart;
//End Cloud & Lighting
}
}
//*********************************************************************************************************************************