Go Down

### Topic: Please help with aquarium lights - one more question- solved (Read 2694 times)previous topic - next topic

#### muda

##### Aug 09, 2011, 10:36 pmLast Edit: Aug 12, 2011, 08:37 pm by muda Reason: 1
Hi all.
Not many days passed, since I discovered arduino's wonders, so still not strong enough  to talk to it.
Hope you guys can help me solve this puzle.
I have variation of popular controler for reef lighting .Originally written by "Christian".  I will demonstrate idea. Not whole sketch , just essentials :

Code: [Select]
`int blue = 10;             // blue  LEDs connected to digital pin 10 (pwm)int white = 11;            // white LEDs connected to digital pin 11 (pwm)int bluepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };int whitepercent[11] = { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255 };  for (int i = 1; i <= 10; i++) // setting ib value for 10% increment. Start with 0% - at given time blue leds fade in from 0 to 255 analogWrite(blue, bluepercent[i]);lcd.setCursor(13, 1);lcd.print(i);  for (int i = 1; i <= 10; i++) // setting i value for 10% increment. Start with 0% - after blues are at 100% , whites start to fade in analogWrite(white, whitepercent[i]);lcd.setCursor(18, 1);lcd.print(i);`

As  you see blue lights come in first and fade in following  " bluepercent" schedule. After it finishes , whites start  fading in folowing its own " whitepercent" schedule which is same in my case.

What I want to change , is that I want  both lights fading in  at the same time but different schedules : like

int bluepercent[11] =  { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255, 255, 255, 255, 255 };
int whitepercent[11] = { 0, 0,  0,  0,  0,   26,  52,  78,  103, 128, 154, 180, 205, 230, 255 };

"11" as i understand is not the right number anymore.And  thats all what I understand at the moment \$
And how to make LCD to show real degree of each  light intensity in this case ?  Better 0 as min  to 10 as max. LCD space is limited

Thanks in advance to anyone, who will bother to help or just read to this word.

#### EinTyp

#1
##### Aug 10, 2011, 01:27 am
I can not help you with the fading at the same time and the LCD output.

But the thing with the array I can explain.

Code: [Select]
`int bluepercent[11] =  { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255, 255, 255, 255, 255 };int whitepercent[11] = { 0, 0,  0,  0,  0,   26,  52,  78,  103, 128, 154, 180, 205, 230, 255 };`

Dont work anymore since with "int bluepercent[ x ]" you are create an array which can store x values. In your code the amount of those values exceeds 11. For it to work you have to change 11 to 15.

Code: [Select]
`int bluepercent[15] =  { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255, 255, 255, 255, 255 };int whitepercent[15] = { 0, 0,  0,  0,  0,   26,  52,  78,  103, 128, 154, 180, 205, 230, 255 };`

I hope someone else with more knowledge can help you out with the rest of your problem.

-EinTyp

#### muda

#2
##### Aug 10, 2011, 09:13 am
Hi,

Thanks for input. That was exactly one thing what I understood by myself
May be I need to paste more of the sketch  ?

Can anybody explain what is (i). If I can give different names for this like (i) and (f)  , then I can run both at the same time ?

#### David Pankhurst

#3
##### Aug 10, 2011, 10:44 amLast Edit: Aug 10, 2011, 10:42 pm by David Pankhurst Reason: 1
You can have them work together by putting them in the same loop:

Code:

Code: [Select]
`int bluepercent[] =  { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255, 255, 255, 255, 255 };int whitepercent[]=  { 0,  0,  0,  0,   0,  26,  52,  78, 103, 128, 154, 180, 205, 230, 255 };for (int i = 0; i <sizeof(bluepercent)/sizeof(bluepercent[0]); i++){  analogWrite(blue, bluepercent[i]);  analogWrite(white, whitepercent[i]);}`

Is that what you mean?

The item "sizeof(bluepercent)/sizeof(bluepercent[0])" is a way to indicate your array length without putting a specific value in it - as you add/remove from the bluepercent array, the count will always be correct. As well, 'i' can start counting at 0, not 1.

Also, if you keep them in the same loop the two arrays must have the same number of items - I've formatted them so they line up so it's easier to check visually.

#### PaulS

#4
##### Aug 10, 2011, 12:05 pm
OP: Note the { and } around the code to be executed in David's example. Note that they are missing in your code. Your code will loop through all the values in the array, changing the brightness of the LED, and then print to the LCD.

Presumably, you want the LED to stay at that brightness for some period of time. Neither your code nor David's will do that. Both will simply run the array as fast as possible.

#### muda

#5
##### Aug 10, 2011, 04:07 pm
Thanks David ,

Thats exactly what I wanted. I will try it and report back how things are going.

Thanks Paul,

There are other procedures in the code too, that are responsible for fading sped, start time etc. ... I just wanted to be simple here .

#### muda

#6
##### Aug 10, 2011, 05:10 pmLast Edit: Aug 10, 2011, 05:14 pm by muda Reason: 1

The item "sizeof(bluepercent)/sizeof(bluepercent[0])" is a way to indicate your array length without putting a specific value in it - as you add/remove from the bluepercent array, the count will always be correct. As well, 'i' can start counting at 0, not 1.

David,

This value is used to calculate many other parameters. Now it become too complicated as is very masive. For example :

if (((ontime * 60) + photoperiod + whiteramptime + 2*blueramptime + (blueramptime/10*9)) >= daybyminute)

Now i need to use "sizeof(bluepercent)/sizeof(bluepercent[0])"  instead of 10. And "sizeof(bluepercent)/sizeof(bluepercent[0])" -1  instead of 9.

becomes :  if (((ontime * 60) + photoperiod + whiteramptime + 2*blueramptime + (blueramptime/sizeof(bluepercent)/sizeof(bluepercent[0])*sizeof(bluepercent)/sizeof(bluepercent[0])-1)) >= daybyminute)

Seems crazy to me    Please advice on this.

Can I do like that:

Int array;
array = sizeof(bluepercent)/sizeof(bluepercent[0]);
if (((ontime * 60) + photoperiod + whiteramptime + 2*blueramptime + (blueramptime/array*(array-1))) >= daybyminute)

#### PaulS

#7
##### Aug 10, 2011, 05:28 pm
Quote
Seems crazy to me  Please advice on this.

The compiler doesn't care. If you do, use intermediate variables.

Quote
Can I do like that:

Int array;
array = sizeof(bluepercent)/sizeof(bluepercent[0]);
if (((ontime * 60) + photoperiod + whiteramptime + 2*blueramptime + (blueramptime/array*(array-1))) >= daybyminute)

Will, it should be int, not Int, and array as the name of the variable, when it contains size data, doesn't make sense, but, yes that is the general idea.

#### David Pankhurst

#8
##### Aug 10, 2011, 10:41 pmLast Edit: Aug 10, 2011, 10:44 pm by David Pankhurst Reason: 1
It's true - to reuse that value in more than one place is unwieldy, and so you have the right idea:

Code: [Select]
`int bluepercent[] =  { 0, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255, 255, 255, 255, 255 };int whitepercent[]=  { 0,  0,  0,  0,   0,  26,  52,  78, 103, 128, 154, 180, 205, 230, 255 };int total=sizeof(bluepercent)/sizeof(bluepercent[0]); // <--for (int i = 0; i <total; i++) // <--{  analogWrite(blue, bluepercent[i]);  analogWrite(white, whitepercent[i]);}`
Now you can use 'total' throughout your code to indicate the number of items - I'd recommend it over 'array', which may be confusing at 4am in the morning when you wonder 'what is THAT variable for'?

One advantage of using total here is that the whole code doesn't need a change every time you do something - as long as total is updated, you're safe.

#### muda

#9
##### Aug 11, 2011, 10:26 amLast Edit: Aug 11, 2011, 02:20 pm by muda Reason: 1
Thanks guys,

Now trying to implement . Got bunch of errors, need to deal with them. Can take time for expert like me

OK finally fighted all troubles. And you know what - ALL WORKS BRILLIANTLY. Exactly as I wished - big thanks . My brain is boiling, but was worth.

Now would you please assist me integrating light output printing to LCD? Before simply (i) value was printed 1 to 10, as white and blue proceses were separate and had 10 steps each. Now doing same is rather non informative. It would be nicer to display bluepercent and whitepercent  values in as %  (0   to 99 as only two digit space available).
Now my sketch looks like this:

Code: [Select]
`int bluepercent[] =  { 13, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255, 255, 255, 255, 255, 255 };int whitepercent[]=  { 0,  0,  0,  6,  13,  26,  52,  78, 103, 128, 154, 180, 205, 230, 255, 255 };int abc(sizeof(bluepercent)/sizeof(bluepercent[0])); if (daybyminute >= (ontime*60))   { if (daybyminute < ((ontime*60) + ramptime))            //if time is in range start fading in    {           int i;      for (int i = 0; i < abc; i++){  analogWrite(blue, bluepercent[i]);  analogWrite(white, whitepercent[i]);       lcd.setCursor(13, 1);  // for blue LEDs       lcd.print( here is my problem );                                              lcd.setCursor(18, 1);  // for white LEDs       lcd.print( and here );              int countdown = ((rampup*60)/abc);       while (countdown>0)        {          onesecond();          countdown--;        }       }     }    }    `

Do you have any ideas ?

#### muda

#10
##### Aug 12, 2011, 08:33 am
I try to use "bluepercent" and "whitepercent" variables for calculation, but only  get bunch of errors. I understood this is not allowed to use it directly ?

#### wildbill

#11
##### Aug 12, 2011, 12:45 pm
Post your code & errors, it'll be easier to help you with more data.

#### muda

#12
##### Aug 12, 2011, 05:23 pmLast Edit: Aug 12, 2011, 05:29 pm by muda Reason: 1
Code: [Select]
`int bluepercent[] =  { 13, 26, 52, 78, 103, 128, 154, 180, 205, 230, 255, 255, 255, 255, 255, 255 };int whitepercent[]=  { 0,  0,  0,  6,  13,  26,  52,  78, 103, 128, 154, 180, 205, 230, 255, 255 };int abc(sizeof(bluepercent)/sizeof(bluepercent[0]));          int i;      for (int i = 0; i < abc; i++){  analogWrite(blue, bluepercent[i]);  analogWrite(white, whitepercent[i]);       lcd.setCursor(13, 1);  // for blue LEDs       lcd.print (bluepercent/255*100 );                  // this line                         lcd.setCursor(18, 1);  // for white LEDs       lcd.print( whitepercent/255*100 );                   // this linee too}`

both lines get -" error: invalid operands of types 'int [16]' and 'int' to binary 'operator/'"

EDIT:  would it work  better this way  ?  Im not sure if I understand what "i"  stands for :
Code: [Select]
` lcd.print( whitepercent [i] /255*100 );`

#### PaulS

#13
##### Aug 12, 2011, 05:54 pm
Quote
lcd.print (bluepercent/255*100 );                     // this line

You are trying to pass the whole array to an operation that wants one element of the array.

Quote
EDIT:  would it work  better this way  ?  Im not sure if I understand what "i"  stands for :

Yes. The [ i ] is how you reference the ith element in the array. In this case, the i is the loop index.

#### wildbill

#14
##### Aug 12, 2011, 08:05 pm
and shouldn't this:
Code: [Select]
`int abc(sizeof(bluepercent)/sizeof(bluepercent[0]));`
be this?
Code: [Select]
`int abc=(sizeof(bluepercent)/sizeof(bluepercent[0]));`

Go Up

Please enter a valid email to subscribe