I have a stopwatch that I want to start and stop with these NRF-8PA-LNA+arduino Nano modules. To start/stop the stopwatch it needs a "High pulse (+5V)". So, I bought 4 NRF-8PA-LNA and 4 Arduino Nano for this project. I tested the NRF-8PA-LNA modules with the basic code: nRF24L01 – How It Works, Arduino Interface, Circuits, Codes ("hello world"). It worked well.
The whole project: (TIMING GATE)
I want to measure time between two gates. The first is the "starting gate" and the second will be the "stop gate". If somebody crosses through the laser line the gates immediately pull the output to high for a moment. This output "high" that I want to send to my stopper to start or stop. So, I have to write a code for a transmitter and receiver. Yes, I have two transmitter and two receiver. They have to work on separate frequencies. The first transmitter will be in the "starting gate" and the second in the "stop gate". I want least amount of delay between the "crossing through the laser line" and the start of the stopper.
My problem with the project:
I don't understand the method of writing codes. I read the whole NRF-8PA-LNA documentation to help myself.. I tried Ai Arduino code generator but it didn't work.
I need help to make this.
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN pins
const byte address[6] = "00001"; // Address of NRF-8PA-LNA module
void setup() {
pinMode(2, INPUT); // Set D2 pin as input
radio.begin(); // Initialize radio communication
radio.setPALevel(RF24_PA_MAX); // Set maximum output power
radio.setDataRate(RF24_2MBPS); // Set air data rate to 2Mbps
radio.openWritingPipe(address); // Set address for writing
}
void loop() {
if (digitalRead(2) == HIGH) {
char text[] = "start"; // If D2 pin is high
radio.write(&text, sizeof(text)); // Send "start" text
} else { // If D2 pin is not high
radio.powerDown(); // Switch NRF-8PA-LNA module to Standby-I mode
}
}
Ai generated receiver code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN pins
const byte address[6] = "00001"; // address of NRF-8PA-LNA module
void setup() {
pinMode(2, OUTPUT); // set D2 pin as output
radio.begin(); // initialize radio communication
radio.openReadingPipe(0, address); // set address for receiving data
radio.setPALevel(RF24_PA_MAX); // set max sensitivity
radio.setDataRate(RF24_2MBPS); // set air data rate to 2Mbps
radio.startListening(); // start listening for incoming data
}
void loop() {
if (radio.available()) { // check if data is available
char text[6]; // create char array to store received text
radio.read(&text, sizeof(text)); // read data into char array
if (strcmp(text, "start") == 0) { // check if received text is "start"
digitalWrite(2, HIGH); // set D2 pin to HIGH
delay(50); // wait for 50ms
digitalWrite(2, LOW); // set D2 pin back to LOW
}
}
}
#include <RF24.h>
RF24 radio(9,10); // CE, CSN pins
const byte address[5] = {0xCE,0xCC,0xCC,0xCC,0xCC}; // Address of NRF-8PA-LNA module
void setup() {
Serial.begin(115200);
pinMode(2, INPUT_PULLUP); // Set D2 pin as input
radio.begin(); // Initialize radio communication
radio.setPALevel(RF24_PA_MAX); // Set maximum output power
radio.setDataRate(RF24_2MBPS); // Set air data rate to 2Mbps
radio.openWritingPipe(address); // Set address for writing
}
void loop() {
bool onOff = digitalRead(2);
Serial.print("Write ");
Serial.println(onOff ? "Off" : "On");
radio.write(&onOff, sizeof(onOff));
if(onOff == LOW){
delay(500);
}else{
delay(100);
}
}
Receiver:
#include <RF24.h>
RF24 radio(9,10); // CE, CSN pins
const byte address[5] = {0xCE,0xCC,0xCC,0xCC,0xCC}; // address of NRF-8PA-LNA module
void setup() {
Serial.begin(115200);
pinMode(2, OUTPUT); // set D2 pin as output
radio.begin(); // initialize radio communication
radio.openReadingPipe(1, address); // set address for receiving data
radio.setPALevel(RF24_PA_MAX); // set max sensitivity
radio.setDataRate(RF24_2MBPS); // set air data rate to 2Mbps
radio.startListening(); // start listening for incoming data
}
void loop() {
if (radio.available()) { // check if data is available
bool onOff = false;
radio.read(&onOff, sizeof(onOff)); // read data into char array
Serial.print("Status: ");
Serial.println(onOff ? "Off" : "On");
if (onOff == LOW) { // check if received text is "start"
digitalWrite(2, HIGH); // set D2 pin to HIGH
Serial.println("Gates High");
delay(50); // wait for 50ms
digitalWrite(2, LOW); // set D2 pin back to LOW
Serial.println("Gates Low");
}
}
}
The input button on the transmitter is active LOW, so you will need a button or switch that connects to ground with this current code.
Much appreciation!
I uploaded the code. Works well.
My laser gate output is going to be "high" if somebody crosses the laser line but I need low to start this program. So i have to invert my high to GND level.
One more thing:
I will need two "gates" as I mentioned before:
Starting gate and stop gate. I didn't see the code that can change the channels where the transmitter and receiver works. Like: "starting gate transmitter and receiver" has to work on channel 25 and the "stop gate transmitter and receiver" has to work on channel 30.
The two receiver will be in my handheld stopper's box. They will be very close to each other.
The transmitters will be in the laser gate's boxes.
Hi,
Sorry, I worded it wrong.
I need two transmitter and two receiver. (set two modules to transmitter mode and set two modules to receiver mode.
Starting laser gate: transmitter(ch25) --------->receiver (ch25)---> starting "high"
Stop laser gate: transmitter(ch30) -------------> receiver (ch30)---> stopping "high"
Hi,
I solved my earlier problem with the radio channels because I need two separate receiver and transmitter. ( I put this to the setup: radio.setChannel(10); )
Your code works well but I have little problem. (in the transmitter section)
As I mentioned before my laser gate output will be high if somebody crosses through the laser line. I made a NOT gate with a simple BC547 transistor. The problem:
When somebody crosses the line and the laser gate output goes high and the same time the bc547 NOT GATE output trying to go low but it couldn't really because the D2 pin is also high. The main problem: If somebody crosses the line under 30ms the Arduino don't START. The 30ms is: when somebody crosses through the laser line. The BC547 voltage when goes low: 490mV. I don't know why the Arduino Nano don't want to start at 490mV - this has to be enough for LOW.
How can I solve this problem?