1

I'm sending data to Arduino through the mobile app. So far, all the code has been running consistently.

#include <SoftwareSerial.h>
#define FASTLED_INTERNAL
#include <FastLED.h>
#define NUM_LEDS_PER_STRIP 48
SoftwareSerial bluetooth(53,52);    // TX,RX                               
int bar_analog=0;                                    
int head_analog=0;
int bar_anim=0;
int anim=0;
int motor_speed=1;
int direct=0;
int level=0;
CRGB strip1[NUM_LEDS_PER_STRIP];
CRGB strip2[NUM_LEDS_PER_STRIP];
CRGB strip3[NUM_LEDS_PER_STRIP];
CRGB strip4[NUM_LEDS_PER_STRIP];
boolean debug = true;
int IN1a=49;
int IN2a=48;
int PWM1=3;
int IN1b=22;
int IN2b=23;
int PWM2=4;
int hiz=100;
int PWM_kare=2;
int INA_kare = 51;  
int INB_kare = 50; //LED '+'sı OUTPUT B'de olduğu için INB'yi  HIGH yapıyoruz.
void m1_cw() {
digitalWrite(IN1a,HIGH);
digitalWrite(IN2a,LOW);
analogWrite(PWM1,hiz);
}
void m1_ccw() {
digitalWrite(IN1a,LOW);
digitalWrite(IN2a,HIGH);
analogWrite(PWM1,hiz);
}
void m2_cw() {
digitalWrite(IN1b,HIGH);
digitalWrite(IN2b,LOW);
analogWrite(PWM2,hiz);
}
void m2_ccw() {
digitalWrite(IN1b,LOW);
digitalWrite(IN2b,HIGH);
analogWrite(PWM2,hiz);
}
void m1_stop() {
digitalWrite(IN1a,LOW);
digitalWrite(IN2a,LOW);
analogWrite(PWM1,0);
}
void m2_stop() {
digitalWrite(IN1b,LOW);
digitalWrite(IN2b,LOW);
analogWrite(PWM2,0);
}
void mast_up() {
digitalWrite(24,HIGH);
digitalWrite(25,LOW);
}
void mast_down() {
digitalWrite(24,LOW);
digitalWrite(25,HIGH);
}
void mast_stop() {
digitalWrite(24,LOW);
digitalWrite(25,LOW);
}
void kare() {
analogWrite(PWM_kare,head_analog);
digitalWrite(INB_kare, HIGH);
digitalWrite(INA_kare, LOW);
}
void strip_dim() {
for(int i=0;i<256;i=i+10) {
for(int j=0;j<NUM_LEDS_PER_STRIP;j++) {
FastLED.setBrightness(i); //Parlaklık 
strip1[j] = CRGB( 100, 172, 45); //leds[0] şeritteki 1.Ledi ifade eder. (Buradaki leds variable'dır.)
strip2[j] = CRGB( 100, 172, 45);
strip3[j] = CRGB( 100, 172, 45);
strip4[j] = CRGB( 100, 172, 45);
FastLED.show(); 
}}
for(int i=255;i>=0;i=i-10) {
for(int j=0;j<NUM_LEDS_PER_STRIP;j++) {
FastLED.setBrightness(i); //Parlaklık 
strip1[j] = CRGB( 100, 172, 45); //leds[0] şeritteki 1.Ledi ifade eder. (Buradaki leds variable'dır.)
strip2[j] = CRGB( 100, 172, 45);
strip3[j] = CRGB( 100, 172, 45);
strip4[j] = CRGB( 100, 172, 45);
FastLED.show(); 
}}
}
void strip_stop() {
for(int j=0;j<NUM_LEDS_PER_STRIP;j++) {
FastLED.setBrightness(0); //Parlaklık 
strip1[j] = CRGB( 100, 172, 45); //leds[0] şeritteki 1.Ledi ifade eder. (Buradaki leds variable'dır.)
strip2[j] = CRGB( 100, 172, 45);
strip3[j] = CRGB( 100, 172, 45);
strip4[j] = CRGB( 100, 172, 45);
FastLED.show(); 
}
}
void setup() {
  Serial.begin(9600);                              
  bluetooth.begin(9600);                           
  Serial.println("Program is starting...");              
  Serial.println("by Berke Ogulcan Parlak"); 
pinMode(IN1a,OUTPUT);
pinMode(IN2a,OUTPUT);
pinMode(IN1b,OUTPUT);
pinMode(IN2b,OUTPUT);
pinMode(PWM1,OUTPUT);
pinMode(PWM2,OUTPUT);
pinMode(24,OUTPUT);
pinMode(25,OUTPUT);
pinMode(INA_kare, OUTPUT);
pinMode(INB_kare, OUTPUT);
pinMode(PWM_kare, OUTPUT);
digitalWrite(IN1a,LOW);
digitalWrite(IN2a,LOW);
digitalWrite(IN1b,LOW);
digitalWrite(IN2b,LOW);
digitalWrite(PWM1,LOW);
digitalWrite(PWM2,LOW);  
digitalWrite(24,LOW);
digitalWrite(25,LOW);
digitalWrite(INA_kare, LOW);
digitalWrite(INB_kare, LOW);
digitalWrite(PWM_kare, LOW); 
FastLED.addLeds<NEOPIXEL, 26>(strip1, NUM_LEDS_PER_STRIP); 
FastLED.addLeds<NEOPIXEL, 28>(strip2, NUM_LEDS_PER_STRIP);
FastLED.addLeds<NEOPIXEL, 30>(strip3, NUM_LEDS_PER_STRIP);
FastLED.addLeds<NEOPIXEL, 32>(strip4, NUM_LEDS_PER_STRIP);
FastLED.setBrightness(0); 
}

void loop() {
  while (bluetooth.available())                    
    {                                                
      bar_analog = bluetooth.parseInt();              
      head_analog = bluetooth.parseInt();
      bar_anim = bluetooth.parseInt();
      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(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') {               

  }} 
        kare();            
       if(direct==1) { //ileri
  m1_ccw();
  m2_ccw(); 
  }
else if(direct==2) { //geri
  m1_cw();
  m2_cw(); 
  }
else if(direct==3) { //sol
  m1_ccw();
  m2_cw(); 
  }
else if(direct==4) { //sağ
  m1_cw();
  m2_ccw(); 
  }
else if(direct==0) { //durur
  m1_stop();
  m2_stop(); 
  }
if(level==1) { //mast yukarı
  mast_up(); 
  }
else if(level==2) { //mast aşağı
  mast_down(); 
  }
else if(level==0) { //mast durur
  mast_stop(); 
  }
if(anim==2) {
  strip_dim();
  }
else if(anim==0) {
  strip_stop();
  }
  }

But when I added the for loop to the code, all the data started to shift. (When I press the button from the application 'lag' occurs and the data is mixed.)I mentioned that part of the code below. Why does the data shift?

void strip_dim() {
for(int i=0;i<256;i=i+10) {
for(int j=0;j<NUM_LEDS_PER_STRIP;j++) {
FastLED.setBrightness(i); //Parlaklık 
strip1[j] = CRGB( 100, 172, 45); //leds[0] şeritteki 1.Ledi ifade eder. (Buradaki leds variable'dır.)
strip2[j] = CRGB( 100, 172, 45);
strip3[j] = CRGB( 100, 172, 45);
strip4[j] = CRGB( 100, 172, 45);
FastLED.show(); 
}}
for(int i=255;i>=0;i=i-10) {
for(int j=0;j<NUM_LEDS_PER_STRIP;j++) {
FastLED.setBrightness(i); //Parlaklık 
strip1[j] = CRGB( 100, 172, 45); //leds[0] şeritteki 1.Ledi ifade eder. (Buradaki leds variable'dır.)
strip2[j] = CRGB( 100, 172, 45);
strip3[j] = CRGB( 100, 172, 45);
strip4[j] = CRGB( 100, 172, 45);
FastLED.show(); 
}}
}
void strip_stop() {
for(int j=0;j<NUM_LEDS_PER_STRIP;j++) {
FastLED.setBrightness(0); //Parlaklık 
strip1[j] = CRGB( 100, 172, 45); //leds[0] şeritteki 1.Ledi ifade eder. (Buradaki leds variable'dır.)
strip2[j] = CRGB( 100, 172, 45);
strip3[j] = CRGB( 100, 172, 45);
strip4[j] = CRGB( 100, 172, 45);
FastLED.show(); 
}
}
1
  • 3
    Why are you using software serial on a Mega?!?!
    – Majenko
    Jul 23, 2019 at 17:15

2 Answers 2

3

Your chief problem is that FastLED turns off interrupts when sending the data. It does that because timing is critical, and with interrupts turned on that timing will get completely messed up.

So with FastLED running repeatedly in a loop there is very little time when interrupts are enabled for reception to occur over SoftwareSerial.

Unlike a real UART, which can receive and store a byte before triggering an interrupt, SoftwareSerial triggers an interrupt to at the start of receiving a byte and that interrupt routine is used to do the actual reception. So if interrupts aren't enabled at the moment a byte starts arriving on the GPIO pin it can't start receiving, so it misses the byte and it ends up a mess.

Since you are using an Arduino Mega there is absolutely no reason for you to use SoftwareSerial. You have plenty of real hardware UARTs available to connect your Bluetooth device to which will be able to receive a byte while FastLED is sending out data (as long as it's not sending out data for too long...).

2
  • Upvoted your comment, and your answer gave rise to a question from my side related to your answer. I hope you can take a short look at it. arduino.stackexchange.com/questions/67381/… Jul 23, 2019 at 18:47
  • First of all, thank you for your answer, but I don't know much about UART. What kind of change should I make in code to use UART instead of SoftwareSerial?
    – bopele
    Jul 24, 2019 at 7:18
1

You are creating an instance of a class a lot of times (25 * 48 * 8 + 255 * 48 * 4 = 58,560 times), and that is a costly operation.

Instead, make one instance (before the for loops), and use that to assign everywhere you need it.

Something like;

CRGB crgb( 100, 172, 45);

for(int i=0;i<256;i=i+10) {
    for(int j=0;j<NUM_LEDS_PER_STRIP;j++) {
        FastLED.setBrightness(i);
        strip1[j] = crgb;
        ...
3
  • Good point. However, if setBrightness is doing serial communications, >58,000 loop iterations is still going to take a substantial amount of time.
    – Duncan C
    Jul 23, 2019 at 17:36
  • @DuncanC True, but I didn't want to change functionality. Jul 23, 2019 at 18:37
  • Thanks for the comment but there hasn't been any change.
    – bopele
    Jul 24, 2019 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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