I have tried the nRF24L01p, RF24, and Mirf libraries and they are all overwhelming in the sense that I am trying to do an extremely simple function and cannot seem to get it to work. I need two Arduino Duos; one RX and one TX. The TX needs to constantly output a single number (ex. "2") and the RX needs to look for a number . I basically need several arduinos transmitting different numbers and the RX needs to in a sense "check off" whether previous chronological numbers were received; and if the new number is the next in line, execute a function that is specific to that number (ie each number corresponds to a scene that the RX will trigger a specific event to happen when the RX receives the scene; but all scenes need to be played back in chronological order)
I was trying to start with this code for TX
#include <SPI.h>
#include <nRF24L01p.h>
#include <regMapCmds.h>
// Set up CE & CSN radio pins
nRF24L01p transmitter(7,8);
void setup() {
delay(150);
Serial.begin(115200);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
transmitter.channel(90);
transmitter.TXaddress("HACS");
transmitter.init();
}
String message;
void loop() {
if(Serial.available()>0) {
char character=Serial.read();
if(character=='\n') {
transmitter.txPL(message);
transmitter.send(SLOW);
// SET SCENE NUMBER IN MESSAGE
message="2";
}else{
message+=character;
}
}
}
and this for RX
#include <SPI.h>
#include <nRF24L01p.h>
#include <regMapCmds.h>
// Set up CE & CSN radio pins
nRF24L01p receiver(7,8);
void setup() {
delay(150);
Serial.begin(115200);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
receiver.channel(90);
receiver.RXaddress("HACS");
receiver.init();
}
String message;
void loop() {
if(receiver.available()){
receiver.read();
receiver.rxPL(message);
Serial.println(message);
message="";
}
}
but i honestly am having so much trouble and have so little code knowledge.
I suggest you divide the problem into two parts. The first part is for a transmitter to send a message containing a number, and for the receiver to receive that message.
There are several libraries for the RF24 transceivers. I'm sure any of them would be able to do this - I'm most familiar with ManiacBug's RF24 library, and that can certainly do it. It comes with sender and receiver examples showing how to send messages.
Unless you need to send numbers higher than 255, I suggest you encode your number as a single byte.
Once you have the capability to send and receive messages, you can look at the second part of your problem which is keeping track of the current position in the number sequence. That seems like a relatively simple problem, but I suggest you get the basic communication working first.
I fiddled around with the RF24network example (hello world). But i cant get the scene number "2" to send over... I am completely new at programming so I know its probably a novice mistake... If anyone could correct me or point me in the correct direction that would be great. I also only need the text "2" to send to the RX - no payload or anything else.
TX
/*
CORNER OF CHAOS
Hayride Automation Control System (HACS) v1.0 2014
Wireless show control comunications between hayride wagon
and individual scenes. Use the scene number to corespond
to the chronological scene order.
TRANSMIT NODE - use for scenes
*/
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
// SET SCENE NUMBER
#define sceneNum "2"
// nRF24L01(+) radio attached using Getting Started board
RF24 radio(9,10);
// Network uses that radio
RF24Network network(radio);
// Address of scene nodes
const uint16_t this_node = 1;
// Address of the onboard receiving units
const uint16_t other_node = 0;
// How often to send scene number to onboard receiving unit
const unsigned long interval = 200; //ms
// When did we last send?
unsigned long last_sent;
// How many have we sent already
unsigned long packets_sent;
// Structure of our payload
struct payload_t
{
unsigned long ms;
unsigned long counter;
};
void setup(void)
{
Serial.begin(57600);
SPI.begin();
radio.begin();
network.begin(/*channel*/ 90, /*node address*/ this_node);
}
void loop(void)
{
// Pump the network regularly
network.update();
// If it's time to send a message, send it!
unsigned long now = millis();
if ( now - last_sent >= interval )
{
last_sent = now;
Serial.print("Sending...");
payload_t payload = { millis(), packets_sent++ };
RF24NetworkHeader header(/*to node*/ other_node);
bool ok = network.write(header,sceneNum,sizeof(payload));
if (ok)
Serial.println("ok.");
else
Serial.println("failed.");
}
}
// vim:ai:cin:sts=2 sw=2 ft=cpp
RX
/*
CORNER OF CHAOS
Hayride Automation Control System (HACS) v1.0 2014
Wireless show control comunications between hayride wagon
and individual scenes. Wagon receives scene numebers sent
from scene nodes and cues playback of scenes in
chronological order.
RECEIVE NODE - use for wagons
*/
#include <RF24Network.h>
#include <RF24.h>
#include <SPI.h>
// nRF24L01(+) radio attached using Getting Started board
RF24 radio(9,10);
// Network uses that radio
RF24Network network(radio);
// Address of our node
const uint16_t this_node = 0;
// Address of the other node
const uint16_t other_node = 1;
// Structure of our payload
struct payload_t
{
unsigned long ms;
unsigned long counter;
};
void setup(void)
{
Serial.begin(57600);
Serial.println("RF24Network/examples/helloworld_rx/");
SPI.begin();
radio.begin();
network.begin(/*channel*/ 90, /*node address*/ this_node);
}
void loop(void)
{
// Pump the network regularly
network.update();
// Is there anything ready for us?
while ( network.available() )
{
// If so, grab it and print it out
RF24NetworkHeader header;
payload_t payload;
network.read(header,sceneNum,sizeof(payload));
Serial.print();
}
}
// vim:ai:cin:sts=2 sw=2 ft=cpp
Thank you!! Now when I need to check chronological order on the RX end, what sort of code would do that? Is there a way to reference payload and check previously received numbers? I also would like to stop receiving a specific number once its been received. Otherwise I have scene numbers coming in like crazy
Im sorry i meant to say that when the receiver has received the transmitted scene number, have the receiver stop accepting any more incoming transmissions from that node. In essence, once node 1 has been received then stop looking for node 1 and begin looking for node 2. In the meantime all the individual nodes will still be broadcasting as there are multiple receiving units all working separately in different positions along the "route"
If I use the payload as the binary number like you mentioned, what sort of code would I need to start with to record the number on the receiving end? Every time I try to assign a new integer or something from the payload input on the RX I either cannot leave the while statement or if it's outside the while brackets I cannot reference payload. If you could point me in the correct direction it'd be a lifesaver. I apologize I'm a complete noob at programming...
Perfect! but how could I get the payload.ms to save to newValue? and is there a way to reference the payload.ms outside of the while() function brackets?