Could I have made this code a bit better?

Hi,
I have been experimenting with the nrf24l01+ RF transceivers and the Mirf library, trying to get these two things to work so one arduino can send a string to another. At first I tried making the client arduino sending the whole string to the server arduino, which reads it, punches it into the serial monitor, and sends the string back as a confirmation to the client that the data was successfully sent. When I tried this, the only data it was printing in was something like fjhfvghjfghj except with a bunch of apostrophes (the client printed this in while the server printed in nothing, I had both of them print in the data so i can see they’re getting the same thing from each other). So I then tried making it send one part of the string at a time, and this worked, except sometimes the data wouldn’t be printed out or the data printed in the server’s serial monitor was either slightly or completely different from what the client was printing in. What could I do to fix this? I’ve also noticed that the values before the client sends differ from the ones it receives from the server. I appreciate any help given!

Sketches.zip (1.08 KB)

Server:

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

void setup() {
  Serial.begin(9600);
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"serv1");
  Mirf.payload = sizeof(float);
  Mirf.config();
  Serial.println("Awaiting data...");
}

void loop() {
  float data[] = {0, 0};
  if (!Mirf.isSending() && Mirf.dataReady()) {
    Mirf.getData((byte *) &data[0]);
    delay(10);
  }
  if (Mirf.dataReady()) {
    Mirf.getData((byte *) &data[1]);
    Serial.println("Received data");     
    Mirf.setTADDR((byte *)"clie1");
    Mirf.send((byte *) &data[0]);
    while (Mirf.isSending()) {}
    Mirf.send((byte *) &data[1]);
    while (Mirf.isSending()) {}
    Serial.println(data[0]);
    Serial.println(data[1]);
    Serial.println("Sent conformation response to client");
  }
}

Client

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

void setup() {
  Serial.begin(9600);
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"clie1");
  Mirf.payload = sizeof(float);
  Mirf.config();
  Serial.println("Please wait...");
}

void loop() {
  long time = millis();
  float data[] = {analogRead(A0), analogRead(A1)};
  Mirf.setTADDR((byte *)"serv1");
  Mirf.send((byte *)&data[0]);
  while(Mirf.isSending()){
  }
  Mirf.send((byte *)&data[1]);
  while(Mirf.isSending()){
  }
  Serial.println("Done sending, awaiting response...");
  delay(10);
  while(!Mirf.dataReady()){
    if ( ( millis() - time ) > 1000 ) {
      Serial.println("Timeout on response from server!");
      return;
    }
  }
  
  Mirf.getData((byte *) &data[0]);
  delay(10);
  if (Mirf.dataReady()) {
    Mirf.getData((byte *) &data[1]);
    Serial.println("Data successfully received by server");
    Serial.println(data[0]);
    Serial.println(data[1]);
    delay(1000);
  }
}
float data[] = {0, 0};

//and

float data[] = {analogRead(A0), analogRead(A1)}; // why float, analogRead is a 10 bit integer

yet...

void send(byte *data) //from the mirf library

//still no?

void send(byte *data)

do we see the problem yet?

Absolutely. You should rename one of them.

float Data[] = {0, 0};

//and

float Lore[] = {analogRead(A0), analogRead(A1)};

I’m going to be honest with you Nick…I have no idea if that is a joke or not…

In case it isn’t, you are sending a pointer to the location of a float, not a byte. Also, you will need to send 2 bytes because the ADC returns a 10 bit result which is more than a byte.

float Lore[] = {analogRead(A0), analogRead(A1)};

I don't think that this is a valid array element initialisation, the initializers have to be constants or values at known locations.

That part in fact is OK. You are initializing the array with a value known at the point of initialization.

It would be better if it was int rather than float.

I'm going to be honest with you Nick...I have no idea if that is a joke or not...

Ah yes, that was a joke. There were 2 x Data there so I made a Star Trek reference.

ah...its an episode...