Arduino Program Freez

Hi, I am working on a project to dim/brighten the LED lights between each hour. To make the program readable i have only kept the code for few hours.

For example if T1 is at 255 PWM and T2 is 20 PWM, the code calculates the moves required to dim the LED by One PWM every few second such that by T2 the PWM is at 20.

I have Included DS1307 RTC for keeping track of time, further i have used a HC-05 Bluetooth module to communicate with the Arduino, using Android phone, so that i can change the PWM values between T1 and T2.

When i run the code it works perfectly for some time post which it freezes, and the LED starts flickering.

#include <Wire.h>
#include "RTClib.h"
String fromAndroid;
////////RTC
int h = 0;
int m = 0;
String s;
int hours;
unsigned long interval = 1000;
////////////////

String Status = "Active";

////////////Millis
unsigned long currentMillis;
unsigned long White_previousMillis;
unsigned long previousMillis;//for clock
unsigned long Hr_interval = 3600000;
unsigned long white_each_interval18;
unsigned long white_each_interval19;
unsigned long white_each_interval20;

////////////////////
int pwm02 = 2;


/////////////////Peak Keeper
int white_t17 = 255;
int white_t18 = 20;
int white_t19 = 253;
int white_t20 = 1;

/////Secondary time

int white_Secondary_t17;
int white_Secondary_t18;
int white_Secondary_t19;
int white_Secondary_t20;



int white_steps18;
int white_steps19;
int white_steps20;
int white_steps21;


int white_move17;
int white_move18;
int white_move19;
int white_move20;
int white_move21;



RTC_DS1307 rtc;
void setup() {
  Serial.begin(9600);
  rtc.begin();
  if (! rtc.begin())
  {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning())
  {
    Serial.println("RTC is NOT running!");
  }
  Serial.println("Start");
  //  rtc.adjust(DateTime(2017,12,17,11,52,00));
  pinMode(pwm02, OUTPUT);

}

void loop() {

  while (Serial.available() == 0 && Status == "Active")
  {
   
    /////////WhiteT18
    white_steps18 = white_t18 - white_t17;
    white_Secondary_t18 = white_t17;
    if (white_steps18 == 0)
    {
      white_move18 = 0;
      white_each_interval18 = 1800000;
    }
    if (white_steps18 < 0)
    {
      white_move18 = -1;
    }
    else
    {
      white_move18 = 1;
    }
    if (white_steps18 != 0)
    {
      white_each_interval18 = Hr_interval / abs(white_steps18);
    }
    Serial.print("white_steps18: "); Serial.println(white_steps18);
    Serial.print("white_each_interval18: "); Serial.println(white_each_interval18);
    /////////WhiteT19
    white_steps19 = white_t19 - white_t18;
    white_Secondary_t19 = white_t18;
    if (white_steps19 == 0)
    {
      white_move19 = 0;
      white_each_interval19 = 1800000;
    }
    if (white_steps19 < 0)
    {
      white_move19 = -1;
    }
    else
    {
      white_move19 = 1;
    }
    if (white_steps19 != 0)
    {
      white_each_interval19 = Hr_interval / abs(white_steps19);
    }
    Serial.print("white_steps19: "); Serial.println(white_steps19);
    Serial.print("white_each_interval19: "); Serial.println(white_each_interval19);
    /////////WhiteT20
    white_steps20 = white_t20 - white_t19;
    white_Secondary_t20 = white_t19;
    if (white_steps20 == 0)
    {
      white_move20 = 0;
      white_each_interval20 = 1800000;
    }
    if (white_steps20 < 0)
    {
      white_move20 = -1;
    }
    else
    {
      white_move20 = 1;
    }
    if (white_steps20 != 0)
    {
      white_each_interval20 = Hr_interval / abs(white_steps20);
    }
    Serial.print("white_steps20: "); Serial.println(white_steps20);
    Serial.print("white_each_interval20: "); Serial.println(white_each_interval20);
   
    Status = "NoStatus";
  }

  while (Serial.available() == 0 && Status == "NoStatus")
  {
    DateTime now = rtc.now();
    currentMillis = millis();
    hours = now.hour();
    if (currentMillis - previousMillis >= interval)
    {
      Serial.print(now.year(), DEC);
      Serial.print('/');
      Serial.print(now.month(), DEC);
      Serial.print('/');
      Serial.print(now.day(), DEC);
      Serial.print(" (");
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      Serial.print(now.second(), DEC);
      Serial.println(')');
      previousMillis = currentMillis;
    }
    

    //////T2
   
    if (hours == 18)
    { //Serial.println("In 2");
      if (currentMillis - White_previousMillis >= white_each_interval19)
      {
        white_Secondary_t19 = white_Secondary_t19 + white_move19;
        analogWrite(pwm02, white_Secondary_t19);
        Serial.print("PWM02: "); Serial.println(white_Secondary_t19);

        White_previousMillis = currentMillis;
      }



    }
    //////T3
   
    if (hours == 19)
    { //Serial.println("In 3");
      if (currentMillis - White_previousMillis >= white_each_interval20)
      {
        white_Secondary_t20 = white_Secondary_t20 + white_move20;
        analogWrite(pwm02, white_Secondary_t20);
        Serial.print("PWM02: "); Serial.println(white_Secondary_t20);

        White_previousMillis = currentMillis;
      }



    }
   
    //last
  }

  while (Serial.available() > 0)
  {
    Serial.println("In Input Loop");
    fromAndroid = Serial.readString();
    if (fromAndroid.startsWith("White"))
    {
      fromAndroid.replace("White", "");
      
      if (fromAndroid.startsWith("time18"))
      {
        fromAndroid.replace("time18", "");
        white_t18 = fromAndroid.toInt();
        Serial.print("white_t18 VALUE: ");
        Serial.println(fromAndroid);

      }
      if (fromAndroid.startsWith("time19"))
      {
        fromAndroid.replace("time19", "");
        white_t19 = fromAndroid.toInt();
        Serial.print("white_t19 VALUE: ");
        Serial.println(fromAndroid);

      }
      if (fromAndroid.startsWith("time20"))
      {
        fromAndroid.replace("time20", "");
        white_t20 = fromAndroid.toInt();
        Serial.print("white_t20 VALUE: ");
        Serial.println(fromAndroid);

      }
      

    }


    Status = "Active";
  }

}

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

Your symptoms are consistent with a memory problem caused by the String class.

...R

Robin2 is 100% correct, the way you’ve using the String class for the variable “Status” will eventually lead to a program crash. You also have an unused declaration of another String named “s” which was unused. Here is your program with the String class removed. It compiles but I cannot fully test for obvious reasons.

There was no reason whatsoever to use strings for what you were doing with the variable “Status”. It is a 0/1 or true/false condition.

#include <Wire.h>
#include "RTClib.h"
String fromAndroid;
////////RTC
int h = 0;
int m = 0;
//String s;
int hours;
unsigned long interval = 1000;
////////////////

boolean Status = true;

////////////Millis
unsigned long currentMillis;
unsigned long White_previousMillis;
unsigned long previousMillis;//for clock
unsigned long Hr_interval = 3600000;
unsigned long white_each_interval18;
unsigned long white_each_interval19;
unsigned long white_each_interval20;

////////////////////
int pwm02 = 2;

/////////////////Peak Keeper
int white_t17 = 255;
int white_t18 = 20;
int white_t19 = 253;
int white_t20 = 1;

/////Secondary time

int white_Secondary_t17;
int white_Secondary_t18;
int white_Secondary_t19;
int white_Secondary_t20;

int white_steps18;
int white_steps19;
int white_steps20;
int white_steps21;

int white_move17;
int white_move18;
int white_move19;
int white_move20;
int white_move21;

RTC_DS1307 rtc;
void setup() {
  Serial.begin(9600);
  rtc.begin();
  if (! rtc.begin())
  {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning())
  {
    Serial.println("RTC is NOT running!");
  }
  Serial.println("Start");
  //  rtc.adjust(DateTime(2017,12,17,11,52,00));
  pinMode(pwm02, OUTPUT);

}

void loop() {

  while (Serial.available() == 0 && Status)
  {
   
    /////////WhiteT18
    white_steps18 = white_t18 - white_t17;
    white_Secondary_t18 = white_t17;
    if (white_steps18 == 0)
    {
      white_move18 = 0;
      white_each_interval18 = 1800000;
    }
    if (white_steps18 < 0)
    {
      white_move18 = -1;
    }
    else
    {
      white_move18 = 1;
    }
    if (white_steps18 != 0)
    {
      white_each_interval18 = Hr_interval / abs(white_steps18);
    }
    Serial.print("white_steps18: "); Serial.println(white_steps18);
    Serial.print("white_each_interval18: "); Serial.println(white_each_interval18);
    /////////WhiteT19
    white_steps19 = white_t19 - white_t18;
    white_Secondary_t19 = white_t18;
    if (white_steps19 == 0)
    {
      white_move19 = 0;
      white_each_interval19 = 1800000;
    }
    if (white_steps19 < 0)
    {
      white_move19 = -1;
    }
    else
    {
      white_move19 = 1;
    }
    if (white_steps19 != 0)
    {
      white_each_interval19 = Hr_interval / abs(white_steps19);
    }
    Serial.print("white_steps19: "); Serial.println(white_steps19);
    Serial.print("white_each_interval19: "); Serial.println(white_each_interval19);
    /////////WhiteT20
    white_steps20 = white_t20 - white_t19;
    white_Secondary_t20 = white_t19;
    if (white_steps20 == 0)
    {
      white_move20 = 0;
      white_each_interval20 = 1800000;
    }
    if (white_steps20 < 0)
    {
      white_move20 = -1;
    }
    else
    {
      white_move20 = 1;
    }
    if (white_steps20 != 0)
    {
      white_each_interval20 = Hr_interval / abs(white_steps20);
    }
    Serial.print("white_steps20: "); Serial.println(white_steps20);
    Serial.print("white_each_interval20: "); Serial.println(white_each_interval20);
   
    Status = false;
  }

  while (Serial.available() == 0 && !Status)
  {
    DateTime now = rtc.now();
    currentMillis = millis();
    hours = now.hour();
    if (currentMillis - previousMillis >= interval)
    {
      Serial.print(now.year(), DEC);
      Serial.print('/');
      Serial.print(now.month(), DEC);
      Serial.print('/');
      Serial.print(now.day(), DEC);
      Serial.print(" (");
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      Serial.print(now.second(), DEC);
      Serial.println(')');
      previousMillis = currentMillis;
    }
    //////T2
    if (hours == 18)
    { //Serial.println("In 2");
      if (currentMillis - White_previousMillis >= white_each_interval19)
      {
        white_Secondary_t19 = white_Secondary_t19 + white_move19;
        analogWrite(pwm02, white_Secondary_t19);
        Serial.print("PWM02: "); Serial.println(white_Secondary_t19);
        White_previousMillis = currentMillis;
      }
    }
    //////T3
    if (hours == 19)
    { //Serial.println("In 3");
      if (currentMillis - White_previousMillis >= white_each_interval20)
      {
        white_Secondary_t20 = white_Secondary_t20 + white_move20;
        analogWrite(pwm02, white_Secondary_t20);
        Serial.print("PWM02: "); Serial.println(white_Secondary_t20);
        White_previousMillis = currentMillis;
      }
    }
    //last
  }
  while (Serial.available() > 0)
  {
    Serial.println("In Input Loop");
    fromAndroid = Serial.readString();
    if (fromAndroid.startsWith("White"))
    {
      fromAndroid.replace("White", "");
      if (fromAndroid.startsWith("time18"))
      {
        fromAndroid.replace("time18", "");
        white_t18 = fromAndroid.toInt();
        Serial.print("white_t18 VALUE: ");
        Serial.println(fromAndroid);
      }
      if (fromAndroid.startsWith("time19"))
      {
        fromAndroid.replace("time19", "");
        white_t19 = fromAndroid.toInt();
        Serial.print("white_t19 VALUE: ");
        Serial.println(fromAndroid);
      }
      if (fromAndroid.startsWith("time20"))
      {
        fromAndroid.replace("time20", "");
        white_t20 = fromAndroid.toInt();
        Serial.print("white_t20 VALUE: ");
        Serial.println(fromAndroid);
      }
    }
    Status = true;
  }
}

avr_fred:
Robin2 is 100% correct, the way you've using the String class for the variable "Status" will eventually lead to a program crash.

How so? I see it declared as global variable, later gets assigned the values "active" and "noStatus" depending on the program state.

I have most of the String except the “fromAndroid” which converts the String to Integer immediately. Still I am facing the same issue. my ne code is.

#include <Wire.h>
#include "RTClib.h"
String fromAndroid;
////////RTC
int h = 0;
int m = 0;

int hours;
unsigned long interval = 1000;
////////////////


////////////Millis
unsigned long currentMillis;
unsigned long White_previousMillis;
unsigned long previousMillis;//for clock
unsigned long Hr_interval = 3600000;
unsigned long white_each_interval;


////////////////////
int pwm02 = 2;
int pwm03 = 3;
int pwm04 = 4;
int pwm05 = 5;
int pwm06 = 6;
int pwm07 = 7;


/////////////////Peak Keeper
int white_t24 = 10;
int white_t1 = 233;
int white_t2 = 18;
int white_t3 = 189;
int white_t4 = 12;
int white_t5 = 255;
int white_t6 = 19;
int white_t7 = 199;
int white_t8 = 2;
int white_t9 = 177;
int white_t10 = 2;
int white_t11 = 245;
int white_t12 = 12;
int white_t13 = 252;
int white_t14 = 0;
int white_t15 = 255;
int white_t16 = 50;
int white_t17 = 239;
int white_t18 = 20;
int white_t19 = 253;
int white_t20 = 1;
int white_t21 = 252;
int white_t22 = 2;
int white_t23 = 255;


/////Secondary time

int white_Secondary;


int white_steps; //new

int white_move; //New




RTC_DS1307 rtc;
void setup() {
  Serial.begin(9600);
  rtc.begin();
  if (! rtc.begin())
  {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning())
  {
    Serial.println("RTC is NOT running!");
  }
  Serial.println("Start");
  //  rtc.adjust(DateTime(2017,12,17,11,52,00));
  pinMode(pwm02, OUTPUT);

}

void loop() {

  while (Serial.available() == 0)
  {
   DateTime now = rtc.now();
    currentMillis = millis();
    hours = now.hour();
    if (currentMillis - previousMillis >= interval)
    {
      Serial.print(now.year(), DEC);
      Serial.print('/');
      Serial.print(now.month(), DEC);
      Serial.print('/');
      Serial.print(now.day(), DEC);
      Serial.print(" (");
      Serial.print(now.hour(), DEC);
      Serial.print(':');
      Serial.print(now.minute(), DEC);
      Serial.print(':');
      Serial.print(now.second(), DEC);
      Serial.println(')');
      previousMillis = currentMillis;
    }

    if (hours == 12)
    {
      if(now.minute()==00 && now.second()<=1)
      {
        white_Secondary = white_t24;
        analogWrite(pwm02, white_Secondary);
      }
      white_steps = white_t1 - white_t24;
      if (white_steps == 0)
    {
      white_move = 0;
      white_each_interval = 1800000;
    }
    if (white_steps < 0)
    {
      white_move = -1;
    }
    else
    {
      white_move = 1;
    }
    if (white_steps != 0)
    {
      white_each_interval = Hr_interval / abs(white_steps);
    }
    }


///////////
if (currentMillis - White_previousMillis >= white_each_interval)
      {
        white_Secondary = white_Secondary + white_move;
        analogWrite(pwm02, white_Secondary);
        Serial.print("PWM02: "); Serial.println(white_Secondary);

        White_previousMillis = currentMillis;
      }




   
  }

  

  while (Serial.available() > 0)
  {
    Serial.println("In Input Loop");
    fromAndroid = Serial.readString();
    if (fromAndroid.startsWith("White"))
    {
      fromAndroid.replace("White", "");
      
      if (fromAndroid.startsWith("time18"))
      {
        fromAndroid.replace("time18", "");
        white_t18 = fromAndroid.toInt();
        Serial.print("white_t18 VALUE: ");
        Serial.println(fromAndroid);

      }
      if (fromAndroid.startsWith("time19"))
      {
        fromAndroid.replace("time19", "");
        white_t19 = fromAndroid.toInt();
        Serial.print("white_t19 VALUE: ");
        Serial.println(fromAndroid);

      }
      if (fromAndroid.startsWith("time20"))
      {
        fromAndroid.replace("time20", "");
        white_t20 = fromAndroid.toInt();
        Serial.print("white_t20 VALUE: ");
        Serial.println(fromAndroid);

      }
      

    }


    
  }

}

Nandan_n:
I have most of the String except the "fromAndroid" which converts the String to Integer immediately.

I don't see that in the code in Reply #4.

In any case it is the business of putting data into the String that causes problems because of the way the String class allocates memory.

Have a look at how the data is received into a cstring in Serial Input Basics

By the way I am NOT saying that your use of the String class is definitely the problem - just that your symptoms are consistent with the sort of problem Strings can cause.

...R

@ Robin, I am assigning Strings to Integers (post few replacements in String) in this part of the code, where it revives the String from bluetooth module.

while (Serial.available() > 0)
  {
    Serial.println("In Input Loop");
    fromAndroid = Serial.readString();
    if (fromAndroid.startsWith("White"))
    {
      fromAndroid.replace("White", "");
      
      if (fromAndroid.startsWith("time18"))
      {
        fromAndroid.replace("time18", "");
        white_t18 = fromAndroid.toInt();
        Serial.print("white_t18 VALUE: ");
        Serial.println(fromAndroid);

      }
      if (fromAndroid.startsWith("time19"))
      {
        fromAndroid.replace("time19", "");
        white_t19 = fromAndroid.toInt();
        Serial.print("white_t19 VALUE: ");
        Serial.println(fromAndroid);

      }
      if (fromAndroid.startsWith("time20"))
      {
        fromAndroid.replace("time20", "");
        white_t20 = fromAndroid.toInt();
        Serial.print("white_t20 VALUE: ");
        Serial.println(fromAndroid);

      }

inside of loop() function, your 1st while loop might as well be an if statement. It will only execute once.

while (Serial.available() == 0 && Status == "Active")
{
// code ..... //
Status = "NoStatus";
}

Your 2nd while loop is going to execute repeatedly Very quickly. You might want to add a small delay inside this loop to only check for new serial data every 1 second or so. It will still fall out of the loop when serial data becomes available and you are only making updates hourly. Calling the rtc.now() function that fast that often might be troublesome.

while (Serial.available() == 0 && Status == "NoStatus")
{
DateTime now = rtc.now();
currentMillis = millis();
hours = now.hour();
// code .... //

//last
}

I think your loop really only has two states. One where serial data is available and one where it is not.

You can see your logic reduces to three separate sections inside of the loop function

void loop() {
  while (Serial.available() == 0 && Status == "Active")
  {
    // code set 1
    Status = "NoStatus";
  }
  while (Serial.available() == 0 && Status == "NoStatus")
  {
    // code set 2
  }
  while (Serial.available() > 0)
  {
    fromAndroid = Serial.readString();
    // code set 3
  }
  Status = "Active";
}

If you imagine that your code started with the 3rd while loop instead, it would run for as long as serial data was available, then the 1st while would execute, but only once, then the 2nd while loop would execute waiting for serial data, and the sequence would restart. You might consider refactoring your code and only checking for the two states.

void loop() {
  while (Serial.available()) 
  {
    // code set 3
  } 

  // code set 1

  while (!Serial.available())
  {
   // code set 2
   delay(1000);
  }
}

Nandan_n:
I am assigning Strings to Integers (post few replacements in String) in this part of the code, where it revives the String from bluetooth module.

I would not describe your code like that.

But whatever way it is described using Strings can cause problems. I think this is now the third time I have had to say that.

...R

@ Robin, I tried removing all the strings, still the arduino was freezing.

further i tried to remove the RTC code for pulling time, post which the code was running fine. Is there any issue with the code or the hardware.

also how do assign a cstring from the string recived from bluetoth, i tried using the below code but it keeps giving me error “storage size of ‘s’ isn’t known”. could you help in fining the right function for this.

char s[];
  s=Serial.readString();

find below my complete code with no string function and no RTC used in the loop.

#include <Wire.h>
#include "RTClib.h"

////////RTC
int h = 0;
int m = 0;

int hours;
unsigned long interval = 1000;
////////////////


////////////Millis
unsigned long currentMillis;
unsigned long White_previousMillis;
unsigned long previousMillis;//for clock
unsigned long Hr_interval = 3600000;
unsigned long white_each_interval;


////////////////////
int pwm02 = 2;
int pwm03 = 3;
int pwm04 = 4;
int pwm05 = 5;
int pwm06 = 6;
int pwm07 = 7;


/////////////////Peak Keeper
int white_t24 = 10;
int white_t1 = 233;
int white_t2 = 18;
int white_t3 = 189;
int white_t4 = 12;
int white_t5 = 255;
int white_t6 = 19;
int white_t7 = 199;
int white_t8 = 2;
int white_t9 = 177;
int white_t10 = 2;
int white_t11 = 245;
int white_t12 = 12;
int white_t13 = 252;
int white_t14 = 0;
int white_t15 = 255;
int white_t16 = 50;
int white_t17 = 239;
int white_t18 = 20;
int white_t19 = 253;
int white_t20 = 1;
int white_t21 = 252;
int white_t22 = 2;
int white_t23 = 255;


/////Secondary time

int white_Secondary;


int white_steps; //new

int white_move; //New




RTC_DS1307 rtc;
void setup() {
  Serial.begin(9600);
  rtc.begin();
  if (! rtc.begin())
  {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning())
  {
    Serial.println("RTC is NOT running!");
  }
  Serial.println("Start");
  rtc.adjust(DateTime(2017,12,20,8,59,58));
  pinMode(pwm02, OUTPUT);

}

void loop() {

  while (Serial.available() == 0)
  {
   //DateTime now = rtc.now();
    currentMillis = millis();
 //   hours = now.hour();
 //   if (currentMillis - previousMillis >= interval)
 //   {
 //     Serial.print(now.year(), DEC);
 ///     Serial.print('/');
 //     Serial.print(now.month(), DEC);
 //     Serial.print('/');
 //     Serial.print(now.day(), DEC);
 //     Serial.print(" (");
 //     Serial.print(now.hour(), DEC);
 //     Serial.print(':');
 //     Serial.print(now.minute(), DEC);
 //     Serial.print(':');
  //    Serial.print(now.second(), DEC);
  //    Serial.println(')');
  //    previousMillis = currentMillis;
  //  }

  //  if (hours == 9)
 //   {
      if(currentMillis<=white_each_interval)
     {
       white_Secondary = white_t24;
        analogWrite(pwm02, white_Secondary);
      }
      white_steps = white_t1 - white_t24;
      if (white_steps == 0)
    {
      white_move = 0;
      white_each_interval = 1800000;
    }
    if (white_steps < 0)
    {
      white_move = -1;
    }
    else
    {
      white_move = 1;
    }
    if (white_steps != 0)
    {
      white_each_interval = Hr_interval / abs(white_steps);
    }
  //  }


///////////
if (currentMillis - White_previousMillis >= white_each_interval)
      {
        white_Secondary = white_Secondary + white_move;
        analogWrite(pwm02, white_Secondary);
        Serial.print("PWM02: "); Serial.println(white_Secondary);

        White_previousMillis = currentMillis;
      }
  
  }

}

Use something like:

char s[21];

to allocate storage for up to 20 characters plus the terminating NULL ('\0') character.
Make sure the string is long enough to contain the complete received information, and that readString() returns a char array rather than a String object. The manual page mentions string rather than String but I wouldn't take their word for it.

What is the purpose of this line, which seems to encapsulate all the code in loop()?

  while (Serial.available() == 0)

also how do assign a cstring from the string recived from bluetoth,

That is covered in the link I gave you in Reply #5

...R

Out of curiosity, what again is it you are trying to do?

In your current logic it appears that white_steps will always be 223 from line 119. Never zero, never less than zero, always not zero. So white_each_interval will always be 16143 and white_move always 1. Up until something like 4 hours and 48 minutes, white _Secondary continuously goes from undefined to white_t24 or 10 in line 116 and pwm02 gets set to 10 in line 117. If we assume White_previous starts at zero, eventually that same 4 hours and 48 minutes goes by and line 144 kicks in to add 1 whole ms to white_Secondary and pwm02 goes to 11. 4 hours and 48 minutes later it goes up 1 more. 50 days later pwm02 is at 255 and always on.

Somehow I don't believe this is was your plan. i tried following your first description but got a bit lost on that one as well. In general it seems the goal is to set a light at a new level each hour, but gradually fade to that new level so as to go unnoticed. Then there was something about getting commands from an Android.

I think I would set up a 24 place "Array" of PWM goals for the top of each hour. When the hour changed I would get the new PWM setting for the new hour. Calculate distance and direction required to get there. Calculate the time interval required to get that distance in 60 min and then change that much in that direction until the next hour change. Then just keep repeating this loop. After I got that working properly, then while enjoying my fading light, I would write a different sketch to be sure I could receive and print the data from the Android. Then I would try to put the two sketches together and work on the bigger project.

Just a suggestion.