I'm trying to develop a remote control for my garage using 2 of Nanos, breakout boards and RF24s. It looked good for the first operation then proved to be consistently unreliable. My approach in these situations is to take it back to basics, ensure it works and then build on from there.
I used some Ralf Bacon example code, this generates a random number from 0 to 254 and transmits it. The receiver takes that value, increments it by 1 and passes it back. The serial port on the transmitter shows the incremented number. This works solidly and proves my wiring; firm foundations.
My remote control garage door is simpler. A button on the transmitter supplies 8 volts to the Arduino. All the code is in setup, it just creates and unsigned char, assigns 1 and writes it. Two LEDs (red and green) show if an acknowledgement was received or not.
The receiver reads the data and if it is 1 (which is should be) it currently lights an LED for a second to show it works.
First attempt: Press button on transmitter, green light shows an acknowledgement. The receiver LED goes on. Success!
Second and subsequent attempts: Press button on transmitter, green light shows an acknowledgement. The receiver light remains off.
Powering off the transmitter appears to stop the receiver working, even when the transmitter is powered on again.
Fixes
Powering off the receiver and powering it back on again allows one more press of the transmitter to work.
Powering the transmitter by the USB and then pressing reset will light the green light on the transmitter (an acknowledgement) and the light on the receiver. Remove the USB cable leaving it without power and then press the button to power it on and it works again. However once the power to the transmitter has been lost ,it works for one more button press and is then dysfunctional again.
Note: pressing the reset button on the receiver does not remedy anything, it only works by powering it down.
Note: If I power down the receiver and then press the button on the transmitter, the transmitter red light goes on, showing no acknowledgement was received. That proves the acknowledgement is working correctly.
I've put Serial.println's in my receiver code to see what is happening. Once everything is reset and the transmitter button is pressed, the serial window reports that the radio is available, the value of my data is 1 and then states Open.
However on a subsequent press of the transmitter button no text is displayed so radio.available is false. However I do get an acknowledgement LED on the transmitter
Here is the code for the transmitter
#include "Arduino.h"
#include <SPI.h>
#include <RF24.h>
// This is just the way the RF24 library works:
// Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
RF24 radio(7, 8);
byte addresses[][6] = {"1Node", "2Node"};
const byte greenLed = 3;
const byte redLed = 4;
// -----------------------------------------------------------------------------
// SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
// -----------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Serial.println("THIS IS THE TRANSMITTER CODE - YOU NEED THE OTHER ARDIUNO TO SEND BACK A RESPONSE");
pinMode(greenLed, OUTPUT);
pinMode(redLed, OUTPUT);
// Initiate the radio object
radio.begin();
// Set the transmit power to lowest available to prevent power supply related issues
radio.setPALevel(RF24_PA_MIN);
// Set the speed of the transmission to the quickest available
radio.setDataRate(RF24_2MBPS);
// Use a channel unlikely to be used by Wifi, Microwave ovens etc
radio.setChannel(124);
// Open a writing and reading pipe on each radio, with opposite addresses
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
unsigned char data = 1;
// Ensure we have stopped listening (even if we're not) or we won't be able to transmit
radio.stopListening();
Serial.println("About to transmit");
// Did we manage to SUCCESSFULLY transmit that (by getting an acknowledgement back from the other Arduino)?
// Even we didn't we'll continue with the sketch, you never know, the radio fairies may help us
if (!radio.write( &data, sizeof(unsigned char) )) {
Serial.println("No acknowledgement of transmission - receiving radio device connected?");
digitalWrite(redLed, HIGH);
delay(1000);
digitalWrite(redLed, LOW);
} else {
Serial.println("Acknowledgement received");
digitalWrite(greenLed, HIGH);
delay(1000);
digitalWrite(greenLed, LOW);
}
}
void loop() {
}
Here is the code for my receiver
#include "Arduino.h"
#include <SPI.h>
#include <RF24.h>
// This is just the way the RF24 library works:
// Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
RF24 radio(7, 8);
const byte led = 5;
byte addresses[][6] = {"1Node", "2Node"};
// -----------------------------------------------------------------------------
// SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
// -----------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Serial.println("THIS IS THE RECEIVER CODE - YOU NEED THE OTHER ARDUINO TO TRANSMIT");
//Set pin mode for LED
pinMode(led, OUTPUT);
// Initiate the radio object
radio.begin();
// Set the transmit power to lowest available to prevent power supply related issues
radio.setPALevel(RF24_PA_MIN);
// Set the speed of the transmission to the quickest available
radio.setDataRate(RF24_2MBPS);
// Use a channel unlikely to be used by Wifi, Microwave ovens etc
radio.setChannel(124);
// Open a writing and reading pipe on each radio, with opposite addresses
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
// Start the radio listening for data
radio.startListening();
}
// -----------------------------------------------------------------------------
// We are LISTENING on this device only (although we do transmit a response)
// -----------------------------------------------------------------------------
void loop() {
// This is what we receive from the other device (the transmitter)
//unsigned char data;
// Is there any data for us to get?
if ( radio.available()) {
Serial.println("Radio is available");
// Go and read the data and put it into that variable
while (radio.available()) {
unsigned char data;
radio.read(&data, sizeof(char));
Serial.print("My data is ");
Serial.println(data);
if (data == 1) {
digitalWrite(led, HIGH); //Switch it on
delay(1000); //wait a second
digitalWrite(led, LOW); //Switch it off
Serial.print("Open "); //flash the tx light
}
}
}
}
I've really run into a brick wall. Any help would be greatly appreciated.