So im trying to send a multiple byte array over the nrf24L01, however i cant seem to figure out how to do it, and i cant seem to find much documentation! ultimately i'd like to be able to send a 16byte array... I'll post the latest code i've been messing around with ... could someone point out what i need to change to be able to send a data array? Thanks.
Hmmm until i find out how to paste the code this will have to do ... its almost the same as the actual examples but with for loops to fill the array...
/**
A Mirf example to test the latency between two Ardunio.
Pins:
Hardware SPI:
MISO -> 12
MOSI -> 11
SCK -> 13
Configurable:
CE -> 8
CSN -> 7
Note: To see best case latency comment out all Serial.println
Yup, that's right. You build your entire packet, and call send() once with the whole thing.
Also, next time please post your code surrounded by 'code' markers. See the '#' button in the toolbar. That and it's helpful to remove the commented-out code.
OK heres a sample for anyone else trying to find an example. This will send a 16byte array to a second unit which will then send it back to the first and then display the 16byte array in the monitor window. Hopefully this will save us beginners a bunch of work :-).
Transmitting unit
/**
* A Mirf example to test the latency between two Ardunio.
*
* Pins:
* Hardware SPI:
* MISO -> 12
* MOSI -> 11
* SCK -> 13
*
* Configurable:
* CE -> 8
* CSN -> 7
*
* Note: To see best case latency comment out all Serial.println
* statements not displaying the result and load
* 'ping_server_interupt' on the server.
*/
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
char data[16];
char rec[16];
byte time[16];
int i, a, f;
void setup(){
Serial.begin(9600);
Serial.println("Enter 16 characters");
Serial.println();
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.setRADDR((byte *)"clie1");
Mirf.payload = 16;
Mirf.config();
Serial.println("Beginning ... ");
}
void loop(){
if (a == 0){
if (Serial.available() > 15) {
a = 1;
for (i = 0; i < 16; i = i + 1) {
data[i] = Serial.read();
}
Serial.println(" ");
Serial.print("you sent ");
for (i = 0; i < 16; i = i + 1) {
Serial.print(data[i]);
}
Serial.print(" DEC ");
for (i = 0; i < 16; i = i + 1) {
Serial.print(data[i], DEC);
}
Serial.print(" Hex ");
for (i = 0; i < 16; i = i + 1) {
Serial.print(data[i], HEX);
}
Serial.println(" ");
}
}
if (a == 1){
Mirf.setTADDR((byte *)"serv1");
data[Mirf.payload];
Mirf.send((byte *) &data);
while(Mirf.isSending()){
}
Serial.println(" ");
delay(10);
while(!Mirf.dataReady()){
}
Serial.print("You Got ");
Mirf.getData((byte *) &rec);
for (i = 0; i < 16; i = i + 1) {
Serial.print(rec[i]);
}
Serial.print(" DEC ");
for (i = 0; i < 16; i = i + 1) {
Serial.print(rec[i], DEC);
}
Serial.print(" Hex ");
for (i = 0; i < 16; i = i + 1) {
Serial.print(rec[i], HEX);
}
Serial.println(" ");
a = 0;
}
}
responding unit
/**
* An Mirf example which copies back the data it recives.
*
* Pins:
* Hardware SPI:
* MISO -> 12
* MOSI -> 11
* SCK -> 13
*
* Configurable:
* CE -> 8
* CSN -> 7
*
*/
#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>
boolean b = 0;
byte thisChar[16];
int i;
int data[16];
void setup(){
Serial.begin(9600);
pinMode(4, OUTPUT);
/*
* Set the SPI Driver.
*/
Mirf.spi = &MirfHardwareSpi;
/*
* Setup pins / SPI.
*/
Mirf.init();
/*
* Configure reciving address.
*/
Mirf.setRADDR((byte *)"serv1");
/*
* Set the payload length to sizeof(unsigned long) the
* return type of millis().
*
* NB: payload on client and server must be the same.
*/
Mirf.payload = 16;
char data[Mirf.payload];
/*
* Write channel and payload config then power up reciver.
*/
Mirf.config();
Serial.println("Listening...");
}
void loop(){
/*
* A buffer to store the data.
*/
data[Mirf.payload];
/*
* If a packet has been recived.
*
* isSending also restores listening mode when it
* transitions from true to false.
*/
if(!Mirf.isSending() && Mirf.dataReady()){
Serial.println("Got packet");
/*
* Get load the packet into the buffer.
*/
Mirf.getData((byte *) data);
for (i = 0; i < 16; i = i + 1) {
Serial.print(data[i]);
}
/*
* Set the send address.
*/
Mirf.setTADDR((byte *)"clie1");
/*
* Send the data back to the client.
*/
Mirf.send((byte *) data);
/*
* Wait untill sending has finished
*
* NB: isSending returns the chip to receving after returning true.
*/
b = !b;
digitalWrite(4, b);
Serial.println("Reply sent.");
}
}
These examples are just modified from the included examples.