Page 1 of 1

Re: Countdown timer for relay

Posted: Sat Oct 10, 2015 8:43 am
by lnevo
Pretty sure there's an issue, and if not, it's still a red flag so I registered an issue https://github.com/reefangel/Libraries/issues/222 and will be submitting a patch. If using this for any critical dosing, I would hesitate. I'll put the patch up here as well to make it easier for people to grab, since I'll be submitting it to the current dev repository.

Re: Countdown timer for relay

Posted: Sat Oct 10, 2015 8:46 am
by lnevo
Well in defense of my code, I will say that if you are setting it up for dosing, it will work properly. The issue at the moment is that if Repeat is set to 0 or is less than the Time setting, then you could have an issue. Also the TimedPort has a separate value for Time and triggered time (StartTime) so instead of saying swabbie.Time=120; I would have used swabbie.StartTime=120; The difference is if you are automating you may want 5 seconds every hour, but if you explicitly start it, you might want it for 10 seconds. I clearly need to put some documentation into this class :) I haven't looked at it in a while so having to figure out how it works again.

Re: Countdown timer for relay

Posted: Sat Oct 10, 2015 10:45 am
by lnevo
Ok so I added a sanity check and a few comments to make it clearer. I'm also going to see if I can find the post where I originally posted it as the discussion might lend itself better in there. Here's the patched class. Just unzip it in your Libraries folder and it should overwrite the existing folder.
TimedPort.zip
(3.29 KiB) Downloaded 578 times

Re: Countdown timer for relay

Posted: Sat Oct 10, 2015 6:44 pm
by binder
I will take a look at it this week. kinda busy this week.

Sent from my Moto X

Re: Countdown timer for relay

Posted: Sun Oct 11, 2015 5:04 am
by lnevo
Awesome!

Re: Countdown timer for relay

Posted: Sun Oct 11, 2015 9:13 am
by paperdesk
lnevo wrote: Just change Box1_Port2 to whatever port you want to control and then use the repeat and runtime to set how long you want the port to run and how often to repeat. If you don't want it to repeat then just take out the else statement at the end.
I'm still a bit confused about how and where to post code. Can I put this in the custom code area?

Re: Countdown timer for relay

Posted: Sun Oct 11, 2015 9:14 am
by lnevo
Yep

Re: Countdown timer for relay

Posted: Sun Oct 11, 2015 11:08 am
by paperdesk
lnevo wrote:Yep
Oh dear, I'm doing something wrong.

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 <PAR.h>
#include <ReefAngel.h>

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


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


void setup()
{
    // This must be the first line
    ReefAngel.Init();  //Initialize controller
    ReefAngel.Use2014Screen();  // Let's use 2014 Screen 
    ReefAngel.AddSalinityExpansion();  // Salinity Expansion Module
    ReefAngel.AddWaterLevelExpansion();  // Water Level Expansion Module
    // Ports toggled in Feeding Mode
    ReefAngel.FeedingModePorts = Port1Bit | Port2Bit | Port3Bit | Port4Bit | Port5Bit | Port7Bit | Port8Bit;
    ReefAngel.FeedingModePortsE[0] = 0;
    // Ports toggled in Water Change Mode
    ReefAngel.WaterChangePorts = Port1Bit | Port2Bit | Port3Bit | Port4Bit | Port5Bit | Port7Bit | Port8Bit;
    ReefAngel.WaterChangePortsE[0] = 0;
    // Ports toggled when Lights On / Off menu entry selected
    ReefAngel.LightsOnPorts = 0;
    ReefAngel.LightsOnPortsE[0] = 0;
    // Ports turned off when Overheat temperature exceeded
    ReefAngel.OverheatShutoffPorts = Port2Bit;
    ReefAngel.OverheatShutoffPortsE[0] = 0;
    // Use T1 probe as temperature and overheat functions
    ReefAngel.TempProbe = T1_PROBE;
    ReefAngel.OverheatProbe = T1_PROBE;
    // Set the Overheat temperature setting
    InternalMemory.OverheatTemp_write( 820 );

    // Feeeding and Water Change mode speed
    ReefAngel.DCPump.FeedingSpeed=0;
    ReefAngel.DCPump.WaterChangeSpeed=0;


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

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

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

void loop()
{
    ReefAngel.StandardFan( Port1,784,790 );
    ReefAngel.StandardHeater( Port2,780,785 );
    ReefAngel.Relay.DelayedOn( Port4,8 );
    ReefAngel.Relay.DelayedOn( Port5,8 );
    ReefAngel.WaterLevelATO( Port7,3600,80,100 );
    ReefAngel.Relay.DelayedOn( Port8,10 );
    ReefAngel.StandardLights( Box1_Port1,8,0,21,0 );
    ReefAngel.StandardLights( Box1_Port2,8,0,21,0 );
    ReefAngel.StandardLights( Box1_Port3,8,0,21,0 );
    ReefAngel.StandardLights( Box1_Port4,8,0,21,0 );
    ReefAngel.PWM.SetChannel( 0, PWMSlope(8,0,21,0,10,50,65,10) );
    ReefAngel.PWM.SetChannel( 1, PWMSlope(8,0,21,0,9,30,65,9) );
    ReefAngel.PWM.SetChannel( 2, PWMSlope(8,0,21,0,8,50,65,8) );
    ReefAngel.PWM.SetChannel( 3, PWMSlope(8,0,21,0,6,30,65,6) );
    ReefAngel.DCPump.UseMemory = false;
    ReefAngel.DCPump.SetMode( LongPulse,90,10 );
    ReefAngel.DCPump.DaylightChannel = Sync;
    ReefAngel.DCPump.ActinicChannel = AntiSync;
    ReefAngel.DCPump.ExpansionChannel[0] = None;
    ReefAngel.DCPump.ExpansionChannel[1] = None;
    ReefAngel.DCPump.ExpansionChannel[2] = None;
    ReefAngel.DCPump.ExpansionChannel[3] = None;
    ReefAngel.DCPump.ExpansionChannel[4] = None;
    ReefAngel.DCPump.ExpansionChannel[5] = None;
    ////// Place your custom code below here
    if(ReefAngel.HighATO.IsActive())
{
ReefAngel.Relay.Override(Port8,0);
ReefAngel.Relay.On(Port8);
}
  int repeat=3600; // in minutes
  int runtime=120; // in seconds
  static time_t t;
  
  // Manual mode
  if (ReefAngel.Relay.isMaskOn(Box2_Port6)) {
    ReefAngel.Relay.Auto(Box2_Port6);
    t=now();
  }  

  if (now()-t < runtime) {
    ReefAngel.Relay.On(Box2_Port6);
  } else {
    ReefAngel.DosingPumpRepeat(Box2_Port6,0,repeat,runtime);    
  }  
     ////// Place your custom code above here

    // This should always be the last line
    ReefAngel.Portal( "paperdesk" );
    ReefAngel.ShowInterface();

Re: Countdown timer for relay

Posted: Sun Oct 11, 2015 11:17 am
by lnevo
What error are you getting?

Re: Countdown timer for relay

Posted: Sun Oct 11, 2015 11:45 am
by paperdesk
I don't get an error, it just doesn't do anything. When I toggle the port it stays on and never turns off unless I toggle it again.

Re: Countdown timer for relay

Posted: Sun Oct 11, 2015 3:46 pm
by lnevo
What is the port supposed to do. Just cause the code you posted is gonna run that port for 2 minutes every 6 hours.

Try and put the following before the if statement.

ReefAngel.Relay.Off(Box2_Port6);

Is that the port your using anyway? You have to change the code to use the correct port. I don't see any Box1 or other Box2 ports...

Re: Countdown timer for relay

Posted: Sun Oct 11, 2015 4:07 pm
by paperdesk
Facepalm ;) I thought that having two boxes meant I had a box 1 and a box 2. After reading your comment I realized that box 1 is actually my second box. After that came together it was simple to change the time values and remove the "else" line and it's working perfectly. Thanks!

Re: Countdown timer for relay

Posted: Sun Oct 11, 2015 9:11 pm
by lnevo
Awesome. Can you post your final code that you used.

Re: Countdown timer for relay

Posted: Mon Oct 12, 2015 7:54 am
by paperdesk
I ran into some issues last night, but think it's working now. I'll post the code once I'm sure it's stable and correct, just doing a little testing today.

Re: Countdown timer for relay

Posted: Wed Oct 14, 2015 8:09 am
by paperdesk
Here's what I ended up with. It runs for one hour and repeats every two days. , I have to wait two days to be sure it actually repeats, but I think it will:

Code: Select all

  int repeat=2880; // in minutes
  int runtime=3600; // in seconds
  static time_t t;
  
  // Manual mode
  if (ReefAngel.Relay.isMaskOn(Box1_Port6)) {
    ReefAngel.Relay.Auto(Box1_Port6);
    t=now();
  }  

  if (now()-t < runtime) {
    ReefAngel.Relay.On(Box1_Port6);
  } else {
    ReefAngel.DosingPumpRepeat(Box1_Port6,0,repeat,runtime);    
  }  

Re: Countdown timer for relay

Posted: Wed Oct 14, 2015 8:13 am
by lnevo
Cool and triggering manually is working? What are you automating here?

Re: Countdown timer for relay

Posted: Thu Oct 15, 2015 8:39 am
by paperdesk
Yep, when I toggle the relay from the app it opens the relay for the set amount of time (60 minutes currently) and shuts off again. It then runs automatically every two days for 60 minutes.

I'm using this code to control a solenoid on my RODI system. It refills my fresh water container on a regular basis and other containers on demand.

My fresh water container has a shut off float in it, but I overflowed other water containers too many times, so it's nice to have something that will shut off automatically.

I wonder if this function could be built into the app that would allow one to start a countdown timer for any relay, after which the relay would return to its normal function. Seems like something a lot of people would use, and would be a lot simpler than changing to code frequently.

Re: Countdown timer for relay

Posted: Thu Oct 15, 2015 12:53 pm
by lnevo
I have a class in the libraries called TimedPort that does this. I would recommend looking at the water level sensor module and then just using the WaterLevelATO function to control the solenoid.