How can i run loop and LoRa read at same time?

HI everyone,

I am working with P2P sample from your website and has done some modification.
I want to read incoming msg everytime so in whlie loop i have used " LORA_Read(msg); "
so it can read for msg ,if any, for each count. but there is some delay after each count++.
so i am looking for how to reduce delay OR is there any other option to run while loop and LORA_Read(msg); both at same time? if any please do tell. it is also welcomed if you have alternate coding for these program.

Here is my code:

#include <Keyboard.h>

#include <Arduino.h>

#include “LORA_P2P_lib.h”

#define CONSOLE_STREAM SerialUSB
#define LORA_STREAM Serial1

bool LED_Flag = 1;
volatile bool send_Flag = 0;

void setup()
{
pinMode(LED_GREEN, OUTPUT);
digitalWrite(LED_GREEN, HIGH);
pinMode(LED_BLUE, OUTPUT);
digitalWrite(LED_BLUE, HIGH);
digitalWrite(2,HIGH);
LORA_STREAM.begin(57600);
CONSOLE_STREAM.begin(57600);
delay(3000);

CONSOLE_STREAM.println(“Started!”);

LoraP2P_Setup();

CONSOLE_STREAM.println(“LORA SETUP DONE!”);

attachInterrupt(2, BTN_ISR, FALLING);

}

void BTN_ISR() {

send_Flag = 1;

CONSOLE_STREAM.println(“Interrupt!”);

}

void loop()
{
CONSOLE_STREAM.print("Input Pin state is: ");
CONSOLE_STREAM.println(digitalRead(2));
if (send_Flag == 1) {

send_Flag = 0;
CONSOLE_STREAM.println("Sending Message...");
LORA_Write("10");
delay(1000);

}
else {

char msg[100] = "";

CONSOLE_STREAM.println("Listening for Message...");
int errorCode = LORA_Read(msg); // We have a message if returncode = 1

if (errorCode == 1 && msg[0] == '1' && msg[1] == '0') { // Switch LED if message = 10


  volatile int count = 0;
  while (count < 10) {
    digitalWrite(LED_GREEN, LOW); // Turn on pin 2 if there is a message
    delay(1000);
    digitalWrite(LED_GREEN, HIGH);
    delay(30);
    digitalWrite(LED_BLUE, LOW);
    delay(1000);
    digitalWrite(LED_BLUE, HIGH);
    CONSOLE_STREAM.println(count);
    count++;
    char msg[100] = "/0";
    LORA_Read(msg); // We have a message if returncode = 1

if (errorCode == 1 && msg[0] == ‘1’ && msg[1] == ‘0’)
{count = 0;

 }

}


}
else {

  CONSOLE_STREAM.println("Error: ");
  CONSOLE_STREAM.println(errorCode);
}

}
}

The LoRa library is designed to run synchronously, so it is not really possible to make asynchronous calls.

If there is an unnecessary delay when you call LORA_Read(msg)_ and the return code != 1, there are few options.

You can attach an interrupt, or more specifically modify the existing interrupt, which gets triggered when data is received on the UART attached to the LoRa module. However, it would probably be easier just to check if there is any received data in the LoRa stream (LORA_STREAM.available()), if not then you can skip the call to LORA_Read(msg).

Note: Data in the stream does not necessarily mean there is a message, it could be other data sent by the LoRa module.

how to set interrupt when new data is triggered on UART?
I want to listen for msg when I am in while loop.
please give me some idea
thank you.

Probably the easiest way to do this would be to modify the variant files.

For example the SodaqOne variant.cpp: https://github.com/SodaqMoja/SodaqCore-samd/blob/v1.6.15-sodaq/variants/sodaq_one/variant.cpp

At the end their are two functions labelled SERCOM2_Handler and SERCOM5_Handler. These are the hardware ISRs for those two SERCOMs. The code there simply links that ISR to the function in the UART class which deals with the various hardware interrupts: https://github.com/SodaqMoja/SodaqCore-samd/blob/v1.6.15-sodaq/cores/arduino/Uart.cpp

You could add another function call at the end of the ISRs in variant.cpp to call whatever you want. You might want to use a weak binding so that it will compile regardless of whether the additional functions (which are called from those ISRs) are declared or not in your sketch.