Go Down

Topic: Implementing multiple int's over nRF24L01 via Mirf Library (Read 974 times) previous topic - next topic

spikep

Hello everyone. I'm still rather new, but trying hard to get to grips with the arduino.

I have got a basic line of code that works for sending single bytes of data. This is fine if i want to send 1 reading from 1 pin, but my project requires i send data from 4 analog pins, and be able to print them to a LCD seperately on the other side and named. Also the readings i get are very random, yet i know there coming from the other Arduino. Here is the test code i have for just the transmitting and receiving data:

Transmitting:

Code: [Select]
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

int a;

void setup(){
  Serial.begin(9600);
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"clie1");
  Mirf.payload = sizeof(unsigned long);
  Mirf.config();
  Serial.println("Starting to Transmit");
}

void loop(){
a = analogRead(A0);
Mirf.setTADDR((byte *)"serv1");
    Mirf.send((byte *) a);
    while(Mirf.isSending()){
}
    delay(100);
  }
 


Receiving:

Code: [Select]
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

int a;

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

void loop(){
  while(!Mirf.dataReady()){
  }
  Mirf.getData((byte *) &a);
  Serial.println(a);
  delay(1000);

}


So to recap, i'm struggling on how to get the right information on one side (0-1023 for analog pins) to the other side, and also how to send multiple inputs. I have scoured the internet for the past 5 days with different libraries, examples, and websites. Any help would be appreciated. I am sticking to the Mirf library currently.

Paul

PeterH

The value of payload needs to be the size of the data you're transmitting. In your case you're setting the payload to sizeof(unsigned long) i.e. 4, but the variable you're sending is an int i.e. two bytes. This means the other two byes will contain garbage and will be stored in space which is not allocated to a variable. It's not surprising that doesn't work properly.

On the sending side you need to:
Define a variable that will hold the data to be transmitted. The type will be determined by what data you want to send.
Set payload to the size of that variable.
Call send with the address of that variable.

On the receiving side you need to:
Define a variable which will hold the received data (which must be the same type that the sender is using).
Set the payload to the size of that variable.
When dataReady() returns true, call getData with the address of that variable.

The most obvious way to represent a set of analog values is as an array of ints.

Go Up