Page 1 of 1

Dimming Module code

Posted: Fri May 09, 2014 2:03 pm
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

Re: Dimming Module code

Posted: Sat May 10, 2014 9:20 pm
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);

Re: Dimming Module code

Posted: Sun May 11, 2014 8:16 pm
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?

Re: Dimming Module code

Posted: Mon May 12, 2014 8:10 am
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)

Re: Dimming Module code

Posted: Mon May 12, 2014 12:04 pm
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...

Re: Dimming Module code

Posted: Mon May 12, 2014 1:13 pm
by RootsRockReefer
thanks for the feedback Inevo & Roberto I appreciate it a lot!

Re: Dimming Module code

Posted: Fri May 16, 2014 6:04 pm
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.

Re: Dimming Module code

Posted: Sat May 17, 2014 4:47 am
by lnevo
You could also switch the < to a > to change the behavior :) Always a few ways to skin the cat... :)