This example is a frivolous game in which the PRX generates a random number and the nodes try to guess the number. Its real purpose is to demonstrate the network in action.
While I think Codebender is fantastic, Please copy the code out of Codebender because the RF24.h library in Codebender appears to be an older version.
First, the code for the PRX primary receiver (hub)
=======================================================
// An example demonstrating the multiceiver capability of the NRF24L01+// in a star network with one PRX hub and up to six PTX nodes
//This sketch is a modification from a video on the ForceTronics YouTube Channel,
//which code was leveraged from http://maniacbug.github.io/RF24/starping_8pde-example.html
//This sketch is free to the public to use and modify at your own risk
#include <SPI.h> //Call SPI library so you can communicate with the nRF24L01+
#include <nRF24L01.h> //nRF2401 libarary found at https://github.com/tmrh20/RF24/
#include <RF24.h> //nRF2401 libarary found at https://github.com/tmrh20/RF24/
const int pinCE = 7; //This pin is used to set the nRF24 to standby (0) or active mode (1)
const int pinCSN = 8; //This pin is used to tell the nRF24 whether the SPI communication is a command or message to send out
RF24 radio(pinCE, pinCSN); // Declare object from nRF24 library (Create your wireless SPI)
//Create up to 6 pipe addresses P0 - P5; the "LL" is for LongLong type
const uint64_t rAddress[] = {0x7878787878LL, 0xB3B4B5B6F1LL, 0xB3B4B5B6CDLL, 0xB3B4B5B6A3LL, 0xB3B4B5B60FLL, 0xB3B4B5B605LL };
byte daNumber = 0; //The number that the transmitters are trying to guess
void setup()
{
randomSeed(analogRead(0)); //create unique seed value for random number generation
daNumber = (byte)random(11); //Create random number that transmitters have to guess
Serial.begin(115200); //start serial to communication
Serial.print("The number they are trying to guess is: ");
Serial.println(daNumber); //print the number that they have to guess
Serial.println();
radio.begin(); //Start the nRF24 module
radio.setPALevel(RF24_PA_LOW); // "short range setting" - increase if you want more range AND have a good power supply
radio.setChannel(108); // the higher channels tend to be more "open"
// Open up to six pipes for PRX to receive data
radio.openReadingPipe(0,rAddress[0]);
radio.openReadingPipe(1,rAddress[1]);
radio.openReadingPipe(2,rAddress[2]);
radio.openReadingPipe(3,rAddress[3]);
radio.openReadingPipe(4,rAddress[4]);
radio.openReadingPipe(5,rAddress[5]);
radio.startListening(); // Start listening for messages
}
void loop()
{
byte pipeNum = 0; //variable to hold which reading pipe sent data
byte gotByte = 0; //used to store payload from transmit module
while(radio.available(&pipeNum)){ //Check if received data
radio.read( &gotByte, 1 ); //read one byte of data and store it in gotByte variable
Serial.print("Received guess from transmitter: ");
Serial.println(pipeNum + 1); //print which pipe or transmitter this is from
Serial.print("They guess number: ");
Serial.println(gotByte); //print payload or the number the transmitter guessed
if(gotByte != daNumber) { //if true they guessed wrong
Serial.println("Fail!! Try again.");
}
else { //if this is true they guessed right
if(sendCorrectNumber(pipeNum)) Serial.println("Correct! You're done."); //if true we successfully responded
else Serial.println("Write failed"); //if true we failed responding
}
Serial.println();
}
delay(200);
}
//This function turns the receiver into a transmitter briefly to tell one of the nRF24s
//in the network that it guessed the right number. Returns true if write to module was
//successful
bool sendCorrectNumber(byte xMitter) {
bool worked; //variable to track if write was successful
radio.stopListening(); //Stop listening, start receiving data.
radio.openWritingPipe(rAddress[xMitter]); //Open writing pipe to the nRF24 that guessed the right number
// note that this is the same pipe address that was just used for receiving
if(!radio.write(&daNumber, 1)) worked = false; //write the correct number to the nRF24 module, and check that it was received
else worked = true; //it was received
radio.startListening(); //Switch back to a receiver
return worked; //return whether write was successful
}
=========================================================
And now the code for the PTX primary transmitters (nodes)
// An example demonstrating the multiceiver capability of the NRF24L01+
// in a star network with one PRX hub and up to six PTX nodes
//This sketch is a modification from a video on the ForceTronics YouTube Channel,
//which code was leveraged from http://maniacbug.github.io/RF24/starping_8pde-example.html
//This sketch is free to the public to use and modify at your own risk
#include <SPI.h> //Call SPI library so you can communicate with the nRF24L01+
#include <nRF24L01.h> //nRF2401 libarary found at https://github.com/tmrh20/RF24/
#include <RF24.h> //nRF2401 libarary found at https://github.com/tmrh20/RF24/
const int pinCE = 7; //This pin is used to set the nRF24 to standby (0) or active mode (1)
const int pinCSN = 8; //This pin is used to tell the nRF24 whether the SPI communication is a command or message to send out
RF24 radio(pinCE, pinCSN); // Create your nRF24 object or wireless SPI connection
#define WHICH_NODE 2 // must be a number from 1 - 6 identifying the PTX node
const uint64_t wAddress[] = {0x7878787878LL, 0xB3B4B5B6F1LL, 0xB3B4B5B6CDLL, 0xB3B4B5B6A3LL, 0xB3B4B5B60FLL, 0xB3B4B5B605LL};
const uint64_t PTXpipe = wAddress[ WHICH_NODE - 1 ]; // Pulls the address from the above array for this node's pipe
byte counter = 1; //used to count the packets sent
bool done = false; //used to know when to stop sending packets
void setup()
{
Serial.begin(115200); //start serial to communicate process
randomSeed(analogRead(0)); //create unique seed value for random number generation
radio.begin(); //Start the nRF24 module
radio.setPALevel(RF24_PA_LOW); // "short range setting" - increase if you want more range AND have a good power supply
radio.setChannel(108); // the higher channels tend to be more "open"
radio.openReadingPipe(0,PTXpipe); //open reading or receive pipe
radio.stopListening(); //go into transmit mode
}
void loop()
{
if(!done) { //true once you guess the right number
byte randNumber = (byte)random(11); //generate random guess between 0 and 10
radio.openWritingPipe(PTXpipe); //open writing or transmit pipe
if (!radio.write( &randNumber, 1 )){ //if the write fails let the user know over serial monitor
Serial.println("Guess delivery failed");
}
else { //if the write was successful
Serial.print("Success sending guess: ");
Serial.println(randNumber);
radio.startListening(); //switch to receive mode to see if the guess was right
unsigned long startTimer = millis(); //start timer, we will wait 200ms
bool timeout = false;
while ( !radio.available() && !timeout ) { //run while no receive data and not timed out
if (millis() - startTimer > 200 ) timeout = true; //timed out
}
if (timeout) Serial.println("Last guess was wrong, try again"); //no data to receive guess must have been wrong
else { //we received something so guess must have been right
byte daNumber; //variable to store received value
radio.read( &daNumber,1); //read value
if(daNumber == randNumber) { //make sure it equals value we just sent, if so we are done
Serial.println("You guessed right so you are done");
done = true; //signal to loop that we are done guessing
}
else Serial.println("Something went wrong, keep guessing"); //this should never be true, but just in case
}
radio.stopListening(); //go back to transmit mode
}
}
delay(1000);
}
=========================================
The #define WHICH_NODE statement needs to be altered for each node. Otherwise, the code is the same.
When running, I suggest powering the PRX first and starting the serial monitor for it. Otherwise you might miss the action!
ENJOY! And please do comment on any way in which this Instructable might be improved.
No comments:
Post a Comment