Another Arduino LED light schedule sketch - Page 2 - The Planted Tank Forum
 2Likes
Reply
 
LinkBack Thread Tools Display Modes
post #16 of 100 (permalink) Old 05-21-2013, 06:45 AM Thread Starter
Algae Grower
 
benjaf's Avatar
 
Join Date: Mar 2012
Location: Denmark
Posts: 111
Glad to hear someone is using, or at least attempting to use, this!
If you can send me a copy of your code, I'd be glad to look it over. The original has not been tested with more than 2 channels, so bugs are by no means impossible.

By the way:
I have been doing some re-factoring to the code to make it more user friendly, but have not had a chance to test it yet.

Last edited by benjaf; 05-21-2013 at 12:09 PM. Reason: Added some
benjaf is offline  
Sponsored Links
Advertisement
 
post #17 of 100 (permalink) Old 05-22-2013, 09:40 AM Thread Starter
Algae Grower
 
benjaf's Avatar
 
Join Date: Mar 2012
Location: Denmark
Posts: 111
UPDATE:
The sketch has been updated, hopefully this should make it much easier to add channels.
Any place in the code where you have to change anything (2 places) is marked with ¤CHANGE¤
benjaf is offline  
post #18 of 100 (permalink) Old 05-24-2013, 09:18 PM
Newbie
 
Join Date: May 2013
Location: Deutschland
Posts: 3
benjaf,

i just registered to say THANK YOU.
I have been hacking on my ardunio for a while now and everything was working fine....
Menu and submenus, Light saving on display (switch of 1 minute after last button pressed), RTC, temperature reading.
but i had problems with the light controls.

first i tried to use TimeAlarms to set alarms when to trigger a new end value and then count down or up every 5 seconds... could have worked good, but i also have some manual settings in my menu.
for example all light off or at 25%, 50%, 75%, 100% or automatic (automatic was using the events of TimeAlarm)

it was working, but after going from manual mode to automatic it was missing the events and did not calculate the current value for the light.

you solved my biggest issue with my arduino!

with kind regards
dookie
Dookie is offline  
post #19 of 100 (permalink) Old 05-24-2013, 09:37 PM
Newbie
 
Join Date: May 2013
Location: Deutschland
Posts: 3
Oh,

just a quick question:
Do you have any idea why my lamp is on 0 now? time 22:33 with following channel config:
Channels[0].Pin = 13;
Channels[0].AddPoint(6, 0, 1);
Channels[0].AddPoint(8, 0, 1);
Channels[0].AddPoint(8, 30, 255);
Channels[0].AddPoint(11, 30, 255);
Channels[0].AddPoint(12, 0, 0);
Channels[0].AddPoint(17, 0, 0);
Channels[0].AddPoint(17, 30, 255);
Channels[0].AddPoint(22, 0, 255);
Channels[0].AddPoint(22, 30, 1);
Channels[0].AddPoint(0, 59, 1);
Channels[0].AddPoint(1, 00, 0);

The reason why i sometimes use 1 and 0 as intensity is because when it is 0 i set another digital output to switch off power completely. so with 1 it is supposed to be moon light.

Regards
Dookie
Dookie is offline  
post #20 of 100 (permalink) Old 05-25-2013, 06:41 AM Thread Starter
Algae Grower
 
benjaf's Avatar
 
Join Date: Mar 2012
Location: Denmark
Posts: 111
First of all, you're welcome!
The reason your schedule does not work, is that you can't define something after 24:00. If the schedule does not end in 24, it will assume lights are off after last 'valid' entry.
What you have to do is move the last 2 entries to the top like this:
(0, 0, 1)
(0, 59, 1)
(1, 0, 0)
(5, 59, 0)
and add (24, 0, 1) to the end. That should to the trick! :-)
benjaf is offline  
post #21 of 100 (permalink) Old 05-25-2013, 09:11 AM
Newbie
 
Join Date: May 2013
Location: Deutschland
Posts: 3
oh ok, i will try that.

Thanks!
Dookie is offline  
post #22 of 100 (permalink) Old 05-29-2013, 05:01 AM
Algae Grower
 
Join Date: Jun 2012
Location: Australia
Posts: 16
Hi benjaf, great job here.

Looking to modify my Arduino sketch to contain more light periods. I want to split the photoperiod in two.

Can I assume that your code here does that?

Quote:
Channels[0].AddPoint(8, 0, 0);
Channels[0].AddPoint(8, 30, 255);
Channels[0].AddPoint(11, 0, 0);
Channels[0].AddPoint(11, 10, 0);
Channels[0].AddPoint(13, 0, 0);
Channels[0].AddPoint(13, 10, 255);
Channels[0].AddPoint(20, 0, 255);
Channels[0].AddPoint(20, 30, 0);
From my understanding it goes from 0% (0) to 100% (255) from 8:00 to 8:30 and then it's at 0% again at 11. Does that mean that it fades down from 100% at 8:30 to 0% at 11:00? So the fade period there is 30 minutes up and then 2.5 hours down? Or does the light just switch straight off at 11:00?


Why do you also include another 0% marker at 11:10? Is this just redundancy to make sure it's totally off?

Good work though. Simpler than mine and quite nice.
Simpsoid is offline  
post #23 of 100 (permalink) Old 05-29-2013, 05:11 AM Thread Starter
Algae Grower
 
benjaf's Avatar
 
Join Date: Mar 2012
Location: Denmark
Posts: 111
Ah, sorry, that is a typo. It was supposed to be (11, 0, 255)! Hope it makes a bit more sense now :-)
benjaf is offline  
post #24 of 100 (permalink) Old 05-30-2013, 03:05 AM
Algae Grower
 
Join Date: Jun 2012
Location: Australia
Posts: 16
Quote:
Originally Posted by benjaf View Post
Ah, sorry, that is a typo. It was supposed to be (11, 0, 255)! Hope it makes a bit more sense now :-)
Yep I understand now. The fade up for the "sunrise" is 30 minutes. Then a quick fade down of 10 minutes for a "midday rest". Fade up of 10 minutes "midday wake" then a final 30 minute fade down for a "sunset".

Nicely done.

Now one thing you may have noticed that I can't really seem to crack is that the fade looks a bit bright early on.

It turns out that human eyes perceive light in the logarithmic scale, yet our fades are linear. As such things get to full bright at around 25% of the fade cycle and stay that way until 100%

You may have noticed this. That there's not really a "dawn breaking" effect. It's more dawn for a few minutes then fully bright for the rest. That's because our eyes perceive the full brightness at around 25% and from then on it's still full brightness.

Here are the correct values for a true logarithmic fade from 100% to 0% for a 1 byte (255) range:

Code:
255, 180, 128, 90, 64, 45, 32, 23, 16, 12, 8, 6, 4, 3, 2, 1,0
If you implement these values instead of linearly fading from 100% to 0% (via some sort of lookup array) then you will have a nice fade over time.

I tried to do a calculation that would work out the proper PWM value given a minimum and maximum range (so instead of 100% to 0% if I wanted to fade from 80% to 10%) but I couldn't get one worked out (my maths is poor).

See what you think of the logarithmic fade instead of linear. It's a more gradual effect and I'm sure it's not an issue for the plants. It just means the dimmer values are held a bit longer and so the lights don't look full bright too quickly.
Simpsoid is offline  
post #25 of 100 (permalink) Old 05-30-2013, 07:39 AM
Algae Grower
 
Join Date: Jun 2012
Location: Australia
Posts: 16
So I think I have finalised adding the code to process the linear vs logarithmic scale to my project and thought it could help you and others out.

Here are my results:


With the following values (in case anyone wants to use an array lookup).
Percentage on left, PWM on right:
HTML Code:
1	0
2	0
3	0
4	0
5	0
6	0
7	0
8	1
9	1
10	1
11	1
12	1
13	1
14	1
15	1
16	1
17	2
18	2
19	2
20	2
21	2
22	2
23	3
24	3
25	3
26	3
27	3
28	4
29	4
30	4
31	4
32	5
33	5
34	5
35	6
36	6
37	7
38	7
39	8
40	8
41	9
42	9
43	10
44	10
45	11
46	12
47	12
48	13
49	14
50	15
51	16
52	17
53	18
54	19
55	20
56	21
57	22
58	23
59	25
60	26
61	28
62	30
63	31
64	33
65	35
66	37
67	39
68	42
69	44
70	47
71	49
72	52
73	55
74	58
75	62
76	65
77	69
78	73
79	77
80	82
81	87
82	92
83	97
84	102
85	108
86	115
87	121
88	128
89	136
90	143
91	152
92	160
93	169
94	179
95	190
96	200
97	212
98	224
99	237
100	250
I sourced this a while ago from THIS SITE

The formula is as follows. It accepts an integer input (0% - 100%) and will output a byte (0 - 255) which can be written directly to the PWM ports.
Code:
byte linearPWM(int percentage)
{
	// coefficients
	double a = 9.7758463166360387E-01;
	double b = 5.5498961535023345E-02;
	return (byte)(floor((a * exp(b * percentage) + 0.5)) -1);
}
Simpsoid is offline  
post #26 of 100 (permalink) Old 06-03-2013, 09:58 AM Thread Starter
Algae Grower
 
benjaf's Avatar
 
Join Date: Mar 2012
Location: Denmark
Posts: 111
I have actually been considering adding this, or at least a slightly simplified version of it for some time.
I have been doing some more rewrites to the project in order to simplify usage even more - and will probably add 'perceived linear' fading as an option for each channel. This code is not yet done, but testing is under way. Thanks for doing some of the numbers!
benjaf is offline  
post #27 of 100 (permalink) Old 11-15-2013, 04:32 AM
Algae Grower
 
Join Date: May 2013
Location: Chicago Suburb
Posts: 26
Hi....it's me again. I never got back to thank you for this code, or to let you know I got it up and running without any issues across 9 channels with 3 periods.

I've been running it as my default lighting schedule since the end of May. It's still the older version, but I don't have any issues with it. And I really don't plan to change it.

My question this time is about the timing/intensity matrix.

Is it possible to use a variable within it? Specifically the Intensity?

Current Example:

{
{0, 0, 9, 45, 0},
{10, 45, 16, 55, 90}, ////Wht_Ch1
{17, 55, 24, 0, 0}
},{

I am trying to pass a variable(without success) from a touch "dimming/blending" screen so I will be able to change it without changing the sketch every time.

Something like this:

{
{0, 0, 9, 45, 0},
{10, 45, 16, 55, White_Max}, ////Wht_Ch1
{17, 55, 24, 0, 0}
},{

I figured it would be as simple as passing the variable value into the matrix like that, but it has not. I've tried various ways and variables with out success.

Will the schedule only accept a number between 0 and 255?

Thanks for any help or ideas!

EDIT: here is a photo to try and make clear what I am talking about.

This is a control screen where I can currently dim and blend each of my LED colors.
I would like to be able to set maximum value to the lighting schedule from here.


Last edited by AquaPorn; 11-15-2013 at 04:46 AM. Reason: Added photo for clarity
AquaPorn is offline  
post #28 of 100 (permalink) Old 11-15-2013, 07:32 AM Thread Starter
Algae Grower
 
benjaf's Avatar
 
Join Date: Mar 2012
Location: Denmark
Posts: 111
You're welcome, happy to see someone is still using it
Nice interface by the way!
Anyway, on to what you are trying to do:

Passing a variable that way just passes the value of the variable at that given time.
To change the schedule after initialization you have to change the value directly in the matrix like this:

lightMatrix[Channel][Period][INTENSITY] = Variable;

Assuming you want to change period 2 in the first channel:
Code:
lightMatrix[0][1][INTENSITY] = White_Max;
Remember that array indexes start at 0, otherwise results will be rather odd...

EDIT: This must be done every time the variable is changed! (wasn't sure this came across in the original post)

The downside of doing what you do now - any changes will be lost at reset unless you have made some considerable changes to the sketch.
This will be remedied by an upcoming API edition (currently testing) but it will most likely be released as a separate sketch since it has very little in common with the current version..

Last edited by benjaf; 11-15-2013 at 07:37 AM. Reason: Clarification
benjaf is offline  
post #29 of 100 (permalink) Old 11-15-2013, 07:15 PM
Algae Grower
 
Join Date: May 2013
Location: Chicago Suburb
Posts: 26
Thanks so much for the quick response, and compliment.

Makes perfect sense to me now. You know...sometimes I get in a rut thinking a specific solution that I can't get by that, and I just need a slap in the head to see things another way.

I'm going to give it a go.

I'm also thinking that I might be able to solve the reset/power loss issue by saving the max lighting value in EEPROM or SD card when the user pushes "enter" on my dimming screen. But, I'll cross that bridge when I get to it.

Another bridge down the road is setting the start time. The total period would be fixed(easier math). Now that I see how I have to designate the variable I hope I can bang that code out too.

Thanks again!!
AquaPorn is offline  
post #30 of 100 (permalink) Old 11-16-2013, 08:45 AM Thread Starter
Algae Grower
 
benjaf's Avatar
 
Join Date: Mar 2012
Location: Denmark
Posts: 111
Saving to EEPROM on change and then reading on startup is fairly straightforward. The version I am working on saves the entire schedule in EEPROM and reads next point on demand - it's not that difficult if you keep it simple.
benjaf is offline  
Reply

Tags
arduino, diy, led

Quick Reply
Message:
Options

Register Now



In order to be able to post messages on the The Planted Tank Forum forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.

User Name:
Password
Please enter a password for your user account. Note that passwords are case-sensitive.

Password:


Confirm Password:
Email Address
Please enter a valid email address for yourself.

Email Address:
OR

Log-in









Human Verification

In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.



Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page
Display Modes
Linear Mode Linear Mode



Posting Rules  
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

 
For the best viewing experience please update your browser to Google Chrome