Dimming Module code

Expansion modules and attachments
Post Reply
RootsRockReefer
Posts: 13
Joined: Thu Mar 20, 2014 7:13 pm

Dimming Module code

Post by RootsRockReefer »

Searching for examples have not found much that nails this so looking for guidance.

So I am using my dimming module to drive external DC relays. I have managed to get them running with a simple on and off program using the saddle code programing with wizard using a delay of 0 seconds and picking a 5v on.

What I need to do now is configure a few of the outputs to

1) turn on and off at time increments like a wave maker

2) turn off in certain conditions like in feed mode or water change mode

3) turn off in alarm conditions like overheat

4) turn on or off with PH condition
rimai
Posts: 12857
Joined: Fri Mar 18, 2011 6:47 pm

Re: Dimming Module code

Post by rimai »

Several ways to do each one, but here are some examples:
0% to 100% every 5 seconds:

Code: Select all

if (now()%10<5)
ReefAngel.PWM.SetChannel(0,0);
else
ReefAngel.PWM.SetChannel(0,100);
Turn off in feeding mode:

Code: Select all

if (ReefAngel.DisplayedMenu==FEEDING_MODE) ReefAngel.PWM.SetChannel(0,0);
Turn off in wc mode:

Code: Select all

if (ReefAngel.DisplayedMenu==WATERCHANGE_MODE) ReefAngel.PWM.SetChannel(0,0);
Turn off on overheat

Code: Select all

if (bitRead(ReefAngel.AlerFlags,OverheatFlag)) ReefAngel.PWM.SetChannel(0,0);
Turn on based on pH value:

Code: Select all

if (ReefAngel.Params.PH>800) ReefAngel.PWM.SetChannel(0,100);
Roberto.
RootsRockReefer
Posts: 13
Joined: Thu Mar 20, 2014 7:13 pm

Re: Dimming Module code

Post by RootsRockReefer »

Roberto,
so here is the code for my dimming module. Channel 3 is the port I am attempting to set up with a 12 second on/off 24hrs a day, and turn off during feed mode; it is a DC optioned channel not a PWM. I used your code in the apply custom code here and channel3 wont operate after upload.

this is what channel 3 looks like now before attempted change.

ReefAngel.PWM.SetChannel( 3, PWMSlope(0,0,0,0,60,60,0,60) );
ReefAngel.PWM.SetChannel( 4, PWMSlope(18,0,12,0,60,60,0,60) );
ReefAngel.PWM.SetChannel( 5, PWMSlope(8,0,20,0,60,60,0,60) );

this is where and how I input the new code.
////// Place your custom code below here
if (now()%10<12)
ReefAngel.PWM.SetChannel(3,0);
else
ReefAngel.PWM.SetChannel(3,100);
if (ReefAngel.DisplayedMenu==FEEDING_MODE) ReefAngel.PWM.SetChannel(3,0);

does it need set up as a slope and not simply a 100? I am also thinking is the code placed in the wrong location and if placed in the custom code section, should the old channel 3 info be deleted?
rimai
Posts: 12857
Joined: Fri Mar 18, 2011 6:47 pm

Re: Dimming Module code

Post by rimai »

That's because the mathematical result of now()%10 is always going to be less than 12.
% is explained better in here:
http://arduino.cc/en/Reference/Modulo
So, if you want a full cycle (both on/off periods) to be 24 seconds, you need this:

Code: Select all

if (now()%24<12)
Roberto.
User avatar
lnevo
Posts: 5422
Joined: Fri Jul 20, 2012 9:42 am

Re: Dimming Module code

Post by lnevo »

The best explanation of the syntax when we do examples with now() and % I have is this

now() % repeat_time < runtime

Note that now() is time in seconds...if you want minutes or hours or days multiply the time values by SECS_PER_MINUTE or whatever your interval is...
RootsRockReefer
Posts: 13
Joined: Thu Mar 20, 2014 7:13 pm

Re: Dimming Module code

Post by RootsRockReefer »

thanks for the feedback Inevo & Roberto I appreciate it a lot!
RootsRockReefer
Posts: 13
Joined: Thu Mar 20, 2014 7:13 pm

Re: Dimming Module code

Post by RootsRockReefer »

For anyone else looking to do this and clarify one little thing.

if (now()%30<10)
ReefAngel.PWM.SetChannel(0,0);
else
ReefAngel.PWM.SetChannel(0,100);

The "runtime" is actually the first set channel command line before the else. so if the above lines are used the relay or channel is on for 20 seconds and off for 10 seconds.

I believe if you reversed the 0 & 100 it would reverse and be off for 20 seconds and on for 10. Not sure.

I placed the code in the put custom code here section, and removed the PWM.Set channel in the upper section where it has the PWM channels in series. Not sure if this matters but it is working as planned.
User avatar
lnevo
Posts: 5422
Joined: Fri Jul 20, 2012 9:42 am

Re: Dimming Module code

Post by lnevo »

You could also switch the < to a > to change the behavior :) Always a few ways to skin the cat... :)
Post Reply