Serial Communcation Timer/Interupt Problem

Hi there,

I am trying to use Bluetooth HC-05 (38400 Baud Rate) to control my Arduino Nano.
At the same time I am using the TLC5940 Library to control LEDs. To control animations I am simply waiting the loop() until a certain FrameTime passed (using "millis()" to check if frameTime has passed).
I am confused right now of the usage of timers from the different features.
When I try to use the standard HardwareSerial Serial Connection (RX/TX) I am not able to reliable receive correct data, most of the time it does get a Serial.available() but is not able to the data. When I disable the TLC and millis() functionality it works as it should.
I also tried to use the SoftwareSerial library with e.g. RX Pin D8, TX D12 with no succes either.

Are they simply conflicting with the timers and is there a way around this?

void loop()
{
 
  if(micros()-oldMicros>= LayerDuration)// waits until LayerDuration is reached and than goes through cycle
  {       

     oldMicros=micros();
    if(millis()-frameTime>=FRAME_TIME)
     {

      //AllOff();
      switch(curAnim)
      {
        case 0:randomLeds(30,30,30);//randomLeds(20,20,20);//RGBColorRoom();//ColorCycle();//ValueLed[3][LedBlue[0][1]]=4000;ValueLed[3][LedGreen[0][1]]=4000;ValueLed[3][LedRed[0][1]]=4000;;randomLeds(1,0,0);wallWaag(maxbright,0,0,0,false);wallWaag(0,maxbright,0,1,false);wallSenk(0,0,maxbright,true);
               break;
        case 1:Snake();
               break;      
        case 2:randomLedsFull();//wallSenk(brightR,brightG,brightB,true);//wallWaag(brightR,brightG,brightB,0,true);
               break;       
        case 3:AllRed();//ColorCycle();
               break;       
//...
        default:randomLeds(1,0,0);
               break;   
      }
       frameTime=millis();
       FrameCount ++;  
       if(FrameCount>=MAXCOUNT){FrameCount=0;}  
     }
     CubeUpdate(layer);                                                 // sets the values for the tlc5940 Outputs and puts all MOSFET Gates HIGH (not active) except for one MOSFET Low (active) -->this layer is ON, also look under tab "function"  
     layer = (layer + 1) % CUBE_SIZE;                                   // layer counter +1
   }
   
  if (Serial.available())
  {
   // String s=Serial.readStringUntil('p');#
      String s="";
      while(Serial.available())
      {
        c=(byte)Serial.read();
        s+=c;
      }
    if(s=="off")
      ledOff();
    else //if(s=="on")
       ledOn();
    delay(2000);
    ledOff();
  }
 }

Have you forgotten something?

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

void loop()
{
 
  if(micros()-oldMicros>= LayerDuration)// waits until LayerDuration is reached and than goes through cycle
  {       

     oldMicros=micros();
    if(millis()-frameTime>=FRAME_TIME)
     {

      //AllOff();
      switch(curAnim)
      {
        case 0:randomLeds(30,30,30);//randomLeds(20,20,20);//RGBColorRoom();//ColorCycle();//ValueLed[3][LedBlue[0][1]]=4000;ValueLed[3][LedGreen[0][1]]=4000;ValueLed[3][LedRed[0][1]]=4000;;randomLeds(1,0,0);wallWaag(maxbright,0,0,0,false);wallWaag(0,maxbright,0,1,false);wallSenk(0,0,maxbright,true);
               break;
        case 1:Snake();
               break;      
        case 2:randomLedsFull();//wallSenk(brightR,brightG,brightB,true);//wallWaag(brightR,brightG,brightB,0,true);
               break;       
        case 3:AllRed();//ColorCycle();
               break;       
//...
        default:randomLeds(1,0,0);
               break;   
      }
       frameTime=millis();
       FrameCount ++;  
       if(FrameCount>=MAXCOUNT){FrameCount=0;}  
     }
     CubeUpdate(layer);                                                 // sets the values for the tlc5940 Outputs and puts all MOSFET Gates HIGH (not active) except for one MOSFET Low (active) -->this layer is ON, also look under tab "function"  
     layer = (layer + 1) % CUBE_SIZE;                                   // layer counter +1
   }
   
  if (Serial.available())
  {
   // String s=Serial.readStringUntil('p');#
      String s="";
      while(Serial.available())
      {
        c=(byte)Serial.read();
        s+=c;
      }
    if(s=="off")
      ledOff();
    else //if(s=="on")
       ledOn();
    delay(2000);
    ledOff();
  }
 }

Here is the actual code, I did not include the other methods, since they do not seem to be the problem to talk about. The communication between the HC 05 Bluetooth device and Arduino start with the "if(Serial.available)" line. As you can see I tried different methods to read the Serial data, but neither work if I did not comment out the rest of the loop function above. (Its actually multiplexing a LED Cube with TLC5940 as LED Driver with the library as described in the first post.
The two functionalites obviously seem to conflict
So what is a possible work around?

I think your mistake is assuming that if one character is available then all the characters are available. Since you need three characters to see if they are "off" maybe you should change:

  if (Serial.available())

to:

  if (Serial.available() >= 3)

Of course that won't work for any input longer or shorter than three characters so you should probably read characters one at a time and act when you receive a character that signals the end of the input. See: http://forum.arduino.cc/index.php?topic=396450.0

Thanks john for the suggestion, but the serial communication code seems to be fine. I changed it but still the same result after a lot of testing things out...
I am really turning crazy right now... I thought I got it to work but it looks like it still does not.
If I comment out all the TLC function call it seems to work, so I guess the library is the problem.

PhilKey:
I changed it but still the same result after a lot of testing things out...
I am really turning crazy right now... I thought I got it to work but it looks like it still does not.

Post the latest version of your program and tell us in as much detail as you can what happens when you run it.

...R