Problem about Smart-home Arduino Programming

Hello guys I am new to arduino program and recently I encounter problem when functions in a smart-home combined together. Hope all of you can give me a helping hand :slight_smile:

I want to communicate between smart-home (Arduino) and Smart phone (Android) and the following are part of my code:

void setup()
{
    Serial.begin(9600);   
    pinMode(ledpin, OUTPUT);                   //set pin 10 as output LED
    pinMode(motionpin, INPUT);                 // set pin 2 as input sesnor
    pinMode(Buzzer, OUTPUT) ;                  // set pin 9 as output  buzzer
}
void loop()
{
   if(Serial.available() > 0)          
   {

      data = Serial.read();  
     ledintensity = Serial.read();               //read the data from smartphone (LED brightness control) 
     Serial.println(ledintensity);
     analogWrite(ledpin,ledintensity);       // control the brightness (I want this to be instantly)
}
   temp = analogRead(tempPin);            //read the temp sensor on the breadboard
    temp = temp * 0.48828125;              //calculate temperature
    Serial.print("Temperature = ");
    Serial.print(temp);
    Serial.print("*C");
    Serial.println();
    delay(1000);                                   //the delay make me fail to control LED brightness instantly!
}

From the above coding, I want to control the LED light intensity by sliding the slider (from 0-100%) and I am using App Inventor 2 to build the app. At the same time there is a LM35 temperature sensor on the breadboard and record the temperature and output it on my Android phone 's screen.
The problem is the byte sending out from the smartphone following the delay at the end of the arduino code [dealy (1000);] which is too slow and the LED can just change the intensity with lots of delay. But I don't really want to set down the delay value or the temperature value with keep updating every moment.
How can I deal with this problem guys? Please I want instantly LED light control while my temperature value will not update so fast, is this possible? thank you very much! And sorry for my bad English.

"part of my code" was a bad decision as we cant test it. This will not compile because of that, but you can see my added lines. It will loop without delays to react quick on incoming serial, and take a temp reading every 10 seconds.

unsigned long tempInterval = 10000;
unsigned long myTime;
unsigned long prevTime = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(ledpin, OUTPUT);                   //set pin 10 as output LED
  pinMode(motionpin, INPUT);                 // set pin 2 as input sesnor
  pinMode(Buzzer, OUTPUT) ;                  // set pin 9 as output  buzzer
}
void loop()
{
  if (Serial.available() > 0)
  {

    data = Serial.read();
    ledintensity = Serial.read();               //read the data from smartphone (LED brightness control)
    Serial.println(ledintensity);
    analogWrite(ledpin, ledintensity);      // control the brightness (I want this to be instantly)
  }
  myTime = millis();
  if (myTime - prevTime > tempInterval)
  {
    temp = analogRead(tempPin);            //read the temp sensor on the breadboard
    temp = temp * 0.48828125;              //calculate temperature
    Serial.print("Temperature = ");
    Serial.print(temp);
    Serial.print("*C");
    Serial.println();
    //delay(100);                                   //the delay make me fail to control LED brightness instantly!
    prevTime += tempInterval;
  }
}
   if(Serial.available() > 0)          
   {

      data = Serial.read();  
     ledintensity = Serial.read();               //read the data from smartphone (LED brightness control) 
     Serial.println(ledintensity);
     analogWrite(ledpin,ledintensity);       // control the brightness (I want this to be instantly)
}

You are checking if there is at least 1 byte of data in the serial buffer, but then you proceed to read 2 bytes. What happens when this code executes and the 2nd byte hasn't been received yet?

What, EXACTLY, are you using the serial port for? To send debug data to the PC OR to get info from the Android? Both is the wrong answer. So is yes.