Another Rookie PDE

Share you PDE file with our community
Post Reply
peitro
Posts: 13
Joined: Sat Oct 22, 2011 5:19 pm

Another Rookie PDE

Post by peitro »

Hoping this might help another newbie, here is my latest PDE file that I am running. Started with RAGen, but modified a few things to suit my needs:

Moonlight control, ramp up, then back down
Random wavemaker switching times
Implemented web banner (code from RAGen did not seem to work for me)
Failsafe heater shut off (polls 2 out of 3 temps over limit) relies on heater thermostat to maintain temperature.

Feel free to comment on how to improve upon this code. I haven't written my own code in several years!

Enjoy.
Peitro

Code: Select all

// Autogenerated file by RAGen (v1.1.0.127), (10/22/2011 09:08)
// Modified to add moonlight control, light control, and random pump switching 
// RA_102111_0957.pde
//
// This version designed for v0.8.5 Beta 17 or later

/* The following features are enabled for this PDE File: 
#define DisplayImages
#define WavemakerSetup
#define DateTimeSetup
#define VersionMenu
#define ATOSetup
#define DirectTempSensor
#define DisplayLEDPWM
#define wifi
#define StandardLightSetup
#define SaveRelayState
#define WDT
*/


#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>

#include <avr/pgmspace.h>
// Labels for the web banner
prog_char id_label[] PROGMEM = "peitro";
prog_char probe1_label[] PROGMEM = "Sump";
prog_char probe2_label[] PROGMEM = "Refugium";
prog_char probe3_label[] PROGMEM = "Room";
prog_char relay1_label[] PROGMEM = "ATO";
prog_char relay2_label[] PROGMEM = "LED%20Main";
prog_char relay3_label[] PROGMEM = "LED%20Strip";
prog_char relay4_label[] PROGMEM = "Powerhead%202";
prog_char relay5_label[] PROGMEM = "Powerhead%201";
prog_char relay6_label[] PROGMEM = "Powerhead%203";
prog_char relay7_label[] PROGMEM = "Heater";
prog_char relay8_label[] PROGMEM = "Skimmer";
PROGMEM const char *webbanner_items[] = {
    id_label, probe1_label, probe2_label, probe3_label, relay1_label, relay2_label,
	relay3_label, relay4_label, relay5_label, relay6_label, relay7_label, relay8_label};


void setup()
{
    ReefAngel.Init();  //Initialize controller
    // Initialize and start the web banner timer
    ReefAngel.LoadWebBanner(pgm_read_word(&(webbanner_items[0])), SIZE(webbanner_items));
    ReefAngel.Timer[4].SetInterval(180);  // set interval to 180 seconds
    ReefAngel.Timer[4].Start();
    ReefAngel.OverheatShutoffPorts = B01000100;
   
    // random timer for wavemaker
    {
       randomSeed(analogRead(0));
        ReefAngel.Init();  //Initialize controller   
        ReefAngel.Timer[1].SetInterval(random(15,35));
        ReefAngel.Timer[1].Start();
        ReefAngel.Relay.On(Port5);
        ReefAngel.Relay.Off(Port4);
    }
}

void loop()
{
    // Specific functions
    ReefAngel.StandardATO(Port1);
    //turn on main light
    ReefAngel.StandardLights(2,10,00,17,00);
    //turn on strip light 1 hr b4 main
    ReefAngel.StandardLights(3,9,00,18,00);
    //ReefAngel.Wavemaker1(Port4);
    //ReefAngel.Wavemaker2(Port5);
        // Ports that are always on
    ReefAngel.Relay.On(Port6);
    ReefAngel.Relay.On(Port8);
    //ReefAngel.StandardHeater(Port7);
    // Turn off Port 7 when the temp of 2 of T1, T2, T3 gets above 82.0 degrees 
    int TFlag1, TFlag2, TFlag3;
    if ( ReefAngel.Params.Temp1 >= 820 )
        {
        TFlag1 = 1;
      }
      else {
        TFlag1 = 0;
      }
    if ( ReefAngel.Params.Temp2 >= 820 )
        {
        TFlag2 = 1;
      }
      else {
        TFlag2 = 0;
      }
    if ( ReefAngel.Params.Temp3 >= 820 )
        {
        TFlag3 = 1;
      }
      else {
        TFlag3 = 0;
      }      
    if (( TFlag1 + TFlag2 + TFlag3 > 1 )) ReefAngel.Relay.Off(Port7);
      else ReefAngel.Relay.On(Port7);

   // Turn on moon lights based at different levels 7pm and 7am.
    if (hour()>19 || hour()<7)
    {
      if (hour()>19 || hour()<21){
      ReefAngel.PWM.SetActinic(100);
      ReefAngel.PWM.SetDaylight(100);
      }
      if (hour()>=21 || hour()<23){
      ReefAngel.PWM.SetActinic(60);
      ReefAngel.PWM.SetDaylight(80);
      }
      if (hour()>=23 || hour()<1){
      ReefAngel.PWM.SetActinic(25);
      ReefAngel.PWM.SetDaylight(50);
      }
      if (hour()>=1 || hour()<5){
      ReefAngel.PWM.SetActinic(80);
      ReefAngel.PWM.SetDaylight(60);
      }
      if (hour()>=5 || hour()<7){
      ReefAngel.PWM.SetActinic(100);
      ReefAngel.PWM.SetDaylight(100);
      }
        } else {
      ReefAngel.PWM.SetActinic(0);
      ReefAngel.PWM.SetDaylight(0);
        }
        
        // wavemaker code
        
        ReefAngel.ShowInterface();

    if ( ReefAngel.Timer[1].IsTriggered() )
          {
            ReefAngel.Timer[1].SetInterval(random(15,35));
            ReefAngel.Timer[1].Start();
            ReefAngel.Relay.Toggle(Port5);
            ReefAngel.Relay.Toggle(Port4);
           }

    // Web Banner stuff
    if(ReefAngel.Timer[4].IsTriggered())
    {
        ReefAngel.Timer[4].Start();
        ReefAngel.WebBanner();
    }

	ReefAngel.ShowInterface();
}
peitro
Posts: 13
Joined: Sat Oct 22, 2011 5:19 pm

Re: Another Rookie PDE

Post by peitro »

Well it looks like the Moonlight section isn't working. They are either on 100% or off. Can someone help me with my nested if statement?
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Another Rookie PDE

Post by binder »

You have several things that needed cleaned up with the code. I cleaned things up and matched up your curly braces and improved somethings for you. You had 2 calls to ShowInterface. You also had the always on ports in the loop() section which they don't need to be.

Also, you did have problems with the moonlight section. Here's a little explanation about the logic operators:
  • && - means AND. Both sides of the && must be true for the condition to be true. If not, the condition is false.
  • || - means OR. One side of the || must be true for the condition to be true. If both sides are false, then the condition is false.
Here's the setup & loop functions. This should work for you now:

Code: Select all

void setup()
{
    ReefAngel.Init();  //Initialize controller
    // Initialize and start the web banner timer
    ReefAngel.LoadWebBanner(pgm_read_word(&(webbanner_items[0])), SIZE(webbanner_items));
    ReefAngel.Timer[4].SetInterval(180);  // set interval to 180 seconds
    ReefAngel.Timer[4].Start();
    ReefAngel.OverheatShutoffPorts = B01000100;
   
    // random timer for wavemaker
	randomSeed(analogRead(0));
	ReefAngel.Init();  //Initialize controller   
	ReefAngel.Timer[1].SetInterval(random(15,35));
	ReefAngel.Timer[1].Start();
	ReefAngel.Relay.On(Port5);
	ReefAngel.Relay.Off(Port4);

    // Ports that are always on
    ReefAngel.Relay.On(Port6);
    ReefAngel.Relay.On(Port8);
}

void loop()
{
    // Specific functions
    ReefAngel.StandardATO(Port1);
    //turn on main light
    ReefAngel.StandardLights(2,10,00,17,00);
    //turn on strip light 1 hr b4 main
    ReefAngel.StandardLights(3,9,00,18,00);

    // Turn off Port 7 when the temp of 2 of T1, T2, T3 gets above 82.0 degrees 
    int TFlag1, TFlag2, TFlag3;
	if ( ReefAngel.Params.Temp1 >= 820 ) {
		TFlag1 = 1;
	} else {
		TFlag1 = 0;
	}
	if ( ReefAngel.Params.Temp2 >= 820 ) {
		TFlag2 = 1;
	} else {
		TFlag2 = 0;
	}
	if ( ReefAngel.Params.Temp3 >= 820 ) {
		TFlag3 = 1;
	} else {
		TFlag3 = 0;
	}      
    
	if ( (TFlag1 + TFlag2 + TFlag3) > 1 ) ReefAngel.Relay.Off(Port7);
	else ReefAngel.Relay.On(Port7);

	// Turn on moon lights based at different levels 7pm and 7am.
	if (hour()>=19 || hour()<7)
	{
		if (hour()>=19 && hour()<21) {
			ReefAngel.PWM.SetActinic(100);
			ReefAngel.PWM.SetDaylight(100);
		}
		if (hour()>=21 && hour()<23) {
			ReefAngel.PWM.SetActinic(60);
			ReefAngel.PWM.SetDaylight(80);
		}
		if (hour()>=23 && hour()<1) {
			ReefAngel.PWM.SetActinic(25);
			ReefAngel.PWM.SetDaylight(50);
		}
		if (hour()>=1 && hour()<5) {
			ReefAngel.PWM.SetActinic(80);
			ReefAngel.PWM.SetDaylight(60);
		}
		if (hour()>=5 && hour()<7) {
			ReefAngel.PWM.SetActinic(100);
			ReefAngel.PWM.SetDaylight(100);
		}
	} else {
		ReefAngel.PWM.SetActinic(0);
		ReefAngel.PWM.SetDaylight(0);
	}
        
	// wavemaker code    
	if ( ReefAngel.Timer[1].IsTriggered() )
	{
		ReefAngel.Timer[1].SetInterval(random(15,35));
		ReefAngel.Timer[1].Start();
		ReefAngel.Relay.Toggle(Port5);
		ReefAngel.Relay.Toggle(Port4);
	}

    // Web Banner stuff
    if(ReefAngel.Timer[4].IsTriggered())
    {
        ReefAngel.Timer[4].Start();
        ReefAngel.WebBanner();
    }

	ReefAngel.ShowInterface();
}
All of your moonlight code was using OR (||), which means that they would always be true no matter what. So each pass through the code, the PWM would be set with each if statement. This section needs to be an AND (&&) like i have updated it.

curt
peitro
Posts: 13
Joined: Sat Oct 22, 2011 5:19 pm

Re: Another Rookie PDE

Post by peitro »

Wow, thanks for spending the time on this. I really appreciate it. Feeling a little stupid for making such a basic mistake. But I probably would have never noticed the logic error, after staring at it most of the afternoon.

Thanks again!
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Another Rookie PDE

Post by binder »

peitro wrote:Wow, thanks for spending the time on this. I really appreciate it. Feeling a little stupid for making such a basic mistake. But I probably would have never noticed the logic error, after staring at it most of the afternoon.

Thanks again!
No problem at all. I've been there before with staring at the code and not noticing it. Besides, gotta make sure your tank runs like it's supposed to.....for the fishes & corals sake. :)

curt
peitro
Posts: 13
Joined: Sat Oct 22, 2011 5:19 pm

Re: Another Rookie PDE

Post by peitro »

Yup, its all to keep them happy, and a fun new toy for us to play with. I am still figuring out the details of what I want to do with the unit. Will probably install it into my tank setup next week. Don't want any surprises.
MichelleO
Posts: 2
Joined: Sun Feb 19, 2012 3:42 am

Another Rookie PDE

Post by MichelleO »

And variants are possible still?
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Another Rookie PDE

Post by binder »

MichelleO wrote:And variants are possible still?
What do you mean?
Post Reply