Today I started messing with the idea of getting an arduino uno to autoconnect with another arduino uno using the nrf24l01 breakouts.
I dont have a particular goal in mind per se but was just wanting to get the logistics down so I started simple as pie. One arduino at address 0x0000000B LL sends a one element array over and over. Easy.
The second arduino goes through a for loop in the void loop().
The for loop is set to loop 16 times. It starts with the address 0x00000000 LL and checks to see if radio.available is true. if it is, the program simply states it has found an address over serial. If not it states the address is invalid. afterwards it increments the address by one and repeats until the address gets to 0x0000000F LL (or the for loop repeats 16 times.)
once the address gets to 0x0000000B (or the transmitter address) it states that it has found an address over serial. So far so good!
Now the problem arises when the address increments to the next address. No matter what I seem to do or change, once radio.available() becomes true once…it seems to stay true until the function is called again from the main loop in which case it repeats the same scenario. I have worked hours on such a small program to no avail. Can someone help please??? I must be overlooking the most simple of things but for the life of me I have no idea what.
Super Simple TX Code.
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(8,9);
uint64_t myAddress = 0x0000000BLL;
byte txData[] = {1};
void setup()
{
while (!Serial);
Serial.begin(9600);
radio.begin();
radio.setRetries(15, 15);
radio.openWritingPipe(myAddress);
}
void loop()
{
if( Serial.available() )
{;}
radio.write(txData,sizeof(txData) );
Serial.println("Sent a packet");
}
RX CODE
#include <nRF24L01.h>
#include <RF24.h>
RF24 myRadio(8, 9);
byte rxData[] = {0};
void setup()
{
while (!Serial);
Serial.begin(9600);
myRadio.begin();
myRadio.setRetries(15, 15);
}
void loop()
{
if( Serial.available() )
{;}
//function that searches through a set of 16 addresses
searchAddresses();
}
void searchAddresses()
{
//declare a temp address variable
//and initialize it to first possible address
uint64_t tempAddress = 0x00000000LL;
myRadio.openReadingPipe(1, tempAddress);
myRadio.startListening();
for (int x =0; x < 16; x++)
{
if ( myRadio.available() )
{
myRadio.read(rxData, sizeof(rxData) );
Serial.println("Found an address");
}
else
{
Serial.println("Address Invalid");
}
//increment temporary address to next address
tempAddress++;
myRadio.openReadingPipe(1, tempAddress);
myRadio.startListening();
}
}