I have been tinkering for 2 days to manage send and receive between 2 arduinos. My code is bellow:
Arduino 1:
#include <SPI.h>
#include <RF24.h>
#include "printf.h"
#define LED 2
#define RF_CS 8
#define RF_CSN 9
RF24 radio(8,9);
String datargb = "rgb: 000000005";
char chartosend;
char inchar;
String inradio;
int endchar = '~';
void check_radio(void);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
printf_begin();
radio.begin();
radio.setRetries(15,15);
radio.openWritingPipe(0x080C600C01LL);
radio.openReadingPipe(1,0x080C600C71LL);
radio.setPALevel(RF24_PA_MAX);
radio.setAutoAck(true);
radio.printDetails();
attachInterrupt(0, check_radio, FALLING);
radio.stopListening();
}
void loop() {
// put your main code here, to run repeatedly:
int lnth = datargb.length();
for(int i=0; i<lnth; i++){
chartosend=datargb.charAt(i);
while(!radio.write( &chartosend, sizeof(chartosend) )){}
Serial.println("sent");
}
radio.write(&endchar,1);
}
void check_radio(void){
bool tx,fail,rx;
radio.whatHappened(tx,fail,rx);
if(rx){
Serial.println("interrupt!!");
radio.startListening();
radio.read( &inchar, sizeof(inchar) );
inradio+=inchar;
Serial.println(inchar);
if(inradio.endsWith("~")){
Serial.println(inradio);
if(inradio.startsWith("rgb: ")){
analogWrite(6,inradio.substring(5,8).toInt());
analogWrite(9,inradio.substring(8,11).toInt());
analogWrite(5,inradio.substring(11,14).toInt());
}
inradio = "";
radio.stopListening();
}
}
}
Arduino 2:
#include <SPI.h>
//#include <nRF24L01.h>
#include <RF24.h>
#include "printf.h"
//#include <RF24_config.h>
RF24 radio(8,10);
char data;
char chartosend;
char endchar = '~';
String inradio;
void check_radio(void);
String datargb = "To mounti tis manas tou nrf24";
void setup() {
// put your setup code here, to run once:
pinMode(9, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, INPUT);
attachInterrupt(1, check_radio, FALLING);
Serial.begin(115200);
printf_begin();
radio.begin();
//radio.setRetries(2,15);
// open pipe for writing
radio.openReadingPipe(1,0x080C600C01LL);
radio.openWritingPipe(0x080C600C71LL);
radio.setPALevel(RF24_PA_MAX);
radio.startListening();
radio.printDetails();
radio.setAutoAck(true);
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(7)==HIGH){
radio.stopListening();
for(int i=0; i<28; i++){
chartosend=datargb.charAt(i);
while(!radio.write( &chartosend, sizeof(chartosend) )){}
//Serial.println("sent");
}
radio.write(&endchar,1);
while(digitalRead(7)==HIGH){}
radio.startListening();
}
}
void check_radio(void){
bool tx,fail,rx;
radio.whatHappened(tx,fail,rx);
if(rx){
Serial.println("interrupt!!");
//radio.startListening();
radio.read( &data, sizeof(data) );
inradio+=data;
Serial.println(data);
if(inradio.endsWith("~")){
Serial.println(inradio);
if(inradio.startsWith("rgb: ")){
analogWrite(6,inradio.substring(5,8).toInt());
analogWrite(9,inradio.substring(8,11).toInt());
analogWrite(5,inradio.substring(11,14).toInt());
}
inradio = "";
//radio.stopListening();
}
}
}
Arduino 2 receives all data from ar1 but when I press the button attached to the pin 7 arduino 2 stops listening and only sends the first character of the string.