This is my 1st post, so forgive if a violate any standards.
I have setup 3 arduino's, 1 Receiver and 2 Transmitters, to send data on button presses. It is meant to be a simple sketch that lets 2 remotes toggle relays, it does not need to return whether the transmission was received (for now) and works fine in my scenario.
Right now I'm just trying to get the right values.
Here are the problems:
- The radio keeps "Disconnecting"
- (After removing the block of code that displays "Disconnected") The data received from the 2 transmitters are moving between the 2 Variables : RemOne and RemTwo instead of staying in their respective spots.
- When printing to Serial, I occasionally get missing or altered characters
Receiver Code:
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
RF24 radio(9,10);
const uint64_t Pipe[2] = {0xE8E8F0F0E1, 0x123};
int RemOne[3] ;
int RemTwo[3] ;
boolean lastSig1 = LOW;
boolean RelayOn1 = false;
boolean lastSig2 = LOW;
boolean RelayOn2 = false;
const int Relay1 = 2;
const int Relay2 = 3;
void setup(){
Serial.begin(57600);
radio.begin();
radio.setDataRate(RF24_1MBPS);
radio.openReadingPipe(1, Pipe[1]);
radio.openReadingPipe(0, Pipe[0]);
radio.setChannel(99);
radio.startListening();
pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(13, OUTPUT);
}
void loop(){
if(radio.available(Pipe[0])){
bool done = false;
while (!done){
done = radio.read(RemOne, sizeof(RemOne) );
Serial.print(" RemOne: ");
Serial.print(RemOne[0]);
Serial.print(RemOne[1]);
Serial.println(" ");
}}
else if(radio.available(1)){
bool don = false;
while(!don){
don = radio.read(RemTwo, sizeof(RemTwo));
Serial.print("RemTwo: ");
Serial.print(RemTwo[0]);
Serial.print(RemTwo[1]);
Serial.println(" ");
}
}
/* if (RemOne[0] == HIGH && lastSig1 == LOW){
RelayOn1 = !RelayOn1;
lastSig1 = HIGH;
} else{lastSig1 = RemOne[0]; }
digitalWrite(Relay1, RelayOn1);
if (RemOne[1] == HIGH && lastSig2 == LOW){
RelayOn2 = !RelayOn2;
lastSig2 = HIGH;
} else{lastSig2 = RemOne[1];}
digitalWrite(Relay2, RelayOn2);
*/
if (!radio.available()) {
Serial.println("Disconnected");
digitalWrite(6, HIGH);
delay(50);
digitalWrite(6,LOW);
}
}
Transmitter 1 Code:
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int ledPin =13;
const int butPin1 = 3;
const int butPin2 = 4;
int butState1 = 0;
int butState2 = 0;
RF24 radio(9,10);
const uint64_t Pipe[2] = {0xE8E8F0F0E1, 0x123};
int Rem1[3];
void setup(){
pinMode(butPin1, INPUT);
pinMode(butPin2, INPUT);
pinMode(ledPin, OUTPUT);
radio.begin();
radio.setChannel(99);
radio.setDataRate(RF24_1MBPS);
radio.openWritingPipe(Pipe[0]);
radio.stopListening();
Serial.begin(9600);
Serial.println("TX Rem A");
}
void loop(){
butState1 = digitalRead(butPin1);
if (butState1 >0){
Rem1[0] = 1;
}
else{
Rem1[0] = 0;
}
butState2 = digitalRead(butPin2);
if(butState2 >0){
Rem1[1] = 1;
}
else{Rem1[1] = 0;}
radio.write(Rem1, sizeof(Rem1));
Serial.print(Rem1[0]);
Serial.println(Rem1[1]);
}
Transmitter 2 Code:
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int ledPin =13;
const int butPin1 = 3;
const int butPin2 = 4;
int butState1 = 0;
int butState2 = 0;
RF24 radio(9,10);
const uint64_t Pipe[2] = {0xE8E8F0F0E1, 0x123};
int B_Rem[3] = {5 ,6 ,7};
void setup(){
pinMode(butPin1, INPUT);
pinMode(butPin2, INPUT);
pinMode(ledPin, OUTPUT);
radio.begin();
radio.setChannel(99);
radio.setDataRate(RF24_1MBPS);
radio.openWritingPipe(Pipe[1]);
Serial.begin(9600);
Serial.println("TX Rem B");
}
void loop(){
//Took out the code for reading button presses here and replaced with constant values
radio.write(B_Rem, sizeof(B_Rem));
Serial.print(B_Rem[0]);
Serial.print(B_Rem[1]);
Serial.println(B_Rem[2]);
}
P.S. all the hardware is functioning correctly, tested.