// SimpleTx - the master or the transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 6 //check pin connections
#define CSN_PIN 7 //check pin connections
const byte slaveAddress[][5] = {{'R', 'x', 'A', 'A', 'A'}, {'R', 'x', 'A', 'A', 'B'},
{'R', 'x', 'A', 'A', 'C'}, {'R', 'x', 'A', 'A', 'D'}};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataToSend[10] = "Message 0";
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
int numSlaves = 4;
radio.enableDynamicPayloads() ;
radio.setAutoAck( true ) ;
}
//====================
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis)
{
send();
prevMillis = millis();
}
}
//====================
void send() {
bool rslt;
for (byte n = 1; n < 3; n++) {
radio.openWritingPipe(slaveAddress[n]);
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 to Slave ");
Serial.println(n);
Serial.print(dataToSend);
}
if(rslt) {
Serial.print(" Acknowledge received from Slave ");
Serial.println(n);
updateMessage();
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;
}
/*DUE 2.8" TFT 240x320 uno shield ILI9341 multi wire
EDITED :NO serial monitor print. ONLY print on TFT
MultiTxAckPayload - the master or the transmitter
works with two or more(max6) Arduinos as slaves
each slave use this SimpleRxAckPayload program
example address {'R','x','A','A','A'}
and the other unit with {'R','x','A','A','B'}
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 50 //nrf24
#define CSN_PIN 51 //nrf24
#include <MCUFRIEND_kbv.h>
#include <Adafruit_GFX.h>
#include <TouchScreen.h>
MCUFRIEND_kbv tft;
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
#define TS_MINX 167 //
#define TS_MINY 228 //
#define TS_MAXX 880 //
#define TS_MAXY 860 //
#define YP A2 // must be an analog pin, use "An" notation!
#define XM A3 // must be an analog pin, use "An" notation!
#define YM 8 // can be a digital pin
#define XP 9 // can be a digital pin
#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 LIGHTGREY 0xDEDB
#define GREY 0xCE79
#define MINPRESSURE 10
#define MAXPRESSURE 1000
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 365); //.......p0
int X, Y, Z;
const byte numSlaves = 6; //indicates number of slaves see addresses below in { }
const byte slaveAddress[numSlaves][5] = {
{'R', 'x', 'A', 'A', 'A'}, //need address for each slave 1
{'R', 'x', 'A', 'A', 'B'}, //need address for each slave 2
{'R', 'x', 'A', 'A', 'C'}, //need address for each slave 3
{'R', 'x', 'A', 'A', 'D'}, //need address for each slave 4
{'R', 'x', 'A', 'A', 'E'}, //need address for each slave 5
{'R', 'x', 'A', 'A', 'F'} //need address for each slave 6
};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataToSend[32] = " Unit "; //this [32] must match dataReceived from Rx
char txNum = '0'; //must stay zero. keeps track of data sending
float ackData[2] = { -11.11, -11.11}; // to hold the two values coming from the slave
bool newData = false;
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send request 1ce per second
//===============
void setup() {
Serial.begin(9600);
//Serial.println("SimpleTxAckPayload Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.enableAckPayload();
radio.setRetries(10, 8); // delay, count
tft.reset();
uint16_t identifier = tft.readID();
tft.begin(identifier);
tft.setRotation(2); //screen, power supply port is bottom right
}
//=============
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
send();
}
}
//================
void send() {
//tft.fillScreen(LIGHTGREY);
for (byte n = 0; n < numSlaves; n++) { // call each slave in turn
// open the writing pipe with the address of a slave
radio.openWritingPipe(slaveAddress[n]);
// include the slave number in the message
dataToSend[5] = n + '1'; //start 'unit no' at one
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 ");
tft.setCursor(10, 10); //L Right,U Down print the word 'Unit1 Unit2 etc'
tft.setTextColor(YELLOW);
tft.setTextSize(2);
tft.print(dataToSend); //prints the word Unit1 Unit2 etc
if (rslt) {
if ( radio.isAckPayloadAvailable() ) {
radio.read(&ackData, sizeof(ackData));
newData = true;
}
else {
tft.print(" Acknowledge but no data ");
}
updateMessage();
}
else {
tft.print(" Tx failed");
}
showData();
if (dataToSend() == '1') {
tft.setCursor(10, 10);
}
if (dataToSend() == '2') {
tft.setCursor(10, 20);
}
if (dataToSend() == '3') {
tft.setCursor(10, 30);
}
if (dataToSend() == '4') {
tft.setCursor(10, 60);
}
if (dataToSend() == '5') {
tft.setCursor(10, 70);
}
if (dataToSend() == '6') {
tft.setCursor(10, 90);
}
//tft.print("\n");
}
prevMillis = millis();
}
//=================
void showData() {
if (newData == true) {
// Serial.print(" Acknowledge data ");
tft.setCursor(15, 50); //L Right,U Down
tft.setTextColor(YELLOW);
tft.setTextSize(2);
tft.println(ackData[0]);
tft.println(" , ");
tft.print(ackData[1]);
newData = false;
}
}
//================
void updateMessage() {
// so you can see that new data is being sent
txNum += 1;
if (txNum > '9') {
txNum = '0';
}
dataToSend[8] = txNum;
}