Need someone to review my code

Share you PDE file with our community
Post Reply
sceia
Posts: 30
Joined: Mon Nov 14, 2011 8:06 pm

Need someone to review my code

Post by sceia »

I think I have this about correct, however I was having problems with my custom menu working. Can someone go over this and see where I'm failing?

Scott.

Code: Select all

#include <ReefAngel_Features.h>
#include <ReefAngel_Globals.h>
#include <ReefAngel_Wifi.h>
#include <Wire.h>
#include <OneWire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <ReefAngel_EEPROM.h>
#include <ReefAngel_NokiaLCD.h>
#include <ReefAngel_ATO.h>
#include <ReefAngel_Joystick.h>
#include <ReefAngel_LED.h>
#include <ReefAngel_TempSensor.h>
#include <ReefAngel_Relay.h>
#include <ReefAngel_PWM.h>
#include <ReefAngel_Timer.h>
#include <ReefAngel_Memory.h>
#include <ReefAngel.h>


#define AutoTopOff          1 
#define Refuge              2 
#define MHLight             3 
#define Actinic             4 
#define WaveController      5 
#define Heater              6 
#define Fans                7 
#define Pumps               8 


#define CUSTOM_MENU
#define CUSTOM_MENU_ENTRIES 

boolean wmdelay=false;

#include <avr/pgmspace.h>
// Create the menu entries
prog_char menu1_label[] PROGMEM = "Feed Fish";
prog_char menu2_label[] PROGMEM = "Feed Coral";
prog_char menu3_label[] PROGMEM = "Water Change";
prog_char menu4_label[] PROGMEM = "All Off";
prog_char menu5_label[] PROGMEM = "Daylights";
prog_char menu6_label[] PROGMEM = "Moonights";
prog_char menu7_label[] PROGMEM = "ATO/OH Clear";
prog_char menu8_label[] PROGMEM = "PH Calibration";
prog_char menu9_label[] PROGMEM = "Date / Time";

// Group the menu entries together
PROGMEM const char *menu_items[] = {
menu1_label, 
menu2_label, 
menu3_label,
menu4_label, 
menu5_label, 
menu6_label,
menu7_label,
menu8_label, 
menu9_label
};


void MenuEntry1() //"Feed Fish"
{
  ReefAngel.FeedingModeStart();
}
void MenuEntry2() //"Feed Coral"
{
  ReefAngel.Relay.Off(Pumps);
  ReefAngel.Relay.Write();  // Make relay changes effective
  delay(300000);
  ReefAngel.Relay.Off(WaveController);
  ReefAngel.Relay.Write();  // Make relay changes effective
  delay(300000);
  ReefAngel.Relay.On(Pumps)
  ReefAngel.Relay.On(WaveController);
  ReefAngel.Relay.Write();  // Make relay changes effective
}
void MenuEntry3() //"Water Change"
{
  ReefAngel.WaterChangeModeStart();
}
void MenuEntry4() //"All Off"
{
   ReefAngel.Relay.AllOff(); // Turn all ports off.
   ReefAngel.Relay.Write();  // Make relay changes effective
   ReefAngel.DisplayedMenu = ALT_SCREEN_MODE; 
}
void MenuEntry5() //"Toggle Daylights"
{
  ReefAngel.Relay.Toggle(MHLight);
  ReefAngel.Relay.Toggle(Actinic);
  ReefAngel.Relay.Write();
  ReefAngel.DisplayedMenu = ALT_SCREEN_MODE;
}
void MenuEntry6()  //"Toggle Moonights"
{
  ReefAngel.Relay.Toggle(Refuge);
  ReefAngel.Relay.Write();  // Make relay changes effective 
  ReefAngel.DisplayedMenu = ALT_SCREEN_MODE;
}
void MenuEntry7() //"ATO/OH Clear"
{
  ReefAngel.ATOClear();
  ReefAngel.OverheatClear();
  ReefAngel.DisplayMenuEntry("Clear ATO/Overheat");
}

void MenuEntry8() //"PH Calibration"
{
  ReefAngel.SetupCalibratePH();
  ReefAngel.DisplayedMenu = ALT_SCREEN_MODE;
}

void MenuEntry9() //"Date / Time"
{
  ReefAngel.SetupDateTime();
  ReefAngel.DisplayedMenu = ALT_SCREEN_MODE;
}

void setup() 
{ 
  //Init and custom menus
  ReefAngel.Init();  //Initialize controller
  ReefAngel.InitMenu(pgm_read_word(&(menu_items[0])),SIZE(menu_items));

  //Menu Modes 
  ReefAngel.FeedingModePorts = B00110000;
  ReefAngel.WaterChangePorts = B10110000;
  ReefAngel.OverheatShutoffPorts = B00000111;
  ReefAngel.LightsOnPorts = B00001100;
  
  //Initialize timer
  ReefAngel.Timer[1].SetInterval(InternalMemory.WM1Timer_read());
  ReefAngel.Timer[1].Start();
  
  //Pumps and waves on
  ReefAngel.Relay.On(Pumps);  //Turn Sump/Skimmer on at startup
  ReefAngel.Relay.On(WaveController);  //Turn WaveController on at startup
}

void loop() 
{ 
  
  
  ReefAngel.LCD.SetContrast(60);  // Set contrast to 60
  
  // Wave controller controls 3 powerheads plugged into 1 RA port.  Night time (10p-8a) is a Lull
  // cycle.  Controller runs for 60s then is off for 20s.
  if (ReefAngel.Timer[1].IsTriggered() )
  {
    if ((hour() >= 22) || (hour() <= 8)) //from 10p-8a  
    {
      if (wmdelay)
      {
        ReefAngel.Timer[1].SetInterval(60);  // wm night delay
        ReefAngel.Timer[1].Start();
        ReefAngel.Relay.Off(WaveController);
        wmdelay=false;
      }
      else
      {
        ReefAngel.Timer[1].SetInterval(20);  // short wave
        ReefAngel.Timer[1].Start();
        ReefAngel.Relay.On(WaveController);
        wmdelay=true;
      }
    }
    else
    {
      //8a-10p normal wave settings
      ReefAngel.Timer[1].SetInterval(InternalMemory.WM1Timer_read());
      ReefAngel.Timer[1].Start();
      ReefAngel.Relay.On(WaveController);
    }
  }
  
  //Top Off
  ReefAngel.StandardATO(AutoTopOff,60);  //Setup AutoTopOff as Auto Top-Off function with 60s timeout
  
  //Light Schedule  
  ReefAngel.StandardLights(Refuge,22,00,6,00);  //Refugium schedule 10:00pm - 6:00am   
  ReefAngel.StandardLights(Actinic,12,00,22,00);  //Actinic schedule 12:00pm - 10:00pm
  ReefAngel.MHLights(MHLight,14,0,20,0,15);  //Daylight schedule 2:00pm - 8:00pm with 15min cool down
  
  //Heating/Cooling
  ReefAngel.StandardHeater(Heater,778,782);  // Setup Heater to turn on at 77.8F and off at 78.2F 
  ReefAngel.StandardFan(Fans,783,900);  // Setup Chiller to turn on at 78.3F and off at 90.0F 
  
  ReefAngel.ShowInterface();
  
  ReefAngel.Relay.Write();  // Make relay changes effective 
}
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: Need someone to review my code

Post by rimai »

I'll take a look at it tonight.
Roberto.
sceia
Posts: 30
Joined: Mon Nov 14, 2011 8:06 pm

Re: Need someone to review my code

Post by sceia »

rimai wrote:I'll take a look at it tonight.
Thanks! I also tried a coral feeding routine which seems to reboot the RA...lol.

Here's the thread link...
http://forum.reefangel.com/viewtopic.php?f=12&t=443
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Need someone to review my code

Post by binder »

Here's some observations I've made on your code...

Code: Select all

#define CUSTOM_MENU_ENTRIES
Needs a number after entries. So based on your code, it should be

Code: Select all

#define CUSTOM_MENU_ENTRIES 9
Both CUSTOM_MENU and CUSTOM_MENU_ENTRIES defines need to be in the ReefAngel_Features.h file or else they won't work properly.

Code: Select all

  ReefAngel.LCD.SetContrast(60);  // Set contrast to 60
This may only be needed in the setup() function and not in the loop() function.

shouldn't this be "toggle"? Otherwise the port is always on during the day which is probably not what you want.

Code: Select all

    else
    {
      //8a-10p normal wave settings
      ReefAngel.Timer[1].SetInterval(InternalMemory.WM1Timer_read());
      ReefAngel.Timer[1].Start();
	  // should be ReefAngel.Relay.Toggle(WaveController);
      ReefAngel.Relay.On(WaveController);
    }
There may need to be another way to handle this when you are exiting the "night mode". I haven't looked into it much more than that and I know other discussions are being done on this topic (it may include you, but I just don't recall off hand).

This section, the Relay.write() isn't necessary because that's called at the end of ShowInterface();

Code: Select all

  ReefAngel.ShowInterface();
  
  // remove this next line
  ReefAngel.Relay.Write();  // Make relay changes effective 
Otherwise, the times look proper for the light schedule. The temps look good for your chiller & heaters. The chiller comment should probably read "Turn on at 90.0 and turn off at 78.3" but that's trivial.

The menu entries look appropriate. You may want to look at the topic sceia mentioned for the feeding corals entry. It may make things a little more understandable as to what/where you are in the modes plus it will give you a chance to exit out of the feeding mode early if needed. Your method of using the delay function will render the controller unresponsive the entire time it is delaying and you won't be able to exit the mode until the delay is up.

That's all the comments I've got. Otherwise looking good. Hope these suggestions help.

curt
sceia
Posts: 30
Joined: Mon Nov 14, 2011 8:06 pm

Re: Need someone to review my code

Post by sceia »

Thanks!.... I am sceia and the original poster of both threads... ;)

The wave controller I want on all day and toggle at night. I'm running an independent 3 powerhead wave controller plugged into the "wavecontroller" port. This should allow me to run more flow during the day, and put a 1min delay at night. I did the clean up things you said...thanks! I still am getting an error with the size from the thread you posted on the other topic...
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Need someone to review my code

Post by binder »

sceia wrote:Thanks!.... I am sceia and the original poster of both threads... ;)
Oops. My bad. I wasn't even paying attention to the posters....more focused on helping with the code and getting things working. It's been a long day. :)
I still am getting an error with the size from the thread you posted on the other topic...
I just posted some more info on the other thread with a suggestion to save some code.

curt
Post Reply