I have this code working on two Arduino Nanos with HC-12 wireless modules. Everything works great as long as I power the main controller first, never get out of range, and never have a corrupted byte. Once any of these issues arise I start having intermittent issues or complete failure. Any recommendations on how to sync the serial data back up is greatly appreciated.
Code for the remote control:
int pushButton = 2;// Set PushButton to DI Pin 2
int HB = 1; //intialize the heartbeat to 1 on startup
int HBSTEP = 1;//Step to increment the heartbeat by
byte SendArray[5] = {0,0,0,0,0};//Array to print to the serial port ([0]=HB, [1]=XIN, [2]=YIN, [3]=PB, [4]=Checksum)
void setup() {
// Start serial communication at 1200 buad more stable for wireless
Serial.begin(1200);
// Assign pushbutton as an input
pinMode(pushButton, INPUT);
}
void loop() {
// Read the X value at A0
int XIN = analogRead(A0);
// Read the Y value at A1
int YIN = analogRead(A1);
// Read the pushbutton at digtal pin 2
int PB = digitalRead(pushButton);
//Assign variables to an array
SendArray[0] = HB;
SendArray[1] = map(XIN,0,1024,0,255);//map the AI (0-1024) to the byte size (0-255)
SendArray[2] = map(YIN,0,1024,0,255);//map the AI (0-1024) to the byte size (0-255)
SendArray[3] = PB;//Only using one bit in this byte but leaving room for more push buttons
//Calculate the Checksum
//Find the sum of the product of each array multiplied by its position int he array
int SumP = SendArray[0]*5 + SendArray[1]*4 + SendArray[2]*3 + SendArray[3]*2;
//Checksum is the remainder of the sum of the products divided by the number of positions + 1
SendArray[4] = SumP % 6;
//Print HB, X, Y, PB and checksum to serail
Serial.write(SendArray, 5);
//Increment the heatbeat by step amount unless it is at 255, then set it to 0
if (HB < 255)
{
HB = HB + HBSTEP;
}
else
{
HB = 0;
}
//Delay 1 second before going to begining of loop
delay(100);
}
Code for the Main Controller / Receiver:
int HB; //Heartbeat from remote
int HBLAST;//Heartbeat from last scan
int FROZEN = 6;// Heatbeat frozen count initialize greater than frozen counter to ensure motor does not turn or go into gear until remote signal is being received
int XVAL; //X value from joystick
int YVAL; //Y value from joystick
int PB;// Push Button State
int CS;// Checksum to verify data was received coreectly
int led = 13;// Test LED on when checksum is verifed
byte ReceiveArray[5] = {0,128,128,1,0}; // Serial data array initialize in the center positon for throttle/transmission and stearing.
void setup() {
// Start serial communication at 1200 buad more stable for wireless
Serial.begin(1200);
//Assign led pin as OUTPUT
pinMode(led, OUTPUT);
}
void loop() {
// Get serial data for HB, X, Y, PB and checksum from Serial
if (Serial.available() >0)
{
Serial.readBytes(ReceiveArray, 5);
}
// Calculate the Checksum
//Find the sum of the product of each array multiplied by its position int he array
int SumP = ReceiveArray[0]*5 + ReceiveArray[1]*4 + ReceiveArray[2]*3 + ReceiveArray[3]*2;
//Checksum is the remainder of the sum of the products divided by the number of positions + 1
CS = SumP % 6;
//If serial no new serial data has been received for more than 5 cycles then the throttle/transmission and stearing should return to center position.
if (FROZEN >5)
{
HB = ReceiveArray[0];
XVAL = 128;
YVAL = 128;
PB = 1;
}
// If not frozen and checksum is verified Split serail data for HB, X, Y, and PB from Serail array
else if (CS == ReceiveArray[4])
{
HB = ReceiveArray[0];
XVAL = ReceiveArray[1];
YVAL = ReceiveArray[2];
PB = ReceiveArray[3];
}
else
{
Serial.flush();
}
//Print the values to the serial port
// Serial.print("RAW = ");
// Serial.println(ReceiveArray);
// Serial.print("HB = " );
// Serial.println(HB);
// Serial.print("X = ");
// Serial.println(XVAL);
// Serial.print("Y = ");
// Serial.println(YVAL);
// Serial.print("PB = ");
// Serial.println(PB);
// Serial.print("Frozen = ");
// Serial.println(FROZEN);
// Serial.println("----------------------");
//Check to see if the heatbeat value is changing
if (HBLAST == HB)
{
FROZEN = FROZEN +1;
}
else
{
FROZEN = 0;
}
// If the checksum is verfied as accurate and receiving continous data then turn the onboard LED on. If not turn the LED off
if ((CS == ReceiveArray[4]) and FROZEN < 6 )
{
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
//Set the last heartbeat value to the new heartbeat value for the next scan
HBLAST = HB;
delay(100);
}