BluetoothCommunicationProblem

Hello Arduino Lovers,
I am trying to control an RGB with an android application via bluetooth. Application will be sending ‘ON’ string, and the loop will be started just for one time. This project is intended to build a sunrise alarm clock, so the RGB is used to simulate sun rising colors.

I am having some problem with the Bluetooth communication (serial monitor) on the below program;

#include <SoftwareSerial.h>


SoftwareSerial BT(5, 6); //TX, RX respetively
String state;// string to store incoming message from bluetooth

int redPin = 11;
int greenPin = 10;
int bluePin = 9;

        int r = 0;
        int g = 0;
        int b = 0; 
        
        int redList[] = {  0,8,17,26,35,43,52,61,70,79,87,96,105,114,123,131,140,149,158,167,175,184,193,202,211,219,228,237,246,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,254,253,253,252,252,251,250,250,249,249,248,247,247,246,246,245,245,244,243,243,242,242,241,240,240,239,239,238,237,237,236,236,235,235,234,233,233,232,232,231,230,230,229,229,228,228,227,226,226,225,225,224,223,223,222,222,221,220,220,219,219,218,218,217,216,216,215,215,214,213,213,212,212,211,211,210,209,209,208,208,207,206,206,205,205,204,203,203,202,202,201,201,200,199,199,198,198,197,196,196,195,195,194,193,193,192,192,191,191,190,189,189,188,188,187,186,186,185,185,184,184,183,182,182,181,181,180,179,179,178,178,177,176,176,175,175,174,174,173,172,172,171,171,170,169,169,168,168,167,167,166,165,165,164,164,163,162,162,161,161,160,159,159,158,158,157,157,156,155,155,154,154,153,152,152,151,151,150,150,149,148,147,146,145,144,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128
        };
        int greenList[] = {  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,4,6,8,10,13,15,17,19,21,23,26,28,30,32,34,36,39,41,43,45,47,49,52,54,56,58,60,62,65,67,69,71,73,75,78,80,82,84,86,88,91,93,95,97,99,101,104,106,108,110,112,114,117,119,121,123,125,128,128,130,132,135,137,140,142,145,147,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,150,149,148,147,146,145,144,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128
        };
        int blueList[] = {  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,5,6,7,9,10,11,12,14,15,16,18,19,20,22,23,24,25,27,28,29,31,32,33,34,36,37,38,40,41,42,44,45,46,47,49,50,51,53,54,55,57,58,59,60,62,63,64,66,67,68,69,71,72,73,75,76,77,79,80,81,82,84,85,86,88,89,90,92,93,94,95,97,98,99,101,102,103,104,106,107,108,110,111,112,114,115,116,117,119,120,121,123,124,125,127,128,129,130,132,133,134,136,137,138,139,141,142,143,145,146,147,149,150,151,152,154,155,156,158,159,160,162,163,164,165,167,168,169,171,172,173,174,176,177,178,180,181,182,184,185,186,187,189,190,191,193,194,195,197,198,199,200,202,203,204,206,207,208,209,211,212,213,215,216,217,219,220,221,222,224,225,226,228,229,230,232,233,234,235,236,237,238,240,241,242,243,244,245,246,247,248,249,250,251,252,253,255
        };
        
        
        int x = 0;
        int duration = 6000;

        //300 color values times "duration" milliseconds is the time the sunrise goes for.
        //The sunset is set to 1/6 of that time. At 6000ms this will take 30mins for a sunrise,
        //and 5mins for a sunset. This value can be changed by the slider on the webpage, 
        //from 0 to 6000.





	//uncomment this line if using a Common Anode LED
	//#define COMMON_ANODE
	 
	void setup()
	{
        Serial.begin(9600);

	BT.begin(9600);// bluetooth serial communication will happen on pin 10 and 11

        
        pinMode(redPin, OUTPUT);
        pinMode(greenPin, OUTPUT);
        pinMode(bluePin, OUTPUT);
        
        
	 
	}
	 
	void loop()
	{
	 while (BT.available())
	{  //Check if there is an available byte to read
  	delay(10); //Delay added to make thing stable 
  	char c = BT.read(); //Conduct a serial read
 	 state += c; //build the string- either "On" or "off"
 	 }  
        if (state.length() > 0) {
    	Serial.println(state); }
	sunrise();
	}

        //set the colors all at once, usefull as shorthand.
        void color (unsigned char r, unsigned char g, unsigned char b)
        {     
        analogWrite(redPin, r);     
        analogWrite(greenPin, g);
        analogWrite(bluePin, b);
        
        };


        //Sunrise function. Walks through the arrays.
        void sunrise()
        {

	

	
	if(state == "ON") 
  	{
        for (x = 0; x < 300; x++){
    	r = redList[x];
    	g = greenList[x];
    	b = blueList[x];
    	myDelay(duration);
        color(r, g, b);
    	
        };
	
        }
	
	}
	
	
        // duration is delay time in milliseconds
        void myDelay(unsigned long duration)
        {
        unsigned long start = millis();
        while (millis() - start <= duration);
        
        }

I have tested the RGB functionality with the below codes;

#include <SoftwareSerial.h>


SoftwareSerial BT(10, 11); //TX, RX respetively
String state;// string to store incoming message from bluetooth


void setup() {
 BT.begin(9600);// bluetooth serial communication will happen on pin 10 and 11
 Serial.begin(9600); // serial communication to check the data on serial monitor
  pinMode(13, OUTPUT); // LED connected to 13th pin




}
//-----------------------------------------------------------------------//  
void loop() {
  while (BT.available()){  //Check if there is an available byte to read
  delay(10); //Delay added to make thing stable 
  char c = BT.read(); //Conduct a serial read
  state += c; //build the string- either "On" or "off"
  }  
  if (state.length() > 0) {
    Serial.println(state); 


  if(state == "ON") 
  {
    digitalWrite(13, HIGH);
    
      } 
  
  else if(state == "OFF") 
  {
    digitalWrite(13, LOW);
     }
  
  
state ="";}} //Reset the variable

which functions properly. And i modified above codes to adapt into intended program. This program shows on or off on the serial monitor when the button is pressed.

I assume the problem is related where the

 while (BT.available())
	{  //Check if there is an available byte to read
  	delay(10); //Delay added to make thing stable 
  	char c = BT.read(); //Conduct a serial read
 	 state += c; //build the string- either "On" or "off"
 	 }  
        if (state.length() > 0) {
    	Serial.println(state); }
	sunrise();
	}

is used, I tried placing above codes in the sunrise function, etc…, however my combinations did not help me.

As you understood i am not good with the programming logic, i appreciate any idea since i am a newbie.

Thanks in advance!

Any Ideas?

Try removing the delay(10) and test