I've been experimenting for a while with @Robin2 's examples, and I've run into what should be the last problem with radio communication; I'm trying to adapt your codebase to my needs, and this is what I've made so far:
I've made 2 main changes: type of the arrays which carry data has been changed from char to uint8_t,
but I've tested this with the code you see below, it is just your examples slightly modified
This is the modified code:
RX:
// SimpleRx - the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN);
uint8_t dataReceived[10]; // this must match dataToSend in the TX
bool newData = false;
//===========
void setup() {
Serial.begin(9600);
Serial.println("SimpleRx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
}
//=============
void loop() {
getData();
showData();
}
//==============
void getData() {
if ( radio.available() ) {
Serial.println(sizeof(dataReceived));
radio.read( &dataReceived, sizeof(dataReceived));
newData = true;
}
}
void showData() {
if (newData == true) {
Serial.print("Data received ");
for(int i = 0; i < 10; i++) {
Serial.print(dataReceived[i]);
Serial.print(" ");
}
Serial.println(" ");
newData = false;
}
}
TX
// SimpleTx - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
const byte slaveAddress[5] = {'R','x','A','A','A'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
uint8_t dataToSend[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
char txNum = '0';
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
void setup() {
Serial.begin(9600);
Serial.println("SimpleTx Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.setRetries(3,5); // delay, count
radio.openWritingPipe(slaveAddress);
}
//====================
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
prevMillis = millis();
}
}
//====================
void send() {
bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
// Always use sizeof() as it gives the size as the number of bytes.
// For example if dataToSend was an int sizeof() would correctly return 2
Serial.print("Data Sent ");
if (rslt) {
Serial.println(" Acknowledge received");
}
else {
Serial.println(" Tx failed");
}
}
//================
void updateMessage() {
// so you can see that new data is being sent
txNum += 1;
if (txNum > '9') {
txNum = '0';
}
dataToSend[8] = txNum;
}
RX output:
SimpleRx Starting
10
Data received 1 2 3 4 5 6 7 8 9 10
The other change iI've made is in the .write and .read function I use ARRAY_SIZE instead of sizeof, because it would give out warnings, ARRAY_SIZE #defined as 10, and should function the same way.
warning message:
sketch/transmission.cpp: In function 'void transmit_data(uint8_t*)':
sketch/transmission.cpp:6:53: warning: 'sizeof' on array function parameter 'stuffToSend' will return size of 'uint8_t* {aka unsigned char*}' [-Wsizeof-array-argument]
if (radio.write(&stuffToSend, sizeof(stuffToSend))) // ARRAY_SIZE
^
sketch/transmission.cpp:4:40: note: declared here
void transmit_data(uint8_t stuffToSend[])
sandbox.ino
#include "transmission.h"
#define TX 1
#define RX 2
#define STATE RX
#define CE_PIN 9
#define CSN_PIN 10
#define ARRAY_SIZE 10
RF24 radio(CE_PIN, CSN_PIN);
const byte thisSlaveAddress[5] = {'R','x','A','A','A'};
#if STATE == TX
uint8_t stuffToSend[ARRAY_SIZE] = {};
#elif STATE == RX
uint8_t dataReceived[ARRAY_SIZE] = {};
#endif
void setup()
{
Serial.begin(9600);
radio.begin();
radio.setDataRate( RF24_250KBPS );
#if STATE == TX
radio.setRetries(3,5);
radio.openWritingPipe(thisSlaveAddress);
#elif STATE == RX
radio.openReadingPipe(1, thisSlaveAddress);
radio.startListening();
#endif
}
void loop()
{
#if STATE == TX
Serial.println("TX");
transmit_data(stuffToSend);
#elif STATE == RX
receive_data(dataReceived);
#endif
delay(1000);
}
transmission.h
#pragma once
#define ARRAY_SIZE 10
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
extern RF24 radio;
extern const byte thisSlaveAddress[5];
void transmit_data(uint8_t stuffToSend[]);
void receive_data(uint8_t dataReceived[]);
void print_array(const uint8_t array[]);
transmission.cpp
#include "transmission.h"
void print_array(const uint8_t array[])
{
for (uint8_t i = 0; i < ARRAY_SIZE; i++) {
Serial.print(array[i]);
Serial.print(" ");
}
Serial.println(" ");
}
void transmit_data(uint8_t stuffToSend[])
{
if (radio.write(&stuffToSend, ARRAY_SIZE))
{
print_array(stuffToSend);
Serial.println("Success");
} else {
Serial.println("Fail");
}
}
void receive_data(uint8_t dataReceived[])
{
if ( radio.available())
{
Serial.println("Success");
radio.read(&dataReceived, ARRAY_SIZE);
print_array(dataReceived);
} else {
Serial.println("Fail");
}
}
this is the output from the RX
Fail
Fail
Success
0 0 0 0 0 0 0 0 9 1
TX output
TX
0 0 0 0 0 0 0 0 0 0
Success
If I try initializing the array:
uint8_t stuffToSend[ARRAY_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
RX output
Fail
Fail
Success
1 2 3 4 65 0 0 0 0 79
TX output
TX
1 2 3 4 5 6 7 8 9 10
Success
The code is split in two using preprocessor directives: if the state is TX, everything about the transmitter code is visible, while the receiver code is invisible.
Everything in the setup() is copied and pasted from @Robin2 's examples.
In the loop I call either transmit_data() or receive_data() depending on STATE.
transmit_data doesn't seem to be the problem.
I think that the problem may be with either the arrays being passed by reference (even though I don't think it makes a difference) instead of being global, or their typing.
I've been thinking about this for a few days but I really can't get around this, I hope someone can help, thanks for your time