0

I'm using three NRF24L01 transceivers, each connected to one Arduino Nano. One of the NRF24L01's is configured as a receiver, while the other two NRF24L01's are set as transmitters. Firstly, I'm aware that is not possible for two transmitters to send data at once, but what I want to do, basically is to simultaneously display the transmitters' data in the serial monitor in the same time, and row as each other.

The transmitters collect data from the respective sensors attached to it, such as accelerometer and so forth. Both have the same amount of inputs and sensors.

The receiver is plugged in to the computer awaiting the transmission of data from the transmitters.

Everything is fine in the serial monitor when only one of the transmitters are transmitting, however, when both the transmitters are sending data to the receiver, a garbled mess of data could be seen in the jpg file. here

What I'm trying to visualize in the serial monitor is this: the sensor values of transmitter 1 and transmitter 2, all placed in the same row every second of transmission. That is the format, because I've written a short Python script that takes the serial data and prints it in a csv file, so the format needs to be preserved.

How do I go about it? I've read about the use of arrays and payloads but I'm relatively unfamiliar with those as it is my first time exploring networking peripherals in Arduino. I hope you could help me. Thank you.

Edit: Here is the left transmitter code:

#include <SPI.h> //libraries para sa nrf24l01
#include "RF24.h" //

#include <Wire.h> // MPU-6050 I2C LIBRARY
#include <I2Cdev.h> //
#include <MPU6050.h> //



#define flex_1 A0 //declaring ng variable tsaka pin assignment ng 5 flex sensors
#define flex_2 A1
#define flex_3 A2
#define flex_4 A3
#define flex_5 A6

MPU6050 accelgyro; // variable ng mpu6050

int16_t ax, ay, az; // variables para accelerometer and gyroscope
int16_t gx, gy, gz;

int pinx=2; // pin assignment ng contact sensors
int piny=3;

RF24 myRadio (8, 9); // CSN AT CE PIN ASSIGNMENT
byte addresses[6] = {"1Node"}; // address ng both transmitter and receiver

// structure para masend ng inorder
struct Pack {
  int data11;
  int data22;
  int data33;
  int data44;
  int data55;
  int accxx;
  int accyy;
  int acczz;

} packet1;


void setup() {

  Serial.begin(9600); // para magamit ang serial monitor 
  Serial.println(F("RF24/Simple Transmit data Test"));

  Wire.begin(); // 

  accelgyro.initialize(); // para maread ang pin ng mpu6050

  pinMode(pinx, INPUT);
digitalWrite(pinx, HIGH);
pinMode(piny, INPUT);
digitalWrite(piny, HIGH);
/*
    accelgyro.setXAccelOffset(2825);
    accelgyro.setYAccelOffset(457);
    accelgyro.setZAccelOffset(645);
*/
  myRadio.begin(); // nrf24l01
  myRadio.setChannel(108); 
  myRadio.setPALevel(RF24_PA_MIN);
  myRadio.openWritingPipe(addresses[1]);
}

void loop() {
  packet1.data11 = analogRead(flex_1); 
  packet1.data11=map(packet1.data11, 450, 950, 3000, 4000); 
  packet1.data22 = analogRead(flex_2);
  packet1.data22=map(packet1.data22, 450, 950, 3300, 4500);
  packet1.data33 = analogRead(flex_3);
  packet1.data33=map(packet1.data33, 450, 950, 3000, 4000);
  packet1.data44 = analogRead(flex_4);
  packet1.data44=map(packet1.data44, 450, 950, 3000, 4000);
  packet1.data55 = analogRead(flex_5);
  packet1.data55=map(packet1.data55, 450, 950, 3000, 4000);
  packet1.accxx=ax;
  packet1.accyy=ay;
  packet1.acczz=az;

  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);


  if (myRadio.write(&packet1, sizeof(packet1))) {
    Serial.print(F("flex1 = "));
    Serial.print(packet1.data11);
    Serial.print(F(" flex2 = "));

    Serial.print(packet1.data22);
    Serial.print(F(" flex3 = "));
    Serial.print(packet1.data33);
    Serial.print(F(" flex4 = "));
    Serial.print(packet1.data44);
    Serial.print(F(" flex5 = "));
    Serial.print(packet1.data55);

    Serial.print(F(" accx = "));
    Serial.print(packet1.accxx);
    Serial.print(F(" accy = "));
    Serial.print(packet1.accyy);
    Serial.print(F(" accz = "));
    Serial.print(packet1.acczz);
    Serial.print(F(" con1 = "));

    Serial.println();
  } 

  else {
    Serial.print(F("Send failed."));
  }

}

And here is the right transmitter code:

#include <SPI.h> //libraries para sa nrf24l01
#include "RF24.h" //

#include <Wire.h> // MPU-6050 I2C LIBRARY
#include <I2Cdev.h> //
#include <MPU6050.h> //



#define flex_1 A0 //declaring ng variable tsaka pin assignment ng 5 flex sensors
#define flex_2 A1
#define flex_3 A2
#define flex_4 A3
#define flex_5 A6

MPU6050 accelgyro; // variable ng mpu6050

int16_t ax, ay, az; // variables para accelerometer and gyroscope
int16_t gx, gy, gz;

int pinx=2; // pin assignment ng contact sensors
int piny=3;

RF24 myRadio (8, 9); // CSN AT CE PIN ASSIGNMENT
byte addresses[6] = {"1Node"}; // address ng both transmitter and receiver

// structure para masend ng inorder
struct Pack {
  int data1;
  int data2;
  int data3;
  int data4;
  int data5;
  int accx;
  int accy;
  int accz;
  int con1;
  int con2;
} packet;


void setup() {

  Serial.begin(38400); // para magamit ang serial monitor 
  Serial.println(F("RF24/Simple Transmit data Test"));

  Wire.begin(); // 

  accelgyro.initialize(); // para maread ang pin ng mpu6050

  pinMode(pinx, INPUT);
digitalWrite(pinx, HIGH);
pinMode(piny, INPUT);
digitalWrite(piny, HIGH);
/*
    accelgyro.setXAccelOffset(2825);
    accelgyro.setYAccelOffset(457);
    accelgyro.setZAccelOffset(645);
*/
  myRadio.begin(); // nrf24l01
  myRadio.setChannel(108); 
  myRadio.setPALevel(RF24_PA_MIN);
  myRadio.openWritingPipe(addresses[0]);
}

void loop() {
  packet.data1 = analogRead(flex_1); 
  packet.data1=map(packet.data1, 450, 950, 3000, 4000); 
  packet.data2 = analogRead(flex_2);
  packet.data2=map(packet.data2, 450, 950, 3300, 4500);
  packet.data3 = analogRead(flex_3);
  packet.data3=map(packet.data3, 450, 950, 3000, 4000);
  packet.data4 = analogRead(flex_4);
  packet.data4=map(packet.data4, 450, 950, 3000, 4000);
  packet.data5 = analogRead(flex_5);
  packet.data5=map(packet.data5, 450, 950, 3000, 4000);
  packet.accx=ax;
  packet.accy=ay;
  packet.accz=az;
  packet.con1=digitalRead(pinx);
  packet.con2=digitalRead(piny);

  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);


  if (myRadio.write(&packet, sizeof(packet))) {
    Serial.print(F("flex1 = "));
    Serial.print(packet.data1);
    Serial.print(F(" flex2 = "));

    Serial.print(packet.data2);
    Serial.print(F(" flex3 = "));
    Serial.print(packet.data3);
    Serial.print(F(" flex4 = "));
    Serial.print(packet.data4);
    Serial.print(F(" flex5 = "));
    Serial.print(packet.data5);

    Serial.print(F(" accx = "));
    Serial.print(packet.accx);
    Serial.print(F(" accy = "));
    Serial.print(packet.accy);
    Serial.print(F(" accz = "));
    Serial.print(packet.accz);
    Serial.print(F(" con1 = "));
    Serial.print(packet.con1);
    Serial.print(F(" con2 = "));
    Serial.print(packet.con2);

    Serial.println();
  } 

  else {
    Serial.print(F("Send failed."));
  }

}

And the code for the receiver:

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

RF24 myRadio (8, 9);
byte addresses[][6] = {"1Node"};


int accelgyroscope[3];

int ax, ay, az;
int gx, gy, gz;

int LED_GREEN = A3;
int LED_RED = A2;


struct Pack {
  int data1;
  int data2;
  int data3;
  int data4;
  int data5;
  int accx;
  int accy;
  int accz;
  int con1;
  int con2;

} packet;


void setup() {
  Serial.begin(4800);

  myRadio.begin();
  myRadio.setChannel(108);
  myRadio.setPALevel(RF24_PA_MIN);
  myRadio.openReadingPipe(1, addresses[0]);
  myRadio.startListening();

  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_RED, OUTPUT);

}

void loop() {
  if ( myRadio.available())   {

    analogWrite(LED_GREEN, 1023);
    analogWrite(LED_RED, 0);

    myRadio.read( &packet, sizeof(packet) );
    Serial.print("0");
    Serial.print(", ");
    Serial.print(packet.data1);
    Serial.print(", ");
    Serial.print(packet.data2);
    Serial.print(", ");
    Serial.print(packet.data3);
    Serial.print(", ");
    Serial.print(packet.data4);
    Serial.print(", ");
    Serial.print(packet.data5);

    Serial.print(", ");
    Serial.print(packet.accx);
    Serial.print(", ");
    Serial.print(packet.accy);
    Serial.print(", ");
    Serial.print(packet.accz);
    Serial.print(", ");

    Serial.print(packet.con1);
    Serial.print(", ");
    Serial.print(packet.con2);
    Serial.println();

  }
  else
  {
    analogWrite(LED_RED, 1023);
    analogWrite(LED_GREEN, 0);
  }



}
  • Without seeing your code it's impossible to know what you're doing that makes the output look like that. I think you need to decouple your reception from your printing and update an internal list of values whenever you happen to receive them, and periodically print that list of values as a single unit whenever you deem appropriate. – Majenko Aug 16 at 10:37
  • I already added the codes per your request. What do you mean with "decouple your reception from your printing and update an internal list of values whenever you happen to receive them, and periodically print that list of values as a single unit whenever you deem appropriate."? I'm unfamiliar with those as my Arduino skills are limited. – defineastronaut Aug 16 at 11:04
  • That code doesn't match the output that you are showing in your image. – Majenko Aug 16 at 11:17
  • I'm sorry I just updated it again – defineastronaut Aug 16 at 11:28
  • It seems like you are assuming both transmitters are sending at the same rate, and all packets get received. Why do you want to have the data from the two receivers next to each other? I'd put them all on their own line. It might be useful to add a column for the received timestamp. – Gerben Aug 16 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.