Serial input during running function

Hi,

I'm currently using an Arduino Nano to control a LED matrix consisting of 64 PL9823 LEDs with the help of the FastLED library. To select the various animations, I wanted to use a HC05 bluetooth adapter in combination with my phone. After building and programming the matrix, I wanted to implement the bluetooth control.

My problem there is that I don't know how to read the incoming serial data while the Arduino is controlling the matrix. I've tried to use the serial signal as an interrupt signal so that the animation changes directly, but that didn't work. Is there some other way to solve this?

The animation doesn't have to change immediately, but that would be nice if it's possible. I am also using a selfmade delay function with millis() instead of the normal delay() function to time the animations.

Thanks,

digger955

Have you tried a simple serial.available/serial.read in loop?

If you show your code you might get a more tailored advise.

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

You may also get some ideas from Planning and Implementing a Program

...R

Thanks for your answers.

sterretje: Have you tried a simple serial.available/serial.read in loop?

If you show your code you might get a more tailored advise.

Yes, I tried it with the following test sketch:

int tx = 1;
int rx = 0;
int pin = 3;
volotile char inSerial[8];

void setup()
{
  pinMode(tx, OUTPUT);
  pinMode(rx, INPUT);
  pinMode(13, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(pin), btSerial(), FALLING);
}

void loop()
{
  if(!strcmp(inSerial,"fast"))
  {
    digitalWrite(13, HIGH);
    millisDelay(100);
    digitalWrite(13, LOW);
    millisDelay(100);
  }
  else if(!strcmp(inSerial,"slow"))
  {
    digitalWrite(13, HIGH);
    millisDelay(1000);
    digitalWrite(13, LOW);
    millisDelay(1000);
  }
}

void btSerial()
{
  int i = 0;
                        
  if(Serial.available() > 0)
  {             
    while(Serial.available() > 0)
    {
      inSerial[i] = Serial.read();
      i++;
    }
    inSerial[i] = '\0';
  }
}

void millisDelay(int delayTime) // Delay function with millis
{
  unsigned long currentMillis = millis();
  unsigned long previousMillis = millis();
  bool waiting = true;
  
  while (waiting)
  {
    currentMillis = millis();
    
    if (currentMillis - previousMillis >= delayTime)
    {
      waiting = false;
    }
  }
}

I've also connected the RX pin of the Arduino with pin 3 for the interrupt.

Robin2: Have a look at the examples in Serial Input Basics - simple reliable, non-blocking ways to receive data.

You may also get some ideas from Planning and Implementing a Program

...R

I'll have a look at it, maybe that could work.

Why interrupts? The serial reading is already interrupt driven. Connecting the rx to an interupt pin does not make sens from that perspective.

You can do a (simple) reading from loop() as demonstrated in the first link that Robin provided. That will collect text from the serial port in a character array and next you can use your strcmp as you do now.

sterretje:
Why interrupts? The serial reading is already interrupt driven. Connecting the rx to an interupt pin does not make sens from that perspective.

Ok, that makes sense.

Robin2:
You may also get some ideas from Planning and Implementing a Program

I used your code for the serial communication and modified it slightly, it works!

Thanks everyone for your help!