Countdown timer for relay

Community contributed apps
Post Reply
paperdesk
Posts: 173
Joined: Fri May 23, 2014 8:09 am

Countdown timer for relay

Post by paperdesk »

I didn't know where to put this for sure, but I guess this would be an app option.

I would like to be able to set a countdown timer to control a relay for a select amount of time. For example, I may want to fill a container with RODI water.

Currently I have a solenoid plugged into a relay. I can toggle the relay on and off to fill the container, but it would be much more convenient to be able to set the relay to be on for 30 minutes, or 60 minutes or whatever I like, then automatically shut off.

Has anyone done this? Is it possible? Is is something anyone else is even interested in?
Image
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Countdown timer for relay

Post by binder »

paperdesk wrote:I didn't know where to put this for sure, but I guess this would be an app option.

I would like to be able to set a countdown timer to control a relay for a select amount of time. For example, I may want to fill a container with RODI water.

Currently I have a solenoid plugged into a relay. I can toggle the relay on and off to fill the container, but it would be much more convenient to be able to set the relay to be on for 30 minutes, or 60 minutes or whatever I like, then automatically shut off.

Has anyone done this? Is it possible? Is is something anyone else is even interested in?
yes this is possible. this is exactly what the feeding mode and water change modes do (well...water change mode is not a timed feature, but functions the same none the less).

it will require custom code being added. you will need to set the timer duration, ports to toggle and how you want to activate it. i would suggest looking at the feeding mode function inside the libraries to see how we handle it. what we do is retain the state of the existing relays that get toggled and then when the mode is entered, we turn off all the ports that are specified to be turned off and then leave the others alone. once the mode is exited, we return the ports back to their previous state. i'm assuming that you want to force a specific port (or set of ports) on during the function and then return them to their previous state (or force them off) when the function/countdown finishes.

so, let's get you started (i'll help get the questions going and get you started and others will probably chime in as well).

First off, we need to know these questions:
  • How many ports or what ports do you want to toggle?
  • How long do you want the countdown to be? Do you want it to be changeable on the fly (like via a memory location)?
  • How do you want to activate it? Via a virtual relay port, custom variable value, memory location
Now, once we get this information, we can proceed forward with more of the details.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Countdown timer for relay

Post by lnevo »

I have something more specific. I use it for my swabbie. If I turn on the swabbie, it will run for 2 minutes.

Here's the code I use.

Code: Select all

void RunSwabbie() {
  int repeat=InternalMemory.read(Mem_B_SwabbieRepeat)*60;
  int runtime=InternalMemory.read(Mem_B_SwabbieTime)*60;
  static time_t t;
  
  // Manual mode
  if (ReefAngel.Relay.isMaskOn(Swabbie)) {
    ReefAngel.Relay.Auto(Swabbie);
    t=now();
  }  

  if (now()-t < runtime) {
    ReefAngel.Relay.On(Swabbie);
  } else {
    ReefAngel.DosingPumpRepeat(Swabbie,0,repeat,runtime);    
  }  
}
Here it is a bit more generic that you can put directly into your loop() function:

Code: Select all

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

  if (now()-t < runtime) {
    ReefAngel.Relay.On(Box1_Port2);
  } else {
    ReefAngel.DosingPumpRepeat(Box1_Port2,0,repeat,runtime);    
  }  
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.
paperdesk
Posts: 173
Joined: Fri May 23, 2014 8:09 am

Re: Countdown timer for relay

Post by paperdesk »

Aeesome, i will reply properly when I get back to my computer, don't like typing on the phone!
Image
paperdesk
Posts: 173
Joined: Fri May 23, 2014 8:09 am

Re: Countdown timer for relay

Post by paperdesk »

binder wrote:
paperdesk wrote:I didn't know where to put this for sure, but I guess this would be an app option.

I would like to be able to set a countdown timer to control a relay for a select amount of time. For example, I may want to fill a container with RODI water.

Currently I have a solenoid plugged into a relay. I can toggle the relay on and off to fill the container, but it would be much more convenient to be able to set the relay to be on for 30 minutes, or 60 minutes or whatever I like, then automatically shut off.

Has anyone done this? Is it possible? Is is something anyone else is even interested in?
yes this is possible. this is exactly what the feeding mode and water change modes do (well...water change mode is not a timed feature, but functions the same none the less).

it will require custom code being added. you will need to set the timer duration, ports to toggle and how you want to activate it. i would suggest looking at the feeding mode function inside the libraries to see how we handle it. what we do is retain the state of the existing relays that get toggled and then when the mode is entered, we turn off all the ports that are specified to be turned off and then leave the others alone. once the mode is exited, we return the ports back to their previous state. i'm assuming that you want to force a specific port (or set of ports) on during the function and then return them to their previous state (or force them off) when the function/countdown finishes.

so, let's get you started (i'll help get the questions going and get you started and others will probably chime in as well).

First off, we need to know these questions:
  • How many ports or what ports do you want to toggle?
  • How long do you want the countdown to be? Do you want it to be changeable on the fly (like via a memory location)?
  • How do you want to activate it? Via a virtual relay port, custom variable value, memory location
Now, once we get this information, we can proceed forward with more of the details.
I have only one port I want to toggle.
I would like to be able to change the countdown.
I'd like to control it from the app, not sure what options there are with the app other than toggling the relay.
Image
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Countdown timer for relay

Post by binder »

ok. using lnevo's sample we can make the app start the countdown by turning the port on.
then we can have it read the countdown value from the internal memory.

Sent from my Moto X
Lionfan
Posts: 166
Joined: Wed Nov 26, 2014 8:53 am

Re: Countdown timer for relay

Post by Lionfan »

Any way to get this out to everyone in an update?

Sent from my XT1254 using Tapatalk
Image
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Countdown timer for relay

Post by binder »

Lionfan wrote:Any way to get this out to everyone in an update?

Sent from my XT1254 using Tapatalk
well, that would require adding it to the libraries and making it versatile. I know it can be done but I don't know how fast it would get merged. I will see what I can do.

Sent from my Moto X
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Countdown timer for relay

Post by lnevo »

There's already the TimedPort class that can help with this.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Countdown timer for relay

Post by lnevo »

Code: Select all

TimedPort swabbie;

swabbie.Port=Box1_Port2;
swabbie.StartTime=120; // StartTime is when explicitly triggering the port vs the scheduled usage (Time)
swabbie.Run();

if (ReefAngel.Relay.IsMaskOn(swabbie.Port);
  ReefAngel.Relay.Auto(swabbie.Port);
  swabbie.Start();
}
Although I think I discovered a bug with the Repeat part which may inadvertently cause your port to go on when it shouldn't, but not sure. :) Looking into that. However, that should be available to anyone and there's a lot more functionality in that port.

I had hoped to extend it at some point by having a dosing pump class that would be aware of it's rate and how long it ran for. Curt I'd love if you'd take a look at the class and let me know what you think :)
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Countdown timer for relay

Post 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.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Countdown timer for relay

Post 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.
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Countdown timer for relay

Post 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 576 times
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: Countdown timer for relay

Post by binder »

I will take a look at it this week. kinda busy this week.

Sent from my Moto X
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Countdown timer for relay

Post by lnevo »

Awesome!
paperdesk
Posts: 173
Joined: Fri May 23, 2014 8:09 am

Re: Countdown timer for relay

Post 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?
Image
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Countdown timer for relay

Post by lnevo »

Yep
paperdesk
Posts: 173
Joined: Fri May 23, 2014 8:09 am

Re: Countdown timer for relay

Post 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();
Image
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Countdown timer for relay

Post by lnevo »

What error are you getting?
paperdesk
Posts: 173
Joined: Fri May 23, 2014 8:09 am

Re: Countdown timer for relay

Post 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.
Image
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Countdown timer for relay

Post 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...
paperdesk
Posts: 173
Joined: Fri May 23, 2014 8:09 am

Re: Countdown timer for relay

Post 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!
Image
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Countdown timer for relay

Post by lnevo »

Awesome. Can you post your final code that you used.
paperdesk
Posts: 173
Joined: Fri May 23, 2014 8:09 am

Re: Countdown timer for relay

Post 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.
Image
paperdesk
Posts: 173
Joined: Fri May 23, 2014 8:09 am

Re: Countdown timer for relay

Post 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);    
  }  
Image
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Countdown timer for relay

Post by lnevo »

Cool and triggering manually is working? What are you automating here?
paperdesk
Posts: 173
Joined: Fri May 23, 2014 8:09 am

Re: Countdown timer for relay

Post 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.
Image
User avatar
lnevo
Posts: 5430
Joined: Fri Jul 20, 2012 9:42 am

Re: Countdown timer for relay

Post 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.
Post Reply