I am trying to use RF24 Modules to get data from a master Arduino nano to a slave Arduino mega and display some data on an OLED Display Module SSD1331 then send data back to the master.
I am using a slightly modified code of ackPayload example with LEDs to tell me when the master is sending and when the slave is receiving, but when I try adding the display to the slave code when I initiate the display, the RF24 only receives about 80% of the packets that the master sends. But if I comment out all of the display code, it receives all packets reliably.
The display is on pins
#define sclk 45
#define mosi 43
#define cs 37
#define rst 41
#define dc 39
and the rf24 module is on the hardware SPI pins.
here is my code for the master
// SimpleTxAckPayload - 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
char dataToSend[10] = "Message 0";
char txNum = '0';
int ackData[2] = { -1, -1}; // to hold the two values coming from the slave
bool newData = false;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second
//===============
const int led = 3;
void setup() {
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial.begin(9600);
Serial.println(F("Source File /mnt/sdb1/SGT-Prog/Arduino/ForumDemos/nRF24Tutorial/SimpleTxAckPayload.ino"));
Serial.println("SimpleTxAckPayload Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.enableAckPayload();
radio.setRetries(5, 5); // delay, count
// 5 gives a 1500 µsec delay which is needed for a 32 byte ackPayload
radio.openWritingPipe(slaveAddress);
}
//=============
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
digitalWrite(led, HIGH);
send();
digitalWrite(led, LOW);
}
showData();
}
//================
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 ");
Serial.print(dataToSend);
if (rslt) {
if ( radio.isAckPayloadAvailable() ) {
radio.read(&ackData, sizeof(ackData));
newData = true;
}
else {
Serial.println(" Acknowledge but no data ");
}
}
else {
Serial.println(" Tx failed");
}
updateMessage();
prevMillis = millis();
}
//=================
void showData() {
if (newData == true) {
Serial.print(" Acknowledge data ");
Serial.print(ackData[0]);
Serial.print(", ");
Serial.println(ackData[1]);
Serial.println();
newData = false;
}
}
//================
void updateMessage() {
// so you can see that new data is being sent
txNum += 1;
if (txNum > '9') {
txNum = '0';
}
dataToSend[8] = txNum;
}
here is my code for the slave
// SimpleRxAckPayload- the slave or the receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//oled
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1331.h>
//Oled
#define sclk 45
#define mosi 43
#define cs 37
#define rst 41
#define dc 39
// Color definitions
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define CE_PIN 9
#define CSN_PIN 10
const byte thisSlaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
RF24 radio(CE_PIN, CSN_PIN);
Adafruit_SSD1331 display = Adafruit_SSD1331(cs, dc, mosi, sclk, rst);
char dataReceived[10]; // this must match dataToSend in the TX
int ackData[2] = {109, -4000}; // the two values to be sent to the master
bool newData = false;
//==============
const int led = A3;
void setup() {
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
Serial.begin(9600);
Serial.println("SimpleRxAckPayload Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1, thisSlaveAddress);
radio.enableAckPayload();
radio.startListening();
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // pre-load data
display.begin();
display.fillScreen(BLACK);
}
//==========
void loop() {
getData();
showData();
}
//============
void getData() {
if ( radio.available() ) {
digitalWrite(led, HIGH);
radio.read( &dataReceived, sizeof(dataReceived) );
updateReplyData();
displayString(1.5, 0, 0, dataReceived, WHITE);
newData = true;
digitalWrite(led, LOW);
}
}
//================
void showData() {
if (newData == true) {
Serial.print("Data received ");
Serial.println(dataReceived);
Serial.print(" ackPayload sent ");
Serial.print(ackData[0]);
Serial.print(", ");
Serial.println(ackData[1]);
newData = false;
}
}
//================
void updateReplyData() {
ackData[0] -= 1;
ackData[1] -= 1;
if (ackData[0] < 100) {
ackData[0] = 109;
}
if (ackData[1] < -4009) {
ackData[1] = -4000;
}
radio.writeAckPayload(1, &ackData, sizeof(ackData)); // load the payload for the next time
}
void displayString(float s, float x, float y, String str, char color) {
display.setTextSize(s);
display.setTextColor(color);
display.setCursor(x, y);
display.println(str);
}
My serial monitor looks like this when the display code is on
01:44:53.593 -> Data received Message 3
01:44:53.630 -> ackPayload sent 104, -4005
01:44:54.620 -> Data received Message 4
01:44:54.657 -> ackPayload sent 103, -4006
01:44:55.635 -> Data received Message 5
01:44:55.672 -> ackPayload sent 102, -4007
01:45:00.676 -> Data received Message 0
01:45:00.712 -> ackPayload sent 101, -4008
01:45:02.687 -> Data received Message 2
01:45:02.722 -> ackPayload sent 100, -4009
01:45:04.719 -> Data received Message 4
01:45:04.754 -> ackPayload sent 109, -4000
01:45:05.741 -> Data received Message 5
01:45:05.741 -> ackPayload sent 108, -4001
and like this when the display code is commented out
01:49:04.503 -> Data received Message 2
01:49:04.539 -> ackPayload sent 103, -4006
01:49:05.520 -> Data received Message 3
01:49:05.520 -> ackPayload sent 102, -4007
01:49:06.524 -> Data received Message 4
01:49:06.524 -> ackPayload sent 101, -4008
01:49:07.510 -> Data received Message 5
01:49:07.547 -> ackPayload sent 100, -4009
01:49:08.524 -> Data received Message 6
01:49:08.524 -> ackPayload sent 109, -4000
01:49:09.514 -> Data received Message 7
01:49:09.552 -> ackPayload sent 108, -4001
I think that somehow the SPI is getting messed up by either the RF24 or the OLED display but I don't know how to fix it.