Multiple channels for RC plane using HC-12 module

Hi,

I have the following code for transmitter and receiver respectively which is modified from this link here. It works perfectly well by sending a byte, which can then be read by the second HC12.

For my 5 channel RC plane, in the end I want to send across four bytes (each from 0 - 180 for PWM for servos and an esc) and a bool (for on/off switch).

How would I modify the HC-12.print() parts and all the receiver code to work for MULTIPLE channels instead of just one.

I’m still new to Arduinos and coding in general, and have little understanding of the complex data parsing or communication protocols, despite trying to learn. If someone could tell me how to modify this code to send across the 4 PWM signals and a bool variable, I’d be very grateful.

Thank you very much.

Code:

#include <SoftwareSerial.h>
                
SoftwareSerial HC12(10, 11); 
int val0;
int previousVal0;
int HC12power = 8;
const long interval = 1000; // display time 1 in milliseconds
unsigned long previousMillis = 0; // timer

void setup()
{
  Serial.begin(9600);     // Open serial port to computer
  HC12.begin(9600);       // Open serial port to HC12

  pinMode(HC12power, OUTPUT);
}

void loop(){
  unsigned long currentMillis = millis();

 
  val0 = analogRead(A0);
  
  val0 = map(val0, 0, 1023, 0, 180);
  
  if (val0 > (previousVal0 + 2) || val0 < (previousVal0 - 2)) {
    previousVal0 = val0;
    digitalWrite(HC12power, HIGH);
    delay(1);
    HC12.print("<A,");
    HC12.print(val0);
    HC12.print('>');
    Serial.println(val0);
  } else {
    
    if ((currentMillis - previousMillis) >= interval) {
      previousMillis = currentMillis;
      digitalWrite(HC12power, LOW);
    }
  }
}
#include <SoftwareSerial.h>

SoftwareSerial HC12(10, 11); // Create Software Serial Port
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];        
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.0;

boolean newData = false;



void setup(){
  Serial.begin(9600);
  HC12.begin(9600);       

}

void loop() {
  recvWithStartEndMarkers();
  if (newData == true) {
    strcpy(tempChars, receivedChars);
    parseData();
    Serial.println(integerFromPC);
    newData = false;
  }
}

//============

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

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

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

    else if (rc == startMarker) {
      recvInProgress = true;
    }
  }
}



void parseData() {      

  char * strtokIndx;

  strtokIndx = strtok(tempChars, ",");   
  strcpy(messageFromPC, strtokIndx);

  strtokIndx = strtok(NULL, ","); 
  integerFromPC = atoi(strtokIndx);   
 
}

The information of this post may be useful for you.

You can extend this

    HC12.print("<A,");
    HC12.print(val0);
    HC12.print('>');

to include as many pieces of data as you wish - for example

    HC12.print('<');
    HC12.print(val0);
    HC12,print(',');  // comma
    HC12.print(val1);
    HC12,print(','); 
    HC12.print(val2);
    HC12.print('>');

Always send the items in the same order and their position will identify them.

Then see the parse example in [Serial Input Basics[/url

…R
](http://forum.arduino.cc/index.php?topic=396450.0)