HC-12 Communication Unconsistant

Hi,
I’m Trying to get 2 HC-12 working together for sending datas from a controller board to an other board (Unilateral communication) but I Get the whole data flow at once when I unplug the controller board whereas I should get them every 20 millis (I even tried up to 100). So it works But it looks that I’m sending only one very long String.

Here is the transmitter code :

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); //RX, TX =pinrafio

#define alimPot 13
#define Xpot A1
#define Ypot A0
#define throttle A2
#define PinSecu 8
String message;

void setup() {
  pinMode (alimPot, OUTPUT);
  pinMode (Xpot, INPUT);
  pinMode (Ypot, INPUT);
  pinMode (throttle, INPUT);
  pinMode (PinSecu, INPUT);
  Serial.begin (9600);
  mySerial.begin(9600);
}

void loop() {
  int securiteSwitch;
  int lateral;
  int avance;
  int altitude;
  digitalWrite (alimPot, HIGH);


  //Reception des donnees
  Serial.print ("Securite : \n");
  if (digitalRead (PinSecu) == HIGH){
    securiteSwitch = 0;
  }
  else {
    securiteSwitch = 1;
  }
  //Serial.println (securiteSwitch);

  
  //Serial.print ("Lateral : \n");
  lateral = analogRead (Xpot) + 17;
  //Serial.println (lateral);
  lateral = map(lateral, 16, 1035, 10, 30); // on met le message dans le bon interval
  
  //Serial.print ("avance : \n");
  avance = analogRead (Ypot);
  //Serial.println (avance);
  avance = map(avance, 0, 1023, 40, 60); // on met le message dans le bon interval
  
  
  //Serial.print ("altitude : \n");
  altitude = analogRead (throttle);
  //Serial.println (altitude);  
  altitude = map(altitude, 0, 1023, 100, 980); // on met le message dans le bon interval

  message = String(securiteSwitch) + String(lateral) + String(avance) + String(altitude);
  mySerial.println (message);
    mySerial.flush();
  Serial.println (message);
  delay(20);
}

And the receiver code :

#include <SoftwareSerial.h>

SoftwareSerial mySerial(11, 4); //RX, TX

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
}

void loop() {
 
  if(mySerial.available() > 1){
    String input = mySerial.readString();
    Serial.println(input);    
  }

}

Thanks for your help

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

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

...R

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

Thanks Robin but it looks like now I got no answer anymore, it might be that I poorly understood the method, here is what I wrote for the receiver :

const byte numChars = 10;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

#include <SoftwareSerial.h>

SoftwareSerial mySerial(11, 4); //RX, TX

void setup() {
    Serial.begin(9600);
    mySerial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithEndMarker();
    showNewData();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
    
    while (mySerial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}

Is using the String Class that bad ? It’s seems way easier to use than an array of char :confused:

Thanks

demokrato:
Thanks Robin but it looks like now I got no answer anymore,

Probably not surprising - look at the second line, an easy mistake to make.

   while (mySerial.available() > 0 && newData == false) {
        rc = Serial.read();

Yes. The String class is that bad.

...R

Robin2:
Probably not surprising - look at the second line, an easy mistake to make.

... Shame on me ::slight_smile:

Thanks a lot anyway, I got a smooth communication now