TimedPort Class

Do you have a question on how to do something.
Ask in here.
User avatar
Posts: 5349
Joined: Fri Jul 20, 2012 9:42 am
PostPosted: Fri Jan 22, 2016 9:36 am
Hi everyone. I wanted to start a thread on the usage of the TimedPort class. This is essentially a Class to define a Relay with some properties. Right now it's similar to the DosingPumpRepeat function but with additional features. Here are some examples of things you could do

1. Have a defined runtime, offset, delay (like DelayedPort), repeat - Easy stuff
2. Have a defined StartTime and StopTime (say you want to shut off a pump but only for 10 minutes...) or you want to manually trigger a relay but only want it to run for 5 minutes (when it's normal programming is for 20)
3. Pause/Resume
4. Load/Save settings to/from memory

In the future, I would like to extend this class to a DosingPump class so that it could have parameters like Volume, and Rate and Calibration.

To start off, you will need to define the class

Code: Select all
TimedPort mainPump;

void setup() {
  mainPump.Init();
  mainPump.ID=Box1_Port1;
  mainPump.Time=60;
  mainPump.Repeat=120;
  mainPump.StartTime=720;
  mainPump.StopTime=15;
  mainPump.Scale=60; // in minutes (use 1 for seconds, 360 for hours...)
  mainPump.Save(100); // Will save in memory location 100-115 (16 bytes are required)
}


Now in order to have the port run the way we want, we need a call to mainPump.Run() in loop. Also since we're using memory to store the info, you need to refresh from memory also in loop

Code: Select all
void loop() {
 mainPump.Load(100);
 mainPump.Run();
}


So this will run the mainPump based on the settings I declared in setup. The pump will run for 1 hour and repeat every 2 hours.

If you wanted a 10 minutes offset, it's easy

Code: Select all
mainPump.Offset=10;


If you wanted a 5 minute delay after startup, or mode changes

Code: Select all
mainPump.Delay=5;


Now for me, this pump is the mixing pump for my saltwater station so when I do a new batch of salt, I want it to run for 12 hours, not the usual 60. I don't want to have to remember to turn it off 12 hours later... so we can do something like this:

Code: Select all
if (ReefAngel.Relay.isMaskOn(Port1)) {
  mainPump.Start();
  ReefAngel.Relay.Auto(Port1);
}


So now I've turn the port on and triggered the Start of the TimedPort. Based on the settings it will now run for 12 hours. I then reset the Relay Mask so it will not stay on forever.

The same could be done for Stop()

Code: Select all
if (ReefAngel.Relay.isMaskOff(Port1)) {
  mainPump.Stop();
  ReefAngel.Relay.Auto(Port1);
}


So now the pump will be disabled for 15 minutes based on the StopTime I have set. This is great if you want to turn off your skimmer because you glued some frags and don't want to forget to turn it back on for instance.

Now, the Run() command I mentioned earlier is only needed for managing the timed operations for start/stop and to auto run the schedule. You can also use this without using Run by using the normal Relay commands.

mainPump.On();
mainPump.Off();
mainPump.DelayedOn();

and another usage that is nice is Osc. Osc is similar to the Apex usage.

mainPump.Osc(0,60,60,true);

This will run with 0 offset, for 60 minutes on then 60 minutes off, and true for ON. This is actually called by the Run() command along with the other timer monitoring.

There is more commands that I did not discuss, but most are self explanatory, such as the following.

Toggle(), Pause(), Resume(), SetActive(), IsRunning(), IsStarted(), IsStopped(), IsActive()

Hope this helps people get started with this functionality.
User avatar
Posts: 2743
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
PostPosted: Sun Jul 08, 2018 7:46 pm
Can you post some full code of this in use?
I know you broke out different sections, but seeing a larger/complete code inside the loop() will help me get a better understanding of how you intended the functions to be called. (you don't need to include your entire loop(), just the relevant parts related to using this class).
User avatar
Posts: 5349
Joined: Fri Jul 20, 2012 9:42 am
PostPosted: Tue Jul 10, 2018 8:15 pm
so this was from my water mixing station. The main pump would basically run every other hour. If I started it manually, then it would run for 6 hours, and if I stopped the pump, it would stay off for 15 minutes then resume normal operation.

Code: Select all
// This in your globals
TimedPort mainPump,

//This is in your setup
  mainPump.Init();
  mainPump.ID=MainPump_Port;
  mainPump.Time=60;
  mainPump.Repeat=120;
  mainPump.StartTime=720;
  mainPump.StopTime=15;
  mainPump.Scale=60; // in minutes

// This in your loop
  mainPump.Run();

  if (TriggerPort(MainPump_Port, true)) {
    mainPump.Start();
  }

  if (TriggerPort(MainPump_Port, false)) {
    mainPump.Stop();
  }

  if (!ReefAngel.LowATO.IsActive() || !ReefAngel.HighATO.IsActive()) {
    mainPump.Off();
  }


Then Run function would do it's normal thing. The TriggerPort function looks for an override (off / on ) of a relay port. Returns the result then resets the relay to auto.
User avatar
Posts: 5349
Joined: Fri Jul 20, 2012 9:42 am
PostPosted: Tue Jul 10, 2018 8:53 pm
You could set the Time and Repeat to 0 and it would only run for the StartTime / StopTime when you override on or off respectively. So if you wanted something to run on demand for 10 minutes... or if you want your reactor to go off for half an hour while you change the media or something.

If you wanted the settings to be seconds, you could set the Scale to 1, if you wanted it in hours, you could set Scale to 3600. Them Time=1 would be one hour (or 3600 seconds)
User avatar
Posts: 2743
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
PostPosted: Wed Jul 11, 2018 6:09 pm
i was going to work on creating a dosing pump class that extended your class but it sounds like you already have done that based on your other post.


Sent from my iPad using Tapatalk
User avatar
Posts: 2743
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
PostPosted: Mon Aug 13, 2018 2:55 pm
I've been working on extending this class for a DosingPump class extending this class. One thing I noticed was that the StartTime and StopTime were INT variables and not double's. This "can" pose a problem if you have a runtime in seconds greater than 9 hours. This "should" not ever occur BUT it's possible that it would.
I'm going to tweak it in my code base for testing and will be able to push the changes to the main branch once tested in the future.
User avatar
Posts: 5349
Joined: Fri Jul 20, 2012 9:42 am
PostPosted: Tue Aug 14, 2018 3:20 pm
Cool, glad to see it used :) Yeah, that's probably a good catch. I don't know if my issue, but I know there was a bug somewhere. Plus I was using it for controlling a mixing pump so I was definitely working with values up to and over 12 hours at a time...
User avatar
Posts: 2743
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
PostPosted: Tue Aug 14, 2018 7:41 pm
lnevo wrote:Cool, glad to see it used :) Yeah, that's probably a good catch. I don't know if my issue, but I know there was a bug somewhere. Plus I was using it for controlling a mixing pump so I was definitely working with values up to and over 12 hours at a time...

well, the issue would be if you were using seconds for the scale and trying to go beyond 9 hours (actually 9.1 or more hours....rounded).

bug somewhere?? that’s not good. we don’t need bugs id i’m going to extend it. :-)


Sent from my iPad using Tapatalk

Return to How do I code ...

Who is online

Users browsing this forum: No registered users and 1 guest

cron