I want to write HIGH to the PIN on the receiver side when the NRF transmitter device is on, and write LOW When it's off,
I wrote this code but sometimes the receiver does not receive any thing until period=30000 and that Makes to be written LOW on the pin and after 30000 receives data.
Is there any other way to know if the transmitter is off?
Transmitter:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
// Set the CE & CSN pins
#define CE_PIN 9
#define CSN_PIN 10
#define btn 8
// This is the address used to send/receive
const byte rxAddr[6] = "00001";
// Create a Radio
RF24 radio(CE_PIN, CSN_PIN);
void setup() {
// Start up the Serial connection
while (!Serial);
Serial.begin(9600);
pinMode(btn, INPUT);
// Start the Radio!
radio.begin();
// Power setting. Due to likelihood of close proximity of the devices, set as RF24_PA_MIN (RF24_PA_MAX is default)
radio.setPALevel(RF24_PA_MAX); // RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
// Slower data rate for better range
radio.setDataRate( RF24_250KBPS ); // RF24_250KBPS, RF24_1MBPS, RF24_2MBPS
// Number of retries and set tx/rx address
radio.setRetries(15, 15);
radio.openWritingPipe(rxAddr);
// Stop listening, so we can send!
radio.stopListening();
}
void loop() {
// Set up a message and a timestamp to it using millis()
String str = "on";
// http://stackoverflow.com/questions/7383606/converting-an-int-or-string-to-a-char-array-on-arduino
// Length (with one extra character for the null terminator)
int str_len = str.length() + 1;
// Prepare the character array (the buffer)
char char_array[str_len];
char char_array2[str_len2];
// Copy it over
str.toCharArray(char_array, str_len);
// Ace, let's now send the message
radio.write(&char_array, sizeof(char_array));
// Let the ourside world know..
Serial.print("Sent Message: ");
Serial.print( char_array );
Serial.println("");
// Wait a short while before sending the other one
delay(2000);
}
Receiver:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define pin 8
#define led 5
// define the pins
#define CE_PIN 9
#define CSN_PIN 10
// Create a Radio
RF24 radio(CE_PIN, CSN_PIN);
int cnt=0;
int period =30000;
unsigned long last_rec = 0;
unsigned long now = 0;
unsigned long diff=0;
// The tx/rx address
const byte rxAddr[6] = "00001";
void setup()
{
// Start the serial
Serial.begin(9600);
while(!Serial);
Serial.println("NRF24L01P Receiver Starting...");
// Start the radio, again set to min & slow as I'm guessing while testing theire really close to each other
radio.begin();
radio.setPALevel(RF24_PA_MAX); // RF24_PA_MIN ,RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX
radio.setDataRate( RF24_250KBPS ); // RF24_250KBPS, RF24_1MBPS, RF24_2MBPS
// Set the reading pipe and start listening
radio.openReadingPipe(0, rxAddr);
radio.startListening();
pinMode(pin, OUTPUT);
pinMode(led, OUTPUT);
}
void loop()
{
if (radio.available())
{
// the buffer to store the received message in
char text[100] = {0};
// Now read the message, old examples have done = radio.read(), that doesn't work anymore!!!
radio.read(&text, sizeof(text));
// Print the message out to the COM window
Serial.println("Received Message: ");
Serial.println(text);
//Serial.println(sizeof(text));
if(text[0]=='o'){
cnt=0;
digitalWrite(pin, HIGH);
digitalWrite(led, HIGH);
//Serial.println("HIGH");
last_rec=millis();
}
}
else{
now=millis();
diff = now - last_rec ;
if( diff >= period){
Serial.println("----------------------------------------------not receive-------------------------");
digitalWrite(pin, LOW);
digitalWrite(led, LOW);
now=0;
last_rec=0;
}
}
}