Progressive sunphase

Do you have a question on how to do something.
Ask in here.
Deckoz2302
Posts: 149
Joined: Tue Nov 01, 2011 11:05 am

Re: Progressive sunphase

Post by Deckoz2302 »

No problem - the moonphase is actually part of the libraries. I just made that code so you can see what part of the moonphase its in visually on the main screen. Glad it worked out :)
rufessor
Posts: 291
Joined: Tue Oct 25, 2011 7:39 am

Re: Progressive sunphase

Post by rufessor »

HI-

SO I posted some strange behavior with my lights and the PWM ouput that I have been experiencing and Roberto mentioned that you have been working on a discovered bug which lead your lights to do, I am assuming, the same thing. I am still using your code until I can find more than 1 consecutive hour to work on the INO I am writing for the library calls and lat/lon based sunphase calculations... until then, I would love to see if you have figured this out and pirate your fix so my lights work unattended.

Thanks!
Deckoz2302
Posts: 149
Joined: Tue Nov 01, 2011 11:05 am

Re: Progressive sunphase

Post by Deckoz2302 »

Hmmm late reply haha...I'll post update tonight
rufessor
Posts: 291
Joined: Tue Oct 25, 2011 7:39 am

Re: Progressive sunphase

Post by rufessor »

No worries.. I am finally getting around to finishing trouble shooting/debugging my code that uses the library functions for Lat/Lon sunrise sunset calc.. so I have not been running your code for a good while... just plug in and unplug for now, as I am constantly-the 0.25 days a week I have free time to do this :) loading my code and serial debugging to be sure everything is working... which its not quite yet!

Thanks... still curious though...
User avatar
JNieuwenhuizen
Posts: 96
Joined: Thu Feb 16, 2012 12:39 am
Location: South Africa

Re: Progressive sunphase

Post by JNieuwenhuizen »

My pwm function... wrong or right?

Code: Select all

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


// Initial values to all 6 PWM channels at startup. They will always be 0.
byte PWMChannel[]={
0,0,0,0,0,};
//MoonPhase Value Setting
byte MoonPWMValue;
//------------------------------------------------ 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 ----------------------------------------------
//*********************************************************************************************************************************
// 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 
  }
}



//*********************************************************************************************************************************
 
rimai
Posts: 12857
Joined: Fri Mar 18, 2011 6:47 pm

Re: Progressive sunphase

Post by rimai »

Can you post your entire code?
Roberto.
User avatar
JNieuwenhuizen
Posts: 96
Joined: Thu Feb 16, 2012 12:39 am
Location: South Africa

Re: Progressive sunphase

Post by JNieuwenhuizen »

Yes, entire .pde

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



//*********************************************************************************************************************************
 

rimai
Posts: 12857
Joined: Fri Mar 18, 2011 6:47 pm

Re: Progressive sunphase

Post by rimai »

Very complex :)
Try this:

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 LEDWhiteBlue  0 
#define LEDWhiteBlue1 1
#define LEDWhiteBlue2 2
#define MixedLED 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);
  //------------------------------------------------ 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[MixedLED]=PWMSlope(
  InternalMemory.StdLightsOnHour_read(),
  InternalMemory.StdLightsOnMinute_read(),
  InternalMemory.StdLightsOffHour_read(),
  InternalMemory.StdLightsOffMinute_read(),
  InternalMemory.PWMSlopeStartA_read(),
  InternalMemory.PWMSlopeEndA_read(),
  InternalMemory.PWMSlopeDurationA_read(),
  0);    
  PWMChannel[LEDWhiteBlue1]=PWMChannel[LEDWhiteBlue];
  PWMChannel[LEDWhiteBlue2]=PWMChannel[LEDWhiteBlue];
  ReefAngel.PWM.SetChannel(0,PWMChannel[LEDWhiteBlue]);
  ReefAngel.PWM.SetChannel(1,PWMChannel[LEDWhiteBlue1]);
  ReefAngel.PWM.SetChannel(2,PWMChannel[LEDWhiteBlue2]);
  ReefAngel.PWM.SetChannel(3,PWMChannel[MixedLED]);

  MoonPWMValue=MoonPhase();
  if (hour()==22 || hour()==6)
    MoonPWMValue=PWMSlope(22,00,7,00,0,MoonPhase(),60,0);
  if (PWMChannel[LEDWhiteBlue]) MoonPWMValue=0;
  ReefAngel.PWM.SetChannel(4,MoonPWMValue);

  //------------------------------------------------- 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();
  } 

  ReefAngel.Portal("JNieuwenhuizen");
  ReefAngel.ShowInterface();

}

//*********************************************************************************************************************************
// 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 resul
  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 
}

Roberto.
User avatar
JNieuwenhuizen
Posts: 96
Joined: Thu Feb 16, 2012 12:39 am
Location: South Africa

Re: Progressive sunphase

Post by JNieuwenhuizen »

It Works!

Thank You. Now only 1 thing left... I used other code, and then found the one that defines the channels for cloud and lightning.

This is not in the code : I see it has some lines that display, I dont need them.

Code: Select all

//is it time for cloud yet?
    if (NumMins(hour(),minute())>=cloudstart && NumMins(hour(),minute())<(cloudstart+cloudduration))
    {
      // let's go through all channels to pick which ones will be dimmed
      for (int a=0;a<6;a++)
      {
        if (bitRead(Actinic_Channels,a)==0)
        {
          // this will slope down the channel from the current PWM to 0 within 3minutes.
          // then it will stay at 0 for the duration of the cycle
          // and finally slope up from 0 to PWM value within 3 minutes
          // it is basically an inversed slope
          PWMChannel[a]=ReversePWMSlope(cloudstart,cloudstart+cloudduration,PWMChannel[a],0,180);
        }
      }
      if (lightningchance && (NumMins(hour(),minute())==(cloudstart+(cloudduration/2))) && second()<5) 
      {
        for (int b=0;b<6;b++)
        {
          if (bitRead(Daylight_Channels,b)==1)
          {
            if (random(100)<20) lightningstatus=1; 
            else lightningstatus=0;
            if (lightningstatus) PWMChannel[b]=100; 
            else PWMChannel[b]=0;
            //delay(10);
          }
          else
          {
            PWMChannel[b]=20;
          }
        }
      }
    }
    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(100);
        // if picked number is greater than Lightning_Change_per_Cloud, we will not have lightning today
        if (lightningchance>Lightning_Change_per_Cloud) lightningchance=0;
      }
    }
  }
  
  if (LastNumMins!=NumMins(hour(),minute()))
  {
    LastNumMins=NumMins(hour(),minute());
    ReefAngel.LCD.Clear(255,0,120,132,132);
    ReefAngel.LCD.DrawText(0,255,5,120,"C");
    ReefAngel.LCD.DrawText(0,255,11,120,"00:00");
    ReefAngel.LCD.DrawText(0,255,45,120,"L");
    ReefAngel.LCD.DrawText(0,255,51,120,"00:00");
    if (cloudchance && (NumMins(hour(),minute())<cloudstart))
    {
      int x=0;
      if ((cloudstart/60)>=10) x=11; else x=17;
      ReefAngel.LCD.DrawText(0,255,x,120,(cloudstart/60));
      if ((cloudstart%60)>=10) x=29; else x=35;
      ReefAngel.LCD.DrawText(0,255,x,120,(cloudstart%60));
    }
    ReefAngel.LCD.DrawText(0,255,90,120,cloudduration);
    if (lightningchance) 
    {
      int x=0;
      if (((cloudstart+(cloudduration/2))/60)>=10) x=51; else x=57;
      ReefAngel.LCD.DrawText(0,255,x,120,((cloudstart+(cloudduration/2))/60));
      if (((cloudstart+(cloudduration/2))%60)>=10) x=69; else x=75;
      ReefAngel.LCD.DrawText(0,255,x,120,((cloudstart+(cloudduration/2))%60));
    }
  }   
}

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;

This worries me a bit - So Channel 0 , 1 , 2 will be used for daylight and atinics. Its all mixed LED's. I think 0 and 2 for clouds, and 1 and 3 for lightning

Code: Select all

  // Channels used by the actinic LEDs on the PWM Expansion module
  // These channels will not be dimmed when the cloud effect is triggered
  // Number is a binary form. B001100 means channel 2 and 3 are used for actinics
#define Actinic_Channels B000111

  // Channels used by the daylight LEDs on the PWM Expansion module
  // These channels will be used for the spike when lightning effect is triggered
  // Number is a binary form. B000011 means channel 0 and 1 are used for daylights
#define Daylight_Channels B111000
User avatar
JNieuwenhuizen
Posts: 96
Joined: Thu Feb 16, 2012 12:39 am
Location: South Africa

Re: Progressive sunphase

Post by JNieuwenhuizen »

Think I figured it out , its simply 0 0 0 0 0 0 , one per channel from right to left

so to use 0 and 2 - B000101

And to use 1 and 3 - B001010

All thats left now is this :

Code: Select all

  
  if (LastNumMins!=NumMins(hour(),minute()))
  {
    LastNumMins=NumMins(hour(),minute());
    ReefAngel.LCD.Clear(255,0,120,132,132);
    ReefAngel.LCD.DrawText(0,255,5,120,"C");
    ReefAngel.LCD.DrawText(0,255,11,120,"00:00");
    ReefAngel.LCD.DrawText(0,255,45,120,"L");
    ReefAngel.LCD.DrawText(0,255,51,120,"00:00");
    if (cloudchance && (NumMins(hour(),minute())<cloudstart))
    {
      int x=0;
      if ((cloudstart/60)>=10) x=11; else x=17;
      ReefAngel.LCD.DrawText(0,255,x,120,(cloudstart/60));
      if ((cloudstart%60)>=10) x=29; else x=35;
      ReefAngel.LCD.DrawText(0,255,x,120,(cloudstart%60));
    }
    ReefAngel.LCD.DrawText(0,255,90,120,cloudduration);
    if (lightningchance) 
    {
      int x=0;
      if (((cloudstart+(cloudduration/2))/60)>=10) x=51; else x=57;
      ReefAngel.LCD.DrawText(0,255,x,120,((cloudstart+(cloudduration/2))/60));
      if (((cloudstart+(cloudduration/2))%60)>=10) x=69; else x=75;
      ReefAngel.LCD.DrawText(0,255,x,120,((cloudstart+(cloudduration/2))%60));
    }
  }   
}
It daws text to the screen, I dont think I have space for it. Or if anyone can give me the spacing for it to be in here somewhere

Code: Select all

 // 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();
rimai
Posts: 12857
Joined: Fri Mar 18, 2011 6:47 pm

Re: Progressive sunphase

Post by rimai »

Yeah, you got it.
You can move the stuff around.
The function DrawText() has 5 parameters:
1st - Forecolor
2nd - Backcolor
3rd - X position on the screen
4th - Y position on screen
5th - Text to be placed on the screen

The DrawOutletBox() has 3 parameters:
1st - X position on the screen
2nd - Y position on screen
3rd - Relay data to be placed on the screen

You can move stuff around until you get it to fit in your screen :)
Roberto.
Post Reply