Please help with aquarium lights - one more question- solved

Hi all.
Not many days passed, since I discovered arduino's wonders, so still not strong enough to talk to it. :fearful:
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 :

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 $ :blush:
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 :slight_smile:

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

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.

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.

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

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 ?

You can have them work together by putting them in the same loop:

Code:

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.

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.

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 .

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 + 2blueramptime + (blueramptime/109)) >= 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 :grin: Please advice on this.

Can I do like that:

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

Seems crazy to me Please advice on this.

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

Can I do like that:

Int array;
array = sizeof(bluepercent)/sizeof(bluepercent[0]);
if (((ontime * 60) + photoperiod + whiteramptime + 2blueramptime + (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.

It's true - to reuse that value in more than one place is unwieldy, and so you have the right idea:

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.

Thanks guys,

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

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:

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 ?

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 ?

Post your code & errors, it'll be easier to help you with more data.

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 :  lcd.print( whitepercent [i] /255*100 );

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.

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.

and shouldn't this:

int abc(sizeof(bluepercent)/sizeof(bluepercent[0]));

be this?

int abc=(sizeof(bluepercent)/sizeof(bluepercent[0]));

Yes that worked. Thanks all for help.

Case closed