0

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);  
}
} 

app

  • Do you mean a delay with every command that you send? Or just with one special command? – chrisl Jul 23 at 8:53
  • Every data I send is delayed because there is a 'delay command' in the animation part. – bopele Jul 23 at 9:14
  • 2
    See the example program BlinkWithoutDelay.ino in the IDE. – Majenko Jul 23 at 9:20
1

I see 2 problems with your code:

  1. You use the delay() function, which is not good coding style, since it is basically busy waiting. The Arduino will do nothing in this time, also not reacting to any input. Instead you should use a non-blocking coding style, which only does something, when it is time to do so, and will not block until something happens. You can see this style in the BlinkWithoutDelay example, that comes with the Arduino IDE. It uses millis() as a time measure. Think of how you would bake a pizza. You would not sit on front of the oven doing nothing, until the pizza is ready. You would do other things while looking at your watch regularily, checking, if it is time to take out the pizza.

  2. In the Serial communication part you use parseInt(). That can be a problem, since it has a timeout of 1s (standard value). Until the timeout the function will block. This is mitigated, if you consecently send a non-digit character at the end of every value. But it is better to first read a full message (until the newline character \n) and then process the message as a whole.

Also the line if (bluetooth.available()) is not necessary here, since the while loop will do the same.

  • Thank you for your nice answer.I tried to use the millis () command. However, I was able to turn the LED on and off with only 1 second interval.For example, I want the LED to be on for one second and off for half a second.How do I do this using millis ()? – bopele Jul 23 at 10:37
  • You can use an if statement to distinguish between OFF state and ON state. Inside this if/else you put the millis() if statements with different interval values – chrisl Jul 23 at 10:38
  • I solved this problem, thank you.I have one last question.Is it possible to use millis () with the for loop?e.g for (int j = 0; j <NUM_LEDS_PER_STRIP; j ++) { fastled.setbrightness (255); leds [j] = CRGB (100, 172, 45); fastled.show (); delay (100); } Can I use millis () instead of 'delay' here? – bopele Jul 23 at 11:08
  • You should not use a for loop in this case. You have to build the functionality yourself by declaring a variable, that holds the number of the current LED. Then write a millis if statement, in which you set the color of the current LED and increment the variable to the next number. – chrisl Jul 23 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.