Arduino Receiving Radio Transmitted Data Scrambled

I am working on a project where I need an IMU temperature sensor logging data at different locations and I need to be able to have the live data displayed on an OLED display. The receiver has a "pause switch" that can be flicked at any moment pausing the relay of data from the transmitter. The transmitted also has an RGB led that gives visual feedback that the data is being transmitted.

Both devices boot and connect fine, the problem appears when the data starts being relayed. The transmitter sends out uniform, correctly ordered data, while the receiver is receiving an extra packet with the ("data") IMU temperature SEPARATE from ("data2") SAT the satellite number, ("data3") the Speed, and ("data4") the Altitude. The data also comes in "moved over" as "data2" becomes "data", "data3" becomes "data2", and so on.

Here is the TRANSMITTER CODE

#include "Arduino.h"
#include <SPI.h>
#include <RF24.h>
#include <basicMPU6050.h> 

#include <SoftwareSerial.h>

#include "TinyGPS++.h"
#include "SoftwareSerial.h"


SoftwareSerial serial_connection(9, 10);   //RX=pin 9, TX=pin 10
TinyGPSPlus gps;


basicMPU6050<> imu;


RF24 radio(7, 8);



byte addresses[][6] = {"1Node", "2Node"};

int redPin= 2;
int greenPin = 3;
int bluePin = 4;



void setup() {

  imu.setup();
  imu.setBias();
  
  Serial.begin(9600);

  serial_connection.begin(9600);
  
  Serial.println("THIS IS THE TRANSMITTER CODE - YOU NEED THE OTHER ARDIUNO TO SEND BACK A RESPONSE");

  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  
  radio.begin();

  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(124);
  radio.openWritingPipe(addresses[1]);
  radio.openReadingPipe(1, addresses[0]);

  radio.stopListening();
  
}


void loop() {
  

//  imu.updateBias();

//  analogWrite(redPin,0);
//  analogWrite(greenPin,0);
  analogWrite(bluePin,255);

  
  float data = (imu.temp());                         //Defining temperature as float 
  float data2 = (gps.satellites.value());      
  float data3 = (gps.speed.mph());            
  float data4 = (gps.altitude.feet());          
    

  while(serial_connection.available())                //awaitig a serial connection
  {
    gps.encode(serial_connection.read());
  }
  
 if(gps.location.isUpdated()) {                             //awaiting new GPS data

  if (radio.write( &data, sizeof(data))) {    //Sending out the data and printing it to the serial monitor
    Serial.print(data);
    analogWrite(redPin,255);
//    analogWrite(greenPin,0);
//    analogWrite(bluePin,0);  
  }

  
  if (radio.write( &data2, sizeof(data2))) {          
    Serial.print("  ");
    Serial.print(data2);   
    analogWrite(redPin,255);
//    analogWrite(greenPin,0);
//    analogWrite(bluePin,0); 
  }


  if (radio.write( &data3, sizeof(data3))) {  
    Serial.print("  ");
    Serial.print(data3); 
    analogWrite(redPin,255);
//    analogWrite(greenPin,0);
//    analogWrite(bluePin,0); 
  
 }


  if (radio.write( &data4, sizeof(data4))) {   
    Serial.print("  ");
    Serial.println(data4);
    analogWrite(redPin,255);
//    analogWrite(greenPin,0);
//    analogWrite(bluePin,0); 
  
 }

  
  
  
  delay(50);
}
}

Here is the RECEIVER CODE

#include "Arduino.h"
#include <SPI.h>
#include <RF24.h>

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH1106.h>

#define OLED_RESET 4
Adafruit_SH1106 display(OLED_RESET);

#define LOGO16_GLCD_HEIGHT 16 
#define LOGO16_GLCD_WIDTH  16 

#if (SH1106_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SH1106.h!");
#endif

RF24 radio(7, 8);


byte addresses[][6] = {"1Node","2Node"};

int ledPin = 2;
int switchPin = 3;

  float data;
  float data2;
  float data3;
  float data4;
  
void setup() {
  Serial.begin(9600);
  Serial.println("THIS IS THE RECEIVER CODE - YOU NEED THE OTHER ARDUINO TO TRANSMIT");

  pinMode(ledPin, OUTPUT);
  pinMode(switchPin, INPUT);

  display.begin(SH1106_SWITCHCAPVCC, 0x3C);

  display.display();
  
  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(30,0); 
  display.println("Avionics V1");
  display.setCursor(25,30);
  display.print("CONNECTING...");
  display.display();

  

  radio.begin();
  
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(124);
  radio.openWritingPipe(addresses[0]);
  radio.openReadingPipe(1, addresses[1]);

  radio.startListening();

  display.clearDisplay();


}

void loop() {

  float mode = (digitalRead(switchPin));

 if(digitalRead(switchPin) == HIGH){               //physical switch on the device to pause relay

  digitalWrite(ledPin, HIGH);

  
  if (radio.available()) {

    display.setCursor(0,0);
    display.print("Connection: GOOD");
    display.clearDisplay();
    digitalWrite(ledPin, HIGH);



    while (radio.available()) {                                 //while receiving packets 
      
      display.clearDisplay();
       
      radio.read(&data, sizeof(data));                      //read packets
      radio.read(&data2, sizeof(data2));
      radio.read(&data3, sizeof(data3));
      radio.read(&data4, sizeof(data4));

      display.setCursor(0,0);                                    //display packets (should be in this order)
      display.print("Temp: ");
      display.println(data);
      
      display.setCursor(0,8);
      display.print("SAT: ");
      display.println(data2);
      
      display.setCursor(0,16);
      display.print("Speed: ");
      display.println(data3);
      
      display.setCursor(0,24);
      display.print("Altitude: ");
      display.println(data4);

      display.display();
     
      
      Serial.print(data);                                   //printing received data to serial monitor
      Serial.print("  ");
      Serial.print(data2);
      Serial.print("  ");
      Serial.print(data3);
      Serial.print("  ");
      Serial.println(data4);
    
     }
      
    }

   
 }

 else {
  display.clearDisplay();
  digitalWrite(ledPin, LOW);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0,0); 
  display.print("RELAY PAUSED"); 
  display.display();
  delay(500);
 }

 
}

So sorry if I used the incorrect way to paste code, I just joined today, but I am hoping someone could help me with this. All help will be greatly appreciated!

Your posting of code is fine!
Are you aware that as soon as you open the serial monitor on your PC, most Arduino boards will be RESET, so your receive code starts over?
That may be causing some your problems.

Thank you for the reply!
That is a good point, although I just tried running it without opening the serial monitor and getting rid of the serial connection altogether, but the problem remained.

I tried running code that "sorts" the data to correct places on the display, but when I came back to it the next day, other pieces of data were moved around.

MY first step in debugging would be to Serial.print the messages in HEX so the actual data can be seen. Perhaps that will give a clue as to the source of the problem. Then, do the same for the transmitting device and see if the HEX data is correct at that point.

1 Like

The basic problem is that you are sending the data as individual measurements, without any obvious means for the receiver to tell which is which.

It would be much better to send a single array in one transmission, then the order is obvious. Better to send integers, too, but that can be fixed later.

in outline:

float data[4];
data[0]=imu.temp();
data[1]= gps.satellites.value();
 ...
  if (radio.write( data, sizeof(data))) {  // "&" not needed, data is already a pointer

The same problem occurs with serial data transfer in many situations, so take a look at the forum tutorial "Serial Input Basics". It explains how to solve the order problem.

1 Like

Yep! It was receiving it in a wonky order; I reorganized them as an array and sent a single int packet. Thank you so much for this guys, ya'll are awesome!!

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