'Delay command' and bluetooth communication problem

I have a project where I send data to arduino via bluetooth using an Android application.For example, when I press 1 from the 'bar led' part of the application, an animation needs to happen.When I press 2, a different animation will be displayed.However, because there is a 'delay command' in the animations, there is a delay in data exchange when I press the button.How can I prevent this?Is there a command I can use instead of the 'Delay command'?

void loop() {
  if (bluetooth.available()) {
  while (bluetooth.available())                    
    {                                                
      bar_analog = bluetooth.parseInt();              
      head_analog = bluetooth.parseInt();
      bar_anim = bluetooth.parseInt();
      strip_anim = bluetooth.parseInt();
      motor_speed = bluetooth.parseInt();
      direct = bluetooth.parseInt();
      level = bluetooth.parseInt();
      if (debug) {                                  
        Serial.print("Bar Brightness: ");                        
        Serial.println(bar_analog);
        Serial.print("Bar Animation Type: ");
        Serial.println(bar_anim);
        Serial.print("Headlight Brightness: ");
        Serial.println(head_analog);
        Serial.print("Strip Animation Type: ");
        Serial.println(strip_anim);
        Serial.print("Motor Speed Level: ");
        Serial.println(motor_speed);
        Serial.print("Direction: ");
        Serial.println(direct);
        Serial.print("Mast Goes: ");
        Serial.println(level);
        Serial.println("--------------------------------");
      }
      if (bluetooth.read() == '\n') {            
      
      }
  }}
  if(bar_anim==1) {  
  digitalWrite(8,HIGH);
  delay(50);
  digitalWrite(8,LOW); 
  delay(250);
  }
  else if(bar_anim==2) {
  digitalWrite(8,HIGH);
  delay(250);
  digitalWrite(8,LOW); 
  delay(50);  
}
}
1 Like

You learn to write non-blocking code. It's not rocket science but it is a different approach than the usual top-down method.

Tutorials:

  1. Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs
  2. Gammon Forum : Electronics : Microprocessors : How to process incoming serial data without blocking