PWM ramp up and down

Do you have a question on how to do something.
Ask in here.
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: PWM ramp up and down

Post by rimai »

Pretty sure it already does that
Roberto.
ahmedess
Posts: 174
Joined: Sun May 22, 2011 2:29 pm

Re: PWM ramp up and down

Post by ahmedess »

I ve uploaded the program with the code you have just posted and now the LEDs should be at 100% but its at 0% now
ahmedess
Posts: 174
Joined: Sun May 22, 2011 2:29 pm

Re: PWM ramp up and down

Post by ahmedess »

here is the pwm code i placed in my pde:

Code: Select all

// Have PWM on from 4p to 1a, with gradual 60 minute ramp up and down starting at the given times
    // From 4p to 5p, the PWM will slowly ramp from 20% to 100%
    // From 12a to 1a, the PWM will slowly ramp from 100% to 20%
    ReefAngel.PWM.SetActinic(PWMSlope(16,0,1,0,20,100,60,ReefAngel.PWM.GetActinicValue()));
    ReefAngel.PWM.SetDaylight(PWMSlope(16,0,1,0,20,100,60,ReefAngel.PWM.GetDaylightValue()));
   
    if ((NumMins(hour(now()),minute(now())) < NumMins(16,0)) || (NumMins(hour(now()),minute(now())) > NumMins(1,0)))
   {
     ReefAngel.PWM.SetDaylight(0);
     ReefAngel.PWM.SetActinic(0);
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: PWM ramp up and down

Post by binder »

ahmedess wrote:thats great thanks a lot.

About starting up from a power outage I would like the controller to check the time and resume the pwm light schedule instead of using internal memory's stored value.
The controller should do that. What will happen is initially (as in right at startup), it will restore to what the internal memory is set at. Once it starts running through the loop of the functions, it will resume the PWM where it should be based on the PWM calculations.

That's how it has worked for me during all of my testing and I do believe that is how it has worked for everybody else.

curt
ahmedess
Posts: 174
Joined: Sun May 22, 2011 2:29 pm

Re: PWM ramp up and down

Post by ahmedess »

Curt,

Is there anything wrong with the code i posted above? I have this code placed in loop()
ahmedess
Posts: 174
Joined: Sun May 22, 2011 2:29 pm

Re: PWM ramp up and down

Post by ahmedess »

I don't have the drivers connected to my controller yet cause I ve ordered them and they are on their way. The only way I could tell if its working or not is through the display of AP and DP on the LCD screen and the display says they are both Zero when they re supposed to be 100. Is it possible that the displayed values are incorrect and the output is correct?
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: PWM ramp up and down

Post by binder »

ahmedess wrote:Curt,

Is there anything wrong with the code i posted above? I have this code placed in loop()
Yes, there is a problem with the code above. The additional IF statement is incorrect. That statement will always be true and always set your PWM to 0%.

Your code says: if the current time is before 4pm OR after 1am, set the PWM to 0%

When this is tested, just about every minute encountered will be after 1am and therefore cause that if statement to evaluate to true. The OR conditional indicates that if either expression is true, the entire condition will be true.

You have some options:
  • Remove the IF statement completely and leave the ramp up and down alone
  • Remove the IF statement completely and change the ramp up and down to be from 0% to 100%
  • Change the IF statement around. Since you are running the PWM from 4p to 1a, your logic in the if statement needs to be changed to work properly
Changing the IF statement around like this will yield the appropriate response:

Code: Select all

if ( (NumMins(hour(), minute()) > NumMins(1,0)) &&
     (NumMins(hour(), minute()) < NumMins(16, 0)) )
{
    ReefAngel.PWM.SetActinic(0);
    ReefAngel.PWM.SetDaylight(0);
}
This code says: If the current time is after 1am AND before 4pm, set the PWM to 0%. Otherwise don't touch it

So there are your options with the solutions to them all. :ugeek:

curt
ahmedess
Posts: 174
Joined: Sun May 22, 2011 2:29 pm

Re: PWM ramp up and down

Post by ahmedess »

I still get the displayed values on the LCD screen for AP and DP equal to zero when they should be 100. Is it possible that the problem is with the displayed values?
ahmedess
Posts: 174
Joined: Sun May 22, 2011 2:29 pm

Re: PWM ramp up and down

Post by ahmedess »

b_s_c1 wrote: One more question. Should the PWM reading on the RA display be the current output? Mine stays at 0% all the time but the ramp up works correctly.

I changed the scale to 0% to have the ramp up and down turn on and off the lights to avoid the inrush current on the drivers.
It seems that b_s_c1 had the same issue with the displayed values. the problem is that i dont have the LEDs at the moment to test whether the output is correct or not. if its the problem with the displayed value how can I fix it?
binder
Posts: 2871
Joined: Fri Mar 18, 2011 6:20 pm
Location: Illinois
Contact:

Re: PWM ramp up and down

Post by binder »

ahmedess wrote:I still get the displayed values on the LCD screen for AP and DP equal to zero when they should be 100. Is it possible that the problem is with the displayed values?
The display gets the value from the PWM every time it is updated. So the value is not just stored and displayed.

When I've tested the PWM slope function, I watched the values go up and down appropriately as others have experienced as well.

I do not have anything attached to the PWM to confirm or deny this but it should be working appropriately. My thoughts are that your IF statement is messing things up (even though I sent you an update for it, I never tested it). I want you to remove that IF statement (or comment it out or whatever) and run your code. I think you will find that it will work as it is supposed to. When you start changing the PWM outside the slope function, that's when things can start to get tricky and confusing and other undesired results happen.

Comment out the IF statement and test again (try the first 2 options of my previous post is what I'm saying).

curt
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: PWM ramp up and down

Post by rimai »

I've tested with this:

Code: Select all

    ReefAngel.PWM.SetActinic(PWMSlope(16,0,1,0,20,100,60,ReefAngel.PWM.GetActinicValue()));
    ReefAngel.PWM.SetDaylight(PWMSlope(16,0,1,0,20,100,60,ReefAngel.PWM.GetDaylightValue()));
   
if ( (NumMins(hour(), minute()) > NumMins(1,0)) &&
     (NumMins(hour(), minute()) < NumMins(16, 0)) )
{
    ReefAngel.PWM.SetActinic(0);
    ReefAngel.PWM.SetDaylight(0);
}
Here is the results:

4:00:00 = 0%
15:59:59 = 0%
16:00:00 = 20%
16:11:40 = 34%
16:27:10 = 56%
16:43.30 = 77%
17:16:40 = 100%
20:03:23 = 100%
23:59:59 = 100%
0:0:0 = 100%
0:11:04 = 86%
0:44:05 = 40%
0:50:08 = 34%
0:59:59 = 22%
1:00:00 = 20%
1:00:59 = 20%
1:01:00 = 0%
4:01:34 = 0%
09:56:48 = 0%
Roberto.
ahmedess
Posts: 174
Joined: Sun May 22, 2011 2:29 pm

Re: PWM ramp up and down

Post by ahmedess »

I have tried to ramp from 0 to 100 and I have tried to remove the IF statement but still no change.

Here is what I've found out:

After leaving the code running on the controller for 24 hrs, AP and DP values on the LCD screen have changed according to the correct schedule.

So this is what's happening, If the controller was powered on during the "LED on" period, lights will remain off until it goes through the cycle and starts a new one then it will turn on according to schedule. so it has to go through the on period then through the off period then it will turns on correctly.

Roberto, Is it possible that you run your test again but instead of begining on a time when the lights are off could you begin on a time when the lights are 100% and go through a cycle of "on" "off" "on" instead of an "off" "on" "off" cycle.
alexwbush
Posts: 327
Joined: Tue Mar 22, 2011 12:45 am
Location: San Diego, CA

Re: PWM ramp up and down

Post by alexwbush »

quick off topic question for you guys...

I want to switch to LEDs and meanwells only time to 15%... what setup are you guys using and where did you get it such that it can start at 0%??? Please feel free to PM me so I don't mess up this thread anymore.
ahmedess
Posts: 174
Joined: Sun May 22, 2011 2:29 pm

Re: PWM ramp up and down

Post by ahmedess »

I'm using Mean Well drivers. What I'm doing that I start from 0% then jump to 20% ramp to 100% then go back the same way. I m not using the drivers in the 0% to 20% range
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: PWM ramp up and down

Post by rimai »

My tests were with resetting the controller for every reading.
I'm not going to sit and wait the clock to get to the reading I needed :)
Roberto.
ahmedess
Posts: 174
Joined: Sun May 22, 2011 2:29 pm

Re: PWM ramp up and down

Post by ahmedess »

I thought you run the test using simulation. I know I've been bugging you and Curt with a lot of questions. Your help is really appreciated. I'm just trying to figure out why I m having this problem.

About the function:

Code: Select all

byte PWMSlope(byte startHour, byte startMinute, byte endHour, byte endMinute, byte startPWM, byte endPWM, byte Duration, byte oldValue)
{
	int Now = NumMins(hour(), minute());
	int Start = NumMins(startHour, startMinute);
	int StartD = Start + Duration;
	int End = NumMins(endHour, endMinute);
	int StopD = End - Duration;

	if ( Now >= Start && Now <= StartD )
		return constrain(map(Now, Start, StartD, startPWM, endPWM),startPWM, endPWM);
	else if ( Now >= StopD && Now <= End )
	{
		byte v = constrain(map(Now, StopD, End, startPWM, endPWM),startPWM, endPWM);
		return endPWM-v+startPWM;
	}
	else if ( Now > StartD && Now < StopD )
		return endPWM;

    // lastly return the existing value
    return oldValue;
}
I m trying to understand its code. this function returns two values, one is oldValue and the other is depending on the if statment. lets say this condition is true " else if ( Now > StartD && Now < StopD ) " then the function will return with oldValue and endPWM. are their values any different? Also what is the use of oldValue in this function?
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: PWM ramp up and down

Post by rimai »

Almost there.
When the code finds a return statement, it just exits the function returning whatever it is passed after the return.
Som, in the case you mentioned, it would return endPWM.
The oldValue is one of the input parameters of PWMSlope function

Code: Select all

byte PWMSlope(byte startHour, byte startMinute, byte endHour, byte endMinute, byte startPWM, byte endPWM, byte Duration, byte oldValue)

It will only return oldValue if none of the 3 if statements are true.
Roberto.
ahmedess
Posts: 174
Joined: Sun May 22, 2011 2:29 pm

Re: PWM ramp up and down

Post by ahmedess »

I have found out something very interesting. I wanted to test if PWMSlope() was returning oldValue for some reason so i changed the function's input parameters to this:

ReefAngel.PWM.SetActinic(PWMSlope(16,0,1,0,20,100,60,100));

instead of this:

ReefAngel.PWM.SetActinic(PWMSlope(16,0,1,0,20,100,60,ReefAngel.PWM.GetActinicValue()));

and then compiled and loaded the code to the controller and the result was AP=100% while the daylight function which was left as it is was at zero.

There is a bug that causes the function to return oldValue when it should return endPWM and this only happens if you schedule endHour to be smaller than startHour in value. In my schedule StartD= 1020mins and StopD=0mins when you do this comparison
if ( Now > StartD && Now < StopD )
and lets say Now=1200mins which should return true for this condition returns False because Now is not less than StopD
ahmedess
Posts: 174
Joined: Sun May 22, 2011 2:29 pm

Re: PWM ramp up and down

Post by ahmedess »

I have modified the PWMSlope function a bit incase any one wants to set his LEDs off time after midnight. here is the modified function:

Code: Select all

byte PWMSlope(byte startHour, byte startMinute, byte endHour, byte endMinute, byte startPWM, byte endPWM, byte Duration, byte oldValue)
{
	int Now = NumMins(hour(), minute());
	int Start = NumMins(startHour, startMinute);
	int StartD = Start + Duration;
	int End = NumMins(endHour, endMinute);
	
//Time correction if end time chosen is after midnight

	if ( Start > End )
	{
		End= End+1440;	// 1440mins = 24hr*60mins	
	}

	int StopD = End - Duration;

	if ( Now >= Start && Now <= StartD )
		return constrain(map(Now, Start, StartD, startPWM, endPWM),startPWM, endPWM);
	else if ( Now >= StopD && Now <= End )
	{
		byte v = constrain(map(Now, StopD, End, startPWM, endPWM),startPWM, endPWM);
		return endPWM-v+startPWM;
	}
	else if ( Now > StartD && Now < StopD )
		return endPWM;

    // lastly return the existing value
    return oldValue;
}
rimai
Posts: 12881
Joined: Fri Mar 18, 2011 6:47 pm

Re: PWM ramp up and down

Post by rimai »

Did this solve the issue you were having?
Roberto.
ahmedess
Posts: 174
Joined: Sun May 22, 2011 2:29 pm

Re: PWM ramp up and down

Post by ahmedess »

sadly, i think I ve just noticed that the ramping down is not working so I m gonna try working on it tomorrow
Post Reply