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 »

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