My LED Reef Tank Lighting project need your help!

Hello community,

I 'm working from few month on a LED lighting project controled by a spark for my reef tank.

I have 2 issues that i can’t solve. Maybe you are an expert in C and you can help me :smile:

first issue :
when i call my sunrise or sunset function via the webAPI, i always have a timeout message . My function can take long time to execute, but is it possible to send a return code when the function start his job ?

second issue :
the sunrise and sunset function are not smooth. Sunrise has some flash ( led are flashing one time before starting fade in ) and Sunset stop all light, but don’t fade .

the code :

    // Light Reef Tank project

/* remote commands

// make sunrise
curl -k https://api.spark.io/v1/devices/50ff6xxxxxx/sunlight \
     -d access_token=xxx \
     -d "args=sunrise"
     
// make sunset
curl -k https://api.spark.io/v1/devices/50ff6xxxxxx/sunlight \
     -d access_token=xxx \
     -d "args=sunset"
     
// get last command
curl -k "https://api.spark.io/v1/devices/50ff6xxxxxx/lastcommand?access_token=xxx"

*/


// pin of white lines and pwm value
int8_t white[3][2]={{A5, 0}, {A6, 0}, {A7, 0}};
// pin of blue lines and pwm value
int8_t blue[3][2]={{A0, 0}, {A1, 0}, {A4, 0}};


// time for sunrise/sunset in minutes
unsigned int fadetime = 1;

// maximum Sun intensity in percent
unsigned int sunmaxintensity = 100;

// maximum Moon intensity in percent
unsigned int moonintensity = 1;

// moon line (blue[1])
unsigned int moonline = 1;

// String to hold last command issued
char lastCommand[64] = "NONE";

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//declare global if you don't want it reset every time loop runs
//elapsedMillis timeElapsed = 0; 
elapsedMillis timeElapsed2 = 0; 

// nb white lines
unsigned int nbwhite = 3; //sizeof(white);
// nb blue lines
unsigned int nbblue = 3; //sizeof(blue);


// maximum intensity from 0 to 255
unsigned int maxint = ( sunmaxintensity * 255 / 100 );

// delay in milliseconds between two fade value
unsigned int btwfadeval = ( fadetime * 60 * 1000 ) / ( ( nbwhite + nbblue ) * maxint );




// This routine runs only once upon reset
void setup() 
{
  // Initialize white pins as an output
    for (int i = 0; i <= ( nbwhite - 1 ); i++){
        pinMode(white[i][0], OUTPUT);
    }
  // Initialize blue pins as an output
    for (int i = 0; i <= ( nbblue - 1 ); i++){
        pinMode(blue[i][0], OUTPUT);
    }
    
    // Expose Sun() to the cloud
    Spark.function("sunlight", SunLight);
    
    // Expose lastCommand to the cloud
    Spark.variable("lastcommand", &lastCommand, STRING);
}


// This routine loops forever 
void loop() 
{ 
// nothing to do
}

int8_t fadin(uint8_t line, uint8_t startval, uint8_t stopval, uint8_t atimebtwfade)
{
    int8_t fadeValue;
    for(fadeValue = startval ; fadeValue <= stopval ; fadeValue++) {
        // sets the value (range from 0 to 255):
        analogWrite(line, fadeValue);  
        // wait for 3 seconds to see the dimming effect    
        delay(atimebtwfade);                            
    }
    return fadeValue;
}

int8_t fadout(uint8_t line, uint8_t startval, uint8_t stopval, uint8_t atimebtwfade)
{
    int8_t fadeValue;
    for(fadeValue = startval ; fadeValue >= stopval ; fadeValue--) {
        // sets the value (range from 255 to 0):
        analogWrite(line, fadeValue);        
        // wait for 3 seconds to see the dimming effect    
        delay(atimebtwfade); 
    }
    return fadeValue;
}

void sunrise()
{
        //sunrise
    for (int i = 0; i <= ( nbblue - 1 ); i++){
        blue[i][1]=fadin(blue[i][0], blue[i][1], maxint, btwfadeval);
    }
    for (int i = 0; i <= ( nbwhite - 1 ); i++){
        white[i][1]=fadin(white[i][0], white[i][1], maxint, btwfadeval);
    }
}

void sunset()
{
        // sunset
    for (int i = ( nbwhite - 1 ); i >= 0; i--){
        white[i][1]=fadout(white[i][0], white[i][1], 0, btwfadeval);
    }
    for (int i = ( nbblue - 1 ); i >= 0; i--){
        blue[i][1]=fadout(blue[i][0], blue[i][1], 0, btwfadeval);
    }
}

void moon()
{
        // sunset
    for (int i = ( nbwhite - 1 ); i >= 0; i--){
        white[i][1]=fadout(white[i][0], white[i][1], 0, btwfadeval);
    }
    for (int i = ( nbblue - 1 ); i >= 0; i--){
        blue[i][1]=fadout(blue[i][0], blue[i][1], 0, btwfadeval);
    }
    // moon
    blue[moonline][1]=fadin(blue[moonline][0], blue[moonline][1], moonintensity, 2);
}

void cloud()
{
    while (lastCommand == "CLOUD") {
        //do the cloud
    }
}

void storm()
{
        while (lastCommand == "STORM") {
        //do the storm
    }
}

int SunLight(String command) {
    // Trim extra spaces
    command.trim();

    // Convert it to upper-case for easier matching
    command.toUpperCase();

    // Copy command argument to lastCommand
    command.toCharArray(lastCommand, 64);

    // "Route" the commands to the corresponding function
    if(command.equals("SUNRISE")) {
        sunrise();
        return 1;
    }
    else if(command.equals("SUNSET")) {
        sunset();
        return 1;
    }
    else if(command.equals("MOON")) {
        moon();
        return 1;
    }
    else if(command.equals("CLOUD")) {
        //cloud();
        return 2;
    }
    else if(command.equals("STORM")) {
        //storm();
        return 2;
    }
    else {
        return -1;
    }
}

If you see errors or the possibility to optimize the code, please telle me .

My fish and corals will thank you ! :wink:

Maybe you are thinking i’m not English, and you are right ! (sorry for mistakes) :blush:

1 Like

Wow. Have to spend some time to look at it before commenting :slight_smile:

Try not to call any functions in the Spark.function() and do a check in the loop()

Use a variable to store what was called.

Eg:

int which_command = 0;

void loop(){
    switch (which_command) {
        case 1:
            sunrise();
            which_command = 0;
            break;
            
        case 2:
            sunset();
            which_command = 0;
            break;
    }
}

int SunLight(String command) {
 
  if(command.equals("SUNRISE")) {
        which_command = 1;
        return 1;
    }
    else if(command.equals("SUNSET")) {
        which_command = 2;
        return 2;
    }'
   //rest of the code
}

As for the sunrise function, the flash might be due to the 1st cycle of the fadein loop. Try playing with the startval.

A problem i thought would be the PWM is already working and the startval is lesser than the current PWM value which caused the flash.

Also, your delay for fadein and fadeout seems to be too long:

// time for sunrise/sunset in minutes
unsigned int fadetime = 1;

unsigned int btwfadeval = ( fadetime * 60 * 1000 ) / ( ( nbwhite + nbblue ) * maxint );

delay(btwfadeval) is delay (60000)!!! That’s like 60 seonds

Also, since you use delay() mostly greater than 3 seconds (accordingly to your comments) add in SPARK_WLAN_Loop(); before the delay() to stay connected to the cloud.

Gosh. What a messy post but hope it helps!

2 Likes

kennethlimcp, the Spark delay() function now calls SPARK_WLAN_Loop() by itself! :smile:

3 Likes

Good to knowwwwww :D!

@kennethlimcp @peekay123 thank you guys !

good idea to use the loop :slight_smile:

about the delay, btwfadeval is not 60 seconds, it is 60 seconds divided by (( 3 +3) * 255 ), so it’s 39,2 milliseconds . Each step from 0 to 255 of each line (3 white and 3 blue) take 39,2 milliseconds, so all step of all lines will do 60 seconds.

i will make your changes ASAP ! :smile:

if you have other good idea, please share it ! :wink:

i will keep you in touch

@kennethlimcp i have updated my code with wich_command and the switch case in the loop, and i work very well ! :smiley:

this is the new code ( i added new function like random, cloud and storm, but we don’t care about it for now) :

// Light Reef Tank project

/* remote commands

// make sunrise
curl -k https://api.spark.io/v1/devices/50ff6xxxxxx/sunlight \
     -d access_token=xxx \
     -d "args=sunrise"
     
// make sunset
curl -k https://api.spark.io/v1/devices/50ff6xxxxxx/sunlight \
     -d access_token=xxx \
     -d "args=sunset"
     
// get last command
curl -k "https://api.spark.io/v1/devices/50ff6xxxxxx/lastcommand?access_token=xxx"

*/


// pin of white lines and pwm value
int8_t white[3][2]={{A5, 0}, {A6, 0}, {A7, 0}};
// pin of blue lines and pwm value
int8_t blue[3][2]={{A0, 0}, {A1, 0}, {A4, 0}};


// time for sunrise/sunset in minutes
unsigned int fadetime = 1;

// maximum Sun intensity in percent
unsigned int sunmaxintensity = 100;

// maximum Moon intensity in percent
unsigned int moonintensity = 1;

// moon line (blue[1])
unsigned int moonline = 1;

// minimum cloud intensity from 0 to 255
unsigned int mincldintensity = 90;

// maximum cloud intensity from 0 to 255
unsigned int maxcldintensity = 255;

// minimum cloud intensity during storm from 0 to 255
unsigned int minstrmcldintensity = 5;

// maximum cloud intensity during storm from 0 to 255
unsigned int maxstrmcldintensity = 40;

// String to hold last command issued
char lastCommand[64] = "none";

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// nb white lines
unsigned int nbwhite = 3; //sizeof(white);
// nb blue lines
unsigned int nbblue = 3; //sizeof(blue);


// maximum intensity from 0 to 255
unsigned int maxint = ( sunmaxintensity * 255 / 100 );

// delay in milliseconds between two fade value
unsigned int btwfadeval = ( fadetime * 60 * 1000 ) / ( ( nbwhite + nbblue ) * maxint );

// command for the loop
int which_command = 0;


// This routine runs only once upon reset
void setup() 
{
  // Initialize white pins as an output
    for (int i = 0; i <= ( nbwhite - 1 ); i++){
        pinMode(white[i][0], OUTPUT);
    }
  // Initialize blue pins as an output
    for (int i = 0; i <= ( nbblue - 1 ); i++){
        pinMode(blue[i][0], OUTPUT);
    }
    
    // Expose Sun() to the cloud
    Spark.function("sunlight", SunLight);
    
    // Expose lastCommand to the cloud
    Spark.variable("lastcommand", &lastCommand, STRING);
}


// This routine loops forever 
void loop() 
{ 
    switch (which_command) {
        case 1:
            sunrise();
            which_command = 0;
            break;
        case 2:
            sunset();
            which_command = 0;
            break;
        case 3:
            moon();
            which_command = 0;
            break;
        case 4:
            cloud();
            break;
        case 5:
            storm();
            break;

    }
}

// random function
int random(int minRand, int maxRand)
{
return rand() % (maxRand-minRand+1) + minRand;
}

//fade in function
int8_t fadin(uint8_t line, uint8_t startval, uint8_t stopval, uint8_t atimebtwfade)
{
    int8_t fadeValue;
    for(fadeValue = startval ; fadeValue <= stopval ; fadeValue++) {
        // sets the value (range from 0 to 255):
        analogWrite(line, fadeValue);  
        // wait for x milliseconds to see the dimming effect    
        delay(atimebtwfade);                            
    }
    return fadeValue;
}

//fade out function
int8_t fadout(uint8_t line, uint8_t startval, uint8_t stopval, uint8_t atimebtwfade)
{
    int8_t fadeValue;
    for(fadeValue = startval ; fadeValue >= stopval ; fadeValue--) {
        // sets the value (range from 255 to 0):
        analogWrite(line, fadeValue);        
        // wait for x milliseconds to see the dimming effect    
        delay(atimebtwfade); 
    }
    return fadeValue;
}

void sunrise()
{
        //sunrise
    for (int i = 0; i <= ( nbblue - 1 ); i++){
        blue[i][1]=fadin(blue[i][0], blue[i][1], maxint, btwfadeval);
    }
    for (int i = 0; i <= ( nbwhite - 1 ); i++){
        white[i][1]=fadin(white[i][0], white[i][1], maxint, btwfadeval);
    }
}

void sunset()
{
        // sunset
    for (int i = ( nbwhite - 1 ); i >= 0; i--){
        white[i][1]=fadout(white[i][0], white[i][1], 0, btwfadeval);
    }
    for (int i = ( nbblue - 1 ); i >= 0; i--){
        blue[i][1]=fadout(blue[i][0], blue[i][1], 0, btwfadeval);
    }
}

void moon()
{
    // sunset
    for (int i = ( nbwhite - 1 ); i >= 0; i--){
        white[i][1]=fadout(white[i][0], white[i][1], 0, btwfadeval);
    }
    for (int i = ( nbblue - 1 ); i >= 0; i--){
        blue[i][1]=fadout(blue[i][0], blue[i][1], 0, btwfadeval);
    }
    // moon
    blue[moonline][1]=fadin(blue[moonline][0], blue[moonline][1], moonintensity, 30);
}

void cloud()
{
    //do the cloud
    // choose white line
    int wcldline=random(0, (nbwhite - 1));
    // choose white intensity
    int wcldintensity=random(mincldintensity, maxcldintensity);
    // do the cloud for this line this value
    if(white[wcldline][1] < wcldintensity) {
        white[wcldline][1]=fadin(white[wcldline][0], white[wcldline][1], wcldintensity, 30);
    } else {
        white[wcldline][1]=fadout(white[wcldline][0], white[wcldline][1], wcldintensity, 30);
    }
    // choose blue line
    int bcldline=random(0, (nbblue - 1));
    // choose blue intensity
    int bcldintensity=random(mincldintensity, maxcldintensity);
    // do the cloud for this line this value
    if(blue[bcldline][1] < bcldintensity) {
        blue[bcldline][1]=fadin(blue[bcldline][0], blue[bcldline][1], bcldintensity, 30);
    } else {
        blue[bcldline][1]=fadout(blue[bcldline][0], blue[bcldline][1], bcldintensity, 30);
    }    
}

void storm()
{
    //do the storm
    // choose white line
    int wcldline=random(0, (nbwhite - 1));
    // choose white intensity
    int wcldintensity=random(minstrmcldintensity, maxstrmcldintensity);
    // do the cloud for this line this value
    if(white[wcldline][1] < wcldintensity) {
        white[wcldline][1]=fadin(white[wcldline][0], white[wcldline][1], wcldintensity, 30);
    } else {
        white[wcldline][1]=fadout(white[wcldline][0], white[wcldline][1], wcldintensity, 30);
    }
    // choose blue line
    int bcldline=random(0, (nbblue - 1));
    // choose blue intensity
    int bcldintensity=random((minstrmcldintensity + 50), (maxstrmcldintensity + 50));
    // do the cloud for this line this value
    if(blue[bcldline][1] < bcldintensity) {
        blue[bcldline][1]=fadin(blue[bcldline][0], blue[bcldline][1], bcldintensity, 30);
    } else {
        blue[bcldline][1]=fadout(blue[bcldline][0], blue[bcldline][1], bcldintensity, 30);
    }
    // Flash a white line
    int rdmflash=random(0,5);  // to not flash evry time
    if (rdmflash = 1){
        int prevval = white[wcldline][1];
        analogWrite(white[wcldline][0], 255);
        delay(2);
        analogWrite(white[wcldline][0], prevval);
    }
}

int SunLight(String command) {
    // Trim extra spaces
    command.trim();

    // Convert it to upper-case for easier matching
    command.toLowerCase();

    // Copy command argument to lastCommand
    command.toCharArray(lastCommand, 64);

    // "Route" the commands to the corresponding function
    if(command.equals("sunrise")) {
        which_command = 1;
        return 1;
    }
    else if(command.equals("sunset")) {
        which_command = 2;
        return 2;
    }
    else if(command.equals("moon")) {
        which_command = 3;
        return 3;
    }
    else if(command.equals("cloud")) {
        which_command = 4;
        return 4;
    }
    else if(command.equals("storm")) {
        which_command = 5;
        return 5;
    }
    else {
        return -1;
    }
}

Now i try to track the bug for the second issue but it’s not easy.

I’m not sur that i record the value of the pwm in the 2 dimensional array (maybe i record only the address of the value ? i’m not confortable with array, value, address of the value, …) when i write :

blue[i][1]=fadin(blue[i][0], blue[i][1], maxint, btwfadeval);

int8_t fadin(uint8_t line, uint8_t startval, uint8_t stopval, uint8_t atimebtwfade)
{
    int8_t fadeValue;
for(fadeValue = startval ; fadeValue <= stopval ; fadeValue++) {
    // sets the value (range from 0 to 255):
    analogWrite(line, fadeValue);  
    // wait for x milliseconds to see the dimming effect    
    delay(atimebtwfade);                            
}
    return fadeValue;
}

What do you think about that ?

That’s a tough question when i have to read and fully understand code written by others. :smiley: :smiley:

What you can do is to do a Serial.print() of the values and see on a Terminal.

You might see something like this: (this is purely imaginary)

1
2
3
5
6
......
200
End of fade function
10
11
12

So you see from 200, it jumped to 10 and increased again. I’m thinking that’s causing the sudden flash but i have to sit down and read your code before i can nail down the real problem!

Hi @kriss

Is there a good reason you are using uint8_t for everything except fadeValue which in int8_t?

The type int8_t can only represent numbers from -128 to +127 and cannot go to 255 for instance.

I would think that both fadeValue and the return type of the fadin function would be uint8_t. Similarly, you white, blue, etc. arrays seem to be in the wrong type to me.

Hi @bko your right !

the type of variables was not good. i replace all type with “int” and all my problems are solved ! :smiley:

i adjusted cloud and storm function to be more real.

Thanks to @kennethlimcp , @peekay123 and @bko you are fantastic !

And thanks to all the spark team to make an awsome product !

The Final code, it can be usefull for people with a reef tank :wink:

// Light Reef Tank project

/* remote commands

// make sunrise
curl -k https://api.spark.io/v1/devices/50ff6xxxxxx/sunlight \
     -d access_token=xxx \
     -d "args=sunrise"
     
// make sunset
curl -k https://api.spark.io/v1/devices/50ff6xxxxxx/sunlight \
     -d access_token=xxx \
     -d "args=sunset"
     
// get last command
curl -k "https://api.spark.io/v1/devices/50ff6xxxxxx/lastcommand?access_token=xxx"

*/


// pin of white lines and pwm value
int white[3][2]={{A5, 0}, {A6, 0}, {A7, 0}};
// pin of blue lines and pwm value
int blue[3][2]={{A0, 0}, {A1, 0}, {A4, 0}};


// time for sunrise/sunset in minutes
int fadetime = 15;

// maximum Sun intensity in percent
int sunmaxintensity = 100;

// maximum Moon intensity in percent
int moonintensity = 1;

// moon line (blue[1])
int moonline = 1;

// minimum cloud intensity from 0 to 255
int mincldintensity = 100;

// maximum cloud intensity from 0 to 255
int maxcldintensity = 255;

// minimum cloud intensity during storm from 0 to 255
int minstrmcldintensity = 5;

// maximum cloud intensity during storm from 0 to 255
int maxstrmcldintensity = 40;

// String to hold last command issued
char lastCommand[64] = "none";

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


// nb white lines
int nbwhite = 3; //sizeof(white);
// nb blue lines
int nbblue = 3; //sizeof(blue);


// maximum intensity from 0 to 255
int maxint = ( sunmaxintensity * 255 / 100 );

// delay in milliseconds between two fade value
int btwfadeval = ( fadetime * 60 * 1000 ) / ( ( nbwhite + nbblue ) * maxint );

// command for the loop
int which_command = 0;


// This routine runs only once upon reset
void setup() 
{
  // Initialize white pins as an output
    for (int i = 0; i <= ( nbwhite - 1 ); i++){
        pinMode(white[i][0], OUTPUT);
    }
  // Initialize blue pins as an output
    for (int i = 0; i <= ( nbblue - 1 ); i++){
        pinMode(blue[i][0], OUTPUT);
    }
    
    // Expose Sun() to the cloud
    Spark.function("sunlight", SunLight);
    
    // Expose lastCommand to the cloud
    Spark.variable("lastcommand", &lastCommand, STRING);
}


// This routine loops forever 
void loop() 
{ 
    switch (which_command) {
        case 1:
            sunrise();
            which_command = 0;
            break;
        case 2:
            sunset();
            which_command = 0;
            break;
        case 3:
            moon();
            which_command = 0;
            break;
        case 4:
            cloud();
            break;
        case 5:
            storm();
            break;

    }
}

// random function
int random(int minRand, int maxRand)
{
return rand() % (maxRand-minRand+1) + minRand;
}

//fade in function
int fadin(int line, int startval, int stopval, int atimebtwfade)
{
    int fadeValue;
    for(fadeValue = startval ; fadeValue <= stopval ; fadeValue++) {
        // sets the value (range from 0 to 255):
        analogWrite(line, fadeValue);  
        // wait for x milliseconds to see the dimming effect    
        delay(atimebtwfade);                            
    }
    return (fadeValue - 1);
}

//fade out function
int fadout(int line, int startval, int stopval, int atimebtwfade)
{
    int fadeValue;
    for(fadeValue = startval ; fadeValue >= stopval ; fadeValue--) {
        // sets the value (range from 255 to 0):
        analogWrite(line, fadeValue);        
        // wait for x milliseconds to see the dimming effect    
        delay(atimebtwfade); 
    }
    return (fadeValue + 1);
}

void sunrise()
{
        //sunrise
    for (int i = 0; i <= ( nbblue - 1 ); i++){
        blue[i][1]=fadin(blue[i][0], blue[i][1], maxint, btwfadeval);
    }
    for (int i = 0; i <= ( nbwhite - 1 ); i++){
        white[i][1]=fadin(white[i][0], white[i][1], maxint, btwfadeval);
    }
}

void sunset()
{
        // sunset
    for (int i = ( nbwhite - 1 ); i >= 0; i--){
        white[i][1]=fadout(white[i][0], white[i][1], 0, btwfadeval);
    }
    for (int i = ( nbblue - 1 ); i >= 0; i--){
        blue[i][1]=fadout(blue[i][0], blue[i][1], 0, btwfadeval);
    }
}

void moon()
{
    // sunset
    for (int i = ( nbwhite - 1 ); i >= 0; i--){
        white[i][1]=fadout(white[i][0], white[i][1], 0, btwfadeval);
    }
    for (int i = ( nbblue - 1 ); i >= 0; i--){
        blue[i][1]=fadout(blue[i][0], blue[i][1], 0, btwfadeval);
    }
    // moon
    blue[moonline][1]=fadin(blue[moonline][0], blue[moonline][1], moonintensity, 30);
}

void cloud()
{
    //do the cloud
    // choose white line
    int wcldline=random(0, (nbwhite - 1));
    // choose white intensity
    int wcldintensity=random((mincldintensity - 50), maxcldintensity);
    // do the cloud for this line this value
    if(white[wcldline][1] < wcldintensity) {
        white[wcldline][1]=fadin(white[wcldline][0], white[wcldline][1], wcldintensity, 100);
    } else {
        white[wcldline][1]=fadout(white[wcldline][0], white[wcldline][1], wcldintensity, 100);
    }
    delay(500);
    // choose blue line
    int bcldline=random(0, (nbblue - 1));
    // choose blue intensity
    int bcldintensity=random(mincldintensity, maxcldintensity);
    // do the cloud for this line this value
    if(blue[bcldline][1] < bcldintensity) {
        blue[bcldline][1]=fadin(blue[bcldline][0], blue[bcldline][1], bcldintensity, 100);
    } else {
        blue[bcldline][1]=fadout(blue[bcldline][0], blue[bcldline][1], bcldintensity, 100);
    }    
    delay(500);
}

void storm()
{
    //do the storm
    // choose white line
    int wcldline=random(0, (nbwhite - 1));
    // choose white intensity
    int wcldintensity=random(minstrmcldintensity, maxstrmcldintensity);
    // do the cloud for this line this value
    if(white[wcldline][1] < wcldintensity) {
        white[wcldline][1]=fadin(white[wcldline][0], white[wcldline][1], wcldintensity, 100);
    } else {
        white[wcldline][1]=fadout(white[wcldline][0], white[wcldline][1], wcldintensity, 100);
    }
    delay(500);
    // choose blue line
    int bcldline=random(0, (nbblue - 1));
    // choose blue intensity
    int bcldintensity=random((minstrmcldintensity + 50), (maxstrmcldintensity + 50));
    // do the cloud for this line this value
    if(blue[bcldline][1] < bcldintensity) {
        blue[bcldline][1]=fadin(blue[bcldline][0], blue[bcldline][1], bcldintensity, 100);
    } else {
        blue[bcldline][1]=fadout(blue[bcldline][0], blue[bcldline][1], bcldintensity, 100);
    }
    // Flash a white line
    int rdmflash=random(0,25);  // to not flash evry time
    if (rdmflash = 1){
        int prevval = white[wcldline][1];
        analogWrite(white[wcldline][0], 255);
        delay(5);
        analogWrite(white[wcldline][0], prevval);
    }
    delay(500);
}

int SunLight(String command) {
    // Trim extra spaces
    command.trim();

    // Convert it to upper-case for easier matching
    command.toLowerCase();

    // Copy command argument to lastCommand
    command.toCharArray(lastCommand, 64);

    // "Route" the commands to the corresponding function
    if(command.equals("sunrise")) {
        which_command = 1;
        return 1;
    }
    else if(command.equals("sunset")) {
        which_command = 2;
        return 2;
    }
    else if(command.equals("moon")) {
        which_command = 3;
        return 3;
    }
    else if(command.equals("cloud")) {
        which_command = 4;
        return 4;
    }
    else if(command.equals("storm")) {
        which_command = 5;
        return 5;
    }
    else {
        return -1;
    }
}
2 Likes