LoRa+reading PWM from sensor

Hi everyone!

I am facing some problems reading data from mb1040 ultrasonic sensor (see link below) and send it using LoRa.

I managed to read the distance reading in its own script, without sending data via LoRa. I also managed to send random data via LoRa. But, when I combine both I does not read the sensor.

Reading data only:
`#include “Arduino.h”

//const int pwPin1 = 2;
long pulse1, sensor1;

void setup() {
Serial.begin(115200); // sets the serial port to 9600
// pinMode(pwPin1, INPUT);
pinMode(PWM1,INPUT);
}

void loop() {

pulse1 = pulseIn(PWM1, HIGH);
sensor1 = pulse1/147;

Serial.print(sensor1);
Serial.print(" “);
Serial.print(“inch from PWM”);
Serial.println(” ");

delay(500);
}`

Reading data and sending via LoRa in the same script, I include my code in the prepareTxFrame() function. Unfortunately, it does not read the data.

Any suggestion is greatly appreciated.
Thanks very much in advance!

Sensor: https://www.maxbotix.com/Ultrasonic_Sensors/MB1040.htm

Can you show me the prepareTxFrame() code you wrote?

@Xiao-H thank you for you response. This is the function below, I used the exact same code without LoRa and it was working fine.

    static void prepareTxFrame
( uint8_t port )
{
  
  pinMode(PWM1,INPUT);

  
   pulse1 = pulseIn(PWM1, HIGH);
  sensor1 = pulse1/147;
  

    appDataSize = 4;
    appData[0] = 0x00;
    appData[1] = 0x01;
    appData[2] = 0x02;
    appData[3] = 0x03;
     Serial.print(sensor1);
  Serial.print(" ");
  Serial.print("inch from PWM");
  Serial.println(" ");
}

Don’t use delay with loraWan. Use duty cycles.

I’m not using delays in the script. And there are examples from the library using delays and reading sensors’ data from I2C.