What is the best way to code a port to run 3 times a week?

Do you have a question on how to do something.
Ask in here.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

What is the best way to code a port to run 3 times a week?

Post by Sacohen »

I want to set up port 7 on my expansion relay to run every 3 days or 3 time a week, either way for 5 min a day.

Using the wizard I came up with this...

Code: Select all

ReefAngel.DosingPumpRepeat( Box1_Port7,0,4320,300 );
The problem I see with this is it is not specifying what time to do it, like 2am.
rimai
Posts: 12857
Joined: Fri Mar 18, 2011 6:47 pm

Re: What is the best way to code a port to run 3 times a wee

Post by rimai »

I'd do this:

Code: Select all

ReefAngel.Relay.Set(Box1_Port7,now()%(SECS_PER_DAY*3)<300);
Roberto.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

rimai wrote:I'd do this:

Code: Select all

ReefAngel.Relay.Set(Box1_Port7,now()%(SECS_PER_DAY*3)<300);
So how do you specify what time the relay goes on each night?
rimai
Posts: 12857
Joined: Fri Mar 18, 2011 6:47 pm

Re: What is the best way to code a port to run 3 times a wee

Post by rimai »

That is set to start a midnight.
If you want an offset, I think all you need to do is add the offset in seconds to the current time.
Test this:

Code: Select all

ReefAngel.Relay.Set(Box1_Port7,(now()+7200)%(SECS_PER_DAY*3)<300);
The above would start 7200 seconds from midnight.
Roberto.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

Thanks Roberto
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

I've had this setup for a while, but I changed my code to have it run at 9pm (75600), but the event keeps happening at 3am.

Code: Select all

 //Flush RO/DI Membrane 3 times a week
  ReefAngel.Relay.Set(Main_RODI,(now()+75600)%(SECS_PER_DAY*3)<300);
Why is this and what do I have to do to change it?
Does it have to do with the <300 at the end?
Do I need to reprogram the RANet Relay?
rimai
Posts: 12857
Joined: Fri Mar 18, 2011 6:47 pm

Re: What is the best way to code a port to run 3 times a wee

Post by rimai »

Did it work with the 7200?
Roberto.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

When I first set it up, I set it to 9pm (75600) so I can see if it was working and it did.
Then I changed it to 2am (7200) so I wouldn't hear the solenoid click on every 3 days while I was watching TV.
Even though it's in the garage it's still kind of loud.

Now I want to change it back to 9pm to check something else and it won't take.

I don't know why it's going off at 3am since I never set it to that, unless it has something to do with the RANet keeping the 2am time and because of daylight savings time it's now 3am, even thought I did change the time on the head unit.
rimai
Posts: 12857
Joined: Fri Mar 18, 2011 6:47 pm

Re: What is the best way to code a port to run 3 times a wee

Post by rimai »

Are you sure you don't have any line overriding that code?
Roberto.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

There is nothing else in my code referencing the Main_RODI port.

Take a look.

Code: Select all

#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 <AI.h>
#include <PH.h>
#include <WaterLevel.h>
#include <Humidity.h>
#include <DCPump.h>
#include <ReefAngel.h>
#include <SoftwareSerial.h>

// Define Relay Ports by Name
#define Actinic_Lights     1
#define Day_Lights         2
#define Kalkwasser         3
#define Pumps              4
#define Unused             5
#define DeNit_Doser        6
#define DeNit_Pump         7
#define ATO_Pump           8

#define Unused             Box1_Port1
#define Feeder             Box1_Port2
#define Unused             Box1_Port3
#define Unused             Box1_Port4
#define Unused             Box1_Port5
#define Unused             Box1_Port6
#define Main_RODI          Box1_Port7
#define Dummy              Box1_Port8


////// Place global variable code below here

// Does not need to be global.
// unsigned long ATOUpdate=0;

////// Place global variable code above here


void setup()
{
// This must be the first line
ReefAngel.Init(); //Initialize controller
// Ports toggled in Feeding Mode
ReefAngel.FeedingModePorts = 0;
// Ports toggled in Water Change Mode
ReefAngel.WaterChangePorts = Port4Bit;
ReefAngel.WaterChangePortsE[0] = Port1Bit;
// Ports toggled when Lights On / Off menu entry selected
ReefAngel.LightsOnPorts = Port1Bit | Port2Bit;
// Ports turned off when Overheat temperature exceeded
ReefAngel.OverheatShutoffPorts = Port1Bit | Port2Bit;
// Use T1 probe as temperature and overheat functions
ReefAngel.TempProbe = T1_PROBE;
ReefAngel.OverheatProbe = T1_PROBE;
// Set the Overheat temperature setting
InternalMemory.OverheatTemp_write( 825 );
//Set Standard Menu
ReefAngel.AddStandardMenu();
//Water Level
ReefAngel.AddMultiChannelWaterLevelExpansion();
//Webserver Authentication
ReefAngel.Network.WifiAuthentication("Sacohen:xxxxxxxx");
//DDNS Server
ReefAngel.DDNS("myreeftank");
ReefAngel.AddRANet();
ReefAngel.Relay.RANetFallBackE[0]=Port8Bit;

// Feeeding and Water Change mode speed
ReefAngel.DCPump.FeedingSpeed=0;
ReefAngel.DCPump.WaterChangeSpeed=0;
ReefAngel.DCPump.ActinicChannel=Sync; // Now you're pump will be affected by the portal settings.
//ReefAngel.DCPump.DaylightChannel=AntiSync; // Now you're pump will be affected by the portal settings.
//ReefAngel.DCPump.AntiSyncOffset=85;

// Ports that are always on
ReefAngel.Relay.On( Pumps );
ReefAngel.Relay.On( Dummy );

////// Place additional initialization code below here


////// Place additional initialization code above here
}

void loop()
{
  ReefAngel.StandardLights( Actinic_Lights,12,0,22,0 );
  ReefAngel.StandardLights( Day_Lights,13,0,21,0 );
  ReefAngel.WaterLevelATO(0,ATO_Pump,720,34,36);
  if ( hour()>=13 && hour()<21 )
  ReefAngel.PWM.SetChannel( 5, 0 );
  else
  ReefAngel.PWM.SetChannel( 5, PWMSlope(21,0,13,0,0,MoonPhase() ,60,0) );

 
  ////// Place your custom code below here
  
  //Flush RO/DI Membrane 3 times a week
  ReefAngel.Relay.Set(Main_RODI,(now()+75600)%(SECS_PER_DAY*3)<300);
  
  
  //Mix Kalkwasser
static unsigned long Kalk_Mixing = 0;

static unsigned long stopTime = 0;
static byte prevWL=0;

if (ReefAngel.WaterLevel.GetLevel(1)==100 && prevWL<100) {   
  stopTime=now()+3600; // Get time one hour from now.
}
prevWL = ReefAngel.WaterLevel.GetLevel(1); // Store the water level

if (now() < stopTime) { ReefAngel.Relay.On(Kalkwasser); } else { ReefAngel.Relay.Off(Kalkwasser); } 
  
  //AutoFeeder
static unsigned long autofeeding = 0;

if ((now()%SECS_PER_DAY==64800)) //if it is 6 pm
{
ReefAngel.FeedingModeStart(); //START FEEDING MODE
}

if (ReefAngel.DisplayedMenu==FEEDING_MODE)
{
if ( autofeeding == 0 ) {
autofeeding = now(); //set the time of the start of feeding to variable feeding
}

if ((now()-autofeeding>=60) && (now()-autofeeding<=61)) //if between 60 and 61 seconds has past
{
ReefAngel.Relay.On(Feeder); //TURN FEEDER RELAY ON
}
else 
{
ReefAngel.Relay.Off(Feeder); //TURN FEEDER RELAY OFF
}
} else {
if ( autofeeding > 0 ) {
autofeeding = 0;
}
}
  // DeNitrate Routine
int DeNit_Offset=3600; 
int DeNit_Repeat=21600;
int DeNit_Doser_Offset=1200;
int DeNit_Doser_Runtime=600;
int DeNit_Pump_Runtime=1200;
int DeNit_ATO_Offtime=1500;

// Pump comes on first
ReefAngel.Relay.Set(DeNit_Pump,(now()-DeNit_Offset)%DeNit_Repeat<DeNit_Pump_Runtime);  // Runs denitrator pump
// Doser comes on second
ReefAngel.Relay.Set(DeNit_Doser,((now()-DeNit_Offset)-DeNit_Doser_Offset)%DeNit_Repeat<DeNit_Doser_Runtime); // Runs denitrator doser
   
  // Disable ATO
if ( (now()-3600)%21600<DeNit_ATO_Offtime) ReefAngel.WaterLevelATO(0,ATO_Pump,720,0,1);

  // Delay WL ATO after water change and DeNit_Dosing
  static time_t wcTimer=0;
  if (ReefAngel.DisplayedMenu == WATERCHANGE_MODE) wcTimer=now();

  // First 10 minutes after WC disable ATO
  if (now()-wcTimer >= 0 && now()-wcTimer < 600)
    ReefAngel.WaterLevelATO(0,ATO_Pump,720,0,1);

    // Track Salinity low duration
    static unsigned long lastGoodSal;
    static boolean lowSalFlag;
   
    // Salinity is good, update counter
    if (ReefAngel.Params.Salinity>=335) lastGoodSal=now(); 
    
    // Counter hasn't been updating in 300 seconds..
    if (now()-lastGoodSal>300) {
      lowSalFlag=true; // Salinity is definitely below 33.5
    } else {
      lowSalFlag=false; // Salinity back to normal
    }
    
    // Disable ATO if lowSalFlag is true
   //if (lowSalFlag) ReefAngel.WaterLevelATO(0,ATO_Pump,720,0,1);
    
  // Disable ATO if ATO High IsActive()
  if (ReefAngel.HighATO.IsActive()) 
    ReefAngel.WaterLevelATO(0,ATO_Pump,720,0,1);
    

// Add random mode if we set to Mode to Custom in portal
static int rmode;
static boolean changeMode=true;


// These are the modes we can cycle through. You can add more and even repeat...
byte modes[] = { ReefCrest, Gyre, Lagoon, TidalSwell, ShortPulse, LongPulse, Else, };

if (now()%SECS_PER_DAY==0 || changeMode==true) { // Change at midnight or if controller rebooted
rmode=random(100)%sizeof(modes); // Change the mode once per day to pick from our array
changeMode=false;
}

// Set timer when in feeding mode
static unsigned long feeding;
if (ReefAngel.DisplayedMenu==FEEDING_MODE) feeding=now();

if (now()-feeding<3600) { 
  // Continue NTM for the 60 minutes
  ReefAngel.DCPump.UseMemory=false;
  ReefAngel.DCPump.Duration=InternalMemory.DCPumpDuration_read();
  ReefAngel.DCPump.Mode=NutrientTransport;
} else if (now()%SECS_PER_DAY<43200 || now()%SECS_PER_DAY>=79200) { // 12pm / 10pm
  // Night mode (go to 30%)
  ReefAngel.DCPump.UseMemory=false;
  ReefAngel.DCPump.Duration=InternalMemory.DCPumpDuration_read();
  ReefAngel.DCPump.Mode=Constant;
  ReefAngel.DCPump.Speed=30;
} else if (InternalMemory.DCPumpMode_read()==11) { 
  // Custom Mode and nothing else going on
  ReefAngel.DCPump.UseMemory=false;
  ReefAngel.DCPump.Duration=InternalMemory.DCPumpDuration_read();
  ReefAngel.DCPump.Mode=modes[rmode];  // Put the mode to the random mode :)
  ReefAngel.DCPump.Speed=InternalMemory.DCPumpSpeed_read(); // Set speed from portal
} else {
  ReefAngel.DCPump.UseMemory=true; // Will reset all values from memory
}

  ////// Place your custom code above here

  // This should always be the last line
  ReefAngel.Portal( "Sacohen","xxxxxxxx" );
  ReefAngel.ShowInterface();
}

void DrawCustomMain()
{
int x,y;
char text[10];
// Parameters
#if defined DisplayLEDPWM && ! defined RemoveAllLights
ReefAngel.LCD.DrawMonitor( 15, 20, ReefAngel.Params,
ReefAngel.PWM.GetDaylightValue(), ReefAngel.PWM.GetActinicValue() );
#else // defined DisplayLEDPWM && ! defined RemoveAllLights
ReefAngel.LCD.DrawMonitor( 15, 20, ReefAngel.Params );
#endif // defined DisplayLEDPWM && ! defined RemoveAllLights
pingSerial();

// Water Level
ReefAngel.LCD.DrawText( COLOR_DARKGOLDENROD,DefaultBGColor,75,57, "WL:" );
ReefAngel.LCD.DrawText( COLOR_DARKGOLDENROD,DefaultBGColor,99,57, ReefAngel.WaterLevel.GetLevel() );

ReefAngel.LCD.DrawText( COLOR_DARKGOLDENROD,DefaultBGColor,66,66, "WL 1:" );
ReefAngel.LCD.DrawText( COLOR_DARKGOLDENROD,DefaultBGColor,99,66, ReefAngel.WaterLevel.GetLevel(1) );

//Salinity  
  ConvertNumToString(text, ReefAngel.Params.Salinity, 10);
  strcat(text,"  ");
  ReefAngel.LCD.DrawText(DefaultFGColor,DefaultBGColor,39,75,"Salinity:");
  ReefAngel.LCD.DrawText(DefaultFGColor,DefaultBGColor,99,75,text);
pingSerial();

// Main Relay Box
byte TempRelay = ReefAngel.Relay.RelayData;
TempRelay &= ReefAngel.Relay.RelayMaskOff;
TempRelay |= ReefAngel.Relay.RelayMaskOn;
ReefAngel.LCD.DrawOutletBox( 12, 92, TempRelay );
pingSerial();
TempRelay = ReefAngel.Relay.RelayDataE[0];
TempRelay &= ReefAngel.Relay.RelayMaskOffE[0];
TempRelay |= ReefAngel.Relay.RelayMaskOnE[0];
ReefAngel.LCD.DrawOutletBox(12, 104, TempRelay);

// Date and Time
ReefAngel.LCD.DrawDate( 6, 122 );
pingSerial();
}

void DrawCustomGraph()
{
}
User avatar
lnevo
Posts: 5422
Joined: Fri Jul 20, 2012 9:42 am

Re: What is the best way to code a port to run 3 times a wee

Post by lnevo »

The code is working right. When it's 3am your adding 21 hours and then it does the function as if it were midnight. The rest of the function to the right of % assumes its midnight. So at 3am it is fast forwarding to midnight and working. If you want it at 9pm then add 14400 if you want to use 21 hours (75600) then try subtracting it instead of adding
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

Wow, now I'm totally confused.
Just when I thought I was starting understand this a little bit.

I thought now equalled midnight and then you add the amount of seconds that you want to get to your desired time.

OK I'll will try adding 14400 and see what happens.

Thanks Lee.
User avatar
lnevo
Posts: 5422
Joined: Fri Jul 20, 2012 9:42 am

Re: What is the best way to code a port to run 3 times a wee

Post by lnevo »

Yes but the condition your checking %(3 days)<300 is assuming midnight left of the %. The +whatever is the offset and if your doing something hourly or something divisible evenly throughout the day it won't make a difference (like every 3 hours or something) but since your looking at a once a day we need to be more precise. The now+whatever will make it midnight at the time you want
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

Ok. That makes a bit more sense. Thanks for explaining it.
I really should find an online tutorial or something, I just don't have the time.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

Well it went off today as it should (3rd day), but it went off at 8pm.
So 10800 would be 9pm?
User avatar
lnevo
Posts: 5422
Joined: Fri Jul 20, 2012 9:42 am

Re: What is the best way to code a port to run 3 times a wee

Post by lnevo »

Did you reset your clock for daylight savings? Would either will or won't go for it.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

Yes I reset the clock for daylight savings.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

FYI. changing the time to 10800 set it off at 9pm last night.

The working code for every 3 days at 9pm is...

Code: Select all

//Flush RO/DI Membrane 3 times a week
  ReefAngel.Relay.Set(Main_RODI,(now()+10800)%(SECS_PER_DAY*3)<300);
User avatar
lnevo
Posts: 5422
Joined: Fri Jul 20, 2012 9:42 am

Re: What is the best way to code a port to run 3 times a wee

Post by lnevo »

Yeah, 10800 is 3 hours... 9pm + 3 hours = Midnight.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

If I wanted to change this code from every 3 days to Monday, Wednesday & Friday, how would it differ?

Code: Select all

//Flush RO/DI Membrane 3 times a week
  ReefAngel.Relay.Set(Main_RODI,(now()+10800)%(SECS_PER_DAY*3)<300);
I'm just thinking about something I'm going to add in the future and it may be better to run on specific days rather than every 3 days.
User avatar
lnevo
Posts: 5422
Joined: Fri Jul 20, 2012 9:42 am

Re: What is the best way to code a port to run 3 times a wee

Post by lnevo »

Easy enough. I'd have to test the syntax, but a switch/case statement should be able to handle that easy enough.

Honestly I think flushing the membrane though once a week should be sufficient.

Anyway, what days is it running now. You could always add a day to the now() to fast forward it a day or whatever is needed And then make it another day while skipping saturday/sunday :)
rimai
Posts: 12857
Joined: Fri Mar 18, 2011 6:47 pm

Re: What is the best way to code a port to run 3 times a wee

Post by rimai »

The Time library offers day of the week.
I think this should do it:

Code: Select all

ReefAngel.Relay.Set(Main_RODI, ((weekday()==dowMonday || weekday()==dowWednesday || weekday()==dowFriday) && (now()+10800)<300 ));
Roberto.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

Lee;

I was talking with Russ at Buckeye Hydro on Reef Central (http://www.reefcentral.com/forums/showt ... ?t=2369559) and he said "You should make water at least once per week. Flushing once a week or more often is a good idea too."

Right now it is running every 3 days so if I first started it on Monday then it would run Monday, Thursday. Sunday, Wednesday, Saturday and so on.
Falling back 1 day every week.

What I ultimately want to do is have it turn on port 7 to flush the membrane like Monday and Wednesday and on Friday I will have it turn on port 6 & 7 to make just RO water. It will bypass the DI resin and just flow the water out to the garden bed outside the garage.

Again this is the ultimate end goal. I would also like to have some safety statements in there that would not run either of these if my RO/DI system was triggered by my water level sensors, to make RO/DI water to refill my 20 Gallon Brute ATO Reservoir or my 20 Gallon Brut Salt Water Reservoir for Automatic water changes with in the past 48 hours.

I was going to try to attempt that on my own later, but that is what I'm going for to automate, the water change and ATO stations in terms of refilling them.

Thanks Roberto. I'll try the time library code and see how that does.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

rimai wrote:The Time library offers day of the week.
I think this should do it:

Code: Select all

ReefAngel.Relay.Set(Main_RODI, ((weekday()==dowMonday || weekday()==dowWednesday || weekday()==dowFriday) && (now()+10800)<300 ));
This didn't work.
I originally changed Friday to Thursday to test it that night and nothing, so I set it back to the way Roberto wrote it and although I wasn't home last night at 9pm there was no evidence there it happened.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

I'm not sure why it is not working. I though t it was the double &, but that means "and".
So to me it reads as turn on the port for Main_RODI on Monday, Wednesday and Friday and 3 hours before midnight for 300 seconds (5 min).
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

Does anyone have a suggestion on how to do this?
I can't seem to get it to work.
rimai
Posts: 12857
Joined: Fri Mar 18, 2011 6:47 pm

Re: What is the best way to code a port to run 3 times a wee

Post by rimai »

Try doing several if conditions and see if it works better for you.

Code: Select all

ReefAngel.Relay.Off(Main_RODI);
if (weekday()==dowMonday && hour()==9 && minute()<5)
  ReefAngel.Relay.On(Main_RODI);
Do something similar for the other times.
Roberto.
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

Thanks. I'll try it.
User avatar
lnevo
Posts: 5422
Joined: Fri Jul 20, 2012 9:42 am

Re: What is the best way to code a port to run 3 times a wee

Post by lnevo »

hou()==9 is going to be 9am though...
User avatar
Sacohen
Posts: 1833
Joined: Sun Apr 21, 2013 6:25 am
Location: Davie, FL

Re: What is the best way to code a port to run 3 times a wee

Post by Sacohen »

So it should be the following?

Code: Select all

ReefAngel.Relay.Off(Main_RODI);
if (weekday()==dowMonday && hour()==21 && minute()<5)
  ReefAngel.Relay.On(Main_RODI);
Post Reply