Why my reciver does nothing until recived data are printed?

Hello,

I have a train, controled by reciever consit of:

Arduino Uno Arduino UNO R3, ATmega328P, Klon | laskarduino.cz

H-bridge (whitch control train motor) Double BTS7960B DC 43A Stepper Motor Driver H-Bridge PWM For Arduino Smart Car | eBay

and LoRa https://www.amazon.com/SX1278-Ai-Thinker-Wireless-Spectrum-Transmission/dp/B07KNTKXBL

The sender has 3 buttons (meaning: forward, backward and stop) and one potentiometer for controling the speed of train.

The sender sents to reciever message cosist of status of buttons separated with comma (forward,backward,stop,speed). For example (1,0,0,155)

The sender consist of:

Arduino UNO (same as reciever)

LoRa (same like reciever)

3 buttons

1 potentiometr

Reciever has to split the message and (only for this example) print status of sender buttons.

Everything works perfect, but if I don’t print str (Serial.println(str) ) nothing happens on reciver. WHY? I don’t want to print anything.

I am only “hobby” worker with Arduino and I “glue” my code from some parts which I found.

Reciever code:

#include <SPI.h>
#include <LoRa.h>
String inStringall="";
int speedy=0;
int forward=0;
int backward=0;
int st=0; //stop

void setup() {
  Serial.begin(9600);
  
  while (!Serial);
  Serial.println("LoRa Receiver");
  if (!LoRa.begin(433E6)) { // or 915E6
    Serial.println("Starting LoRa failed!");
    while (1);
  }
    
}

void loop() {

  int packetSize = LoRa.parsePacket();
  if (packetSize) { 
    // read packet    
    while (LoRa.available())
    {
      inStringall = inStringall+(char)LoRa.read();
      
      }
    }
    const char * str = inStringall.c_str();
    Serial.println(str);
    if (sscanf(str, "%d,%d,%d,%d", &forward, &backward, &st, &speedy) == 4){
       Serial.print("forward=");Serial.println(forward);
       Serial.print("backward=");Serial.println(backward);
       Serial.print("stop=");Serial.println(st);
       Serial.print("speedy=");Serial.println(speedy);  
    inStringall = "";
    LoRa.packetRssi();    
  }
}

Sender code:

#include <SPI.h>
#include <LoRa.h>

int speedy=0;
int potentiometr = A0;
int button1=5;
int button2=7;
int button3=6;
int forward=0;
int backward=0;
int st=0; // stop

String stringall;
String comma=",";

void setup() {
  pinMode(button1,INPUT);
  pinMode(button2,INPUT);
  pinMode(button3,INPUT);
  
  Serial.begin(9600);
 
  while (!Serial);
  Serial.println("LoRa Sender");
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  speedy = (analogRead(potentiometr)/4);
  forward = digitalRead(button1);
  backward = digitalRead(button2);
  st = digitalRead(button3);
     
  stringall=forward+comma+backward+comma+st+comma+speedy;
  //Serial.println(stringall);
  LoRa.beginPacket();
  LoRa.print(stringall);
  LoRa.endPacket();
  delay(50);
}

Hope all neccesary information are here, if no, please let me know.

Hello
check this line of code

 if (sscanf(str, "%d,%d,%d,%d", &forward, &backward, &st, &speedy) == 4)

Which variabe should have a value equal to 4?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.