nRF24L01 with interrupt on receiver

I copied from Mr.ForceTronics with some modify, you can check these URL for reference:

youtube: https://www.youtube.com/watch?v=vzWcBAWpTx0

code: http://forcetronic.blogspot.com/2016/07/using-nrf24l01s-irq-pin-to-generate.html

You only act on response. That is the reason why interrupt is useful. The rest time you can rest to save time. On transmitter module, I send one byte of random number. On receiver module, I deploy pin IRQ to trigger interrupt on pin 3.

 nRF24L01_TX_test_c.ino


//********************Transmitter code**************************** 
#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 = 9; //This pin is used to set the nRF24 to standby (0) or active mode (1)
const int pinCSN = 10; //This pin is used to tell the nRF24 whether the SPI communication is a command or message to send out
byte counter = 1; //used to count the packets sent
RF24 wirelessSPI(pinCE, pinCSN); // Create your nRF24 object or wireless SPI connection
const uint64_t pAddress = 0xB00B1E5000LL; // Radio pipe addresses for the 2 nodes to communicate.

void setup() 
{
 Serial.begin(57600); //start serial to communicate process
 wirelessSPI.begin(); //Start the nRF24 module
 wirelessSPI.setAutoAck(1); // Ensure autoACK is enabled so rec sends ack packet to let you know it got the transmit packet payload
 wirelessSPI.enableAckPayload(); // Allow optional ack payloads
 wirelessSPI.setPALevel(RF24_PA_LOW);
 wirelessSPI.openWritingPipe(pAddress); // pipe address that we will communicate over, must be the same for each nRF24 module
 wirelessSPI.stopListening(); //transmitter so stop listening for data
 randomSeed(analogRead(0)); //use random ADC value to seed random number algorithm
}

void loop() {
 delay(random(100,5000)); //Generate delay time between 100msec and 5 sec
 long randNumber = random(0, 255);
 counter = byte(randNumber);
 Serial.println("Sending packet"); 
 if (!wirelessSPI.write( &counter, 1 )){ //if the send fails let the user know over serial monitor
 Serial.println("packet delivery failed"); 
 }
 Serial.println(); 
}

 nRF24L01_RX_test_c.ino


#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/
#include <avr/sleep.h> //library needed to use AVR based sleep API

const int pinCE = 9; //This pin is used to set the nRF24 to standby (0) or active mode (1)
const int pinCSN = 10; //This pin is used to tell the nRF24 whether the SPI communication is a command or message to send out
byte gotByte = 0; //used to store payload from transmit module
volatile int count = 0; //tracks the number of interrupts from IRQ
int pCount = 0; //tracks what last count value was so know when count has been updated
RF24 wirelessSPI(pinCE, pinCSN); // Declare object from nRF24 library (Create your wireless SPI) 
const uint64_t pAddress = 0xB00B1E5000LL; //Create a pipe addresses for the 2 nodes to communicate over, the "LL" is for LongLong type

void setup() {
 wirelessSPI.begin(); //Start the nRF24 module
 wirelessSPI.setAutoAck(1); // Ensure autoACK is enabled so rec sends ack packet to let you know it got the transmit packet payload
 wirelessSPI.enableAckPayload(); //allows you to include payload on ack packet
 wirelessSPI.maskIRQ(1,1,0); //mask all IRQ triggers except for receive (1 is mask, 0 is no mask)
 wirelessSPI.setPALevel(RF24_PA_LOW); //Set power level to low, won't work well at higher levels (interfer with receiver)
 wirelessSPI.openReadingPipe(1,pAddress); //open pipe o for recieving meassages with pipe address
 wirelessSPI.startListening(); // Start listening for messages
 attachInterrupt(1, interruptFunction, FALLING); //Create interrupt: 0 for pin 2 or 1 for pin 3, the name of the interrupt function or ISR, and condition to trigger interrupt
}

void loop() {

if(pCount < count) { //If this is true it means count was interated and another interrupt occurred
 Serial.begin(57600); //start serial to communicate process
 Serial.print("Receive packet number ");
 Serial.print(count); 
 Serial.print(" has content: ");
 Serial.println(gotByte);
 Serial.end(); //have to end serial since it uses interrupts
 pCount = count; 
 }
}

//This is the function called when the interrupt occurs (pin 2 goes high)
//this is often referred to as the interrupt service routine or ISR
//This cannot take any input arguments or return anything
void interruptFunction() {
 count++; //up the receive counter
 while(wirelessSPI.available()) { //get data sent from transmit
 wirelessSPI.read( &gotByte, 1 ); //read one byte of data and store it in gotByte variable
 }
}

Actually it is one-part done while we have total 2 modules here. The transmitter has to send the message all the time while the receiver only works when interrupt is triggered. The ideal is when the receiver makes a request to the transmitter, the transmitter then sends the message. The definition of transmitter and receiver is no longer suitable. The new definition is requester and responser.

 

I am so lazy at this weekend. Maybe nextweek I will be better. I really need a good sleep.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s