Hi, I am working in an Arduino project that works with the communication of 2 Arduinos (one UNO and one Pro Mini) with the NRF24L01 module). The main ideia is to send a 1000microseconds - 2000 microseconds signal from a potenciometer in the ardunio pro mini to an ESC connected to the arduino UNO. Everything works fine, but just for about 30 ou 40 seconds. It does not take the exact same time everytime, but after about 30-40 seconds the connection is just lost. Out of nowhere! I know the connection is lost because I wrote some lines of code telling the Arduino to print a sentence when radio is not available in the "if radio.available" part of the code.
Please, can someone help me with this issue? I'm really struggling with this. (Already tried connecting a capacitor between VCC and ground of the NRF, but it did not solve the problem).
The codes for the transmitter (arduino pro mini) and the receiver (UNO) are here:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(2, 3); // nRF24L01 (CE, CSN)
const byte endereco[6] = "00001"; // Address
struct Data_Package {
int accel;
int yaw;
int roll;
int pitch;
};
Data_Package data;
void setup() {
radio.begin();
radio.openWritingPipe(endereco);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
}
void loop() {
//data.accel = map(analogRead(A0), 0, 1023, 0, 180); // reads the value of the potentiometer (value between 0 and 1023) // scale it to use it with the servo library (value between 0 and 180)
data.accel = map(analogRead(A0), 0, 1023, 1000, 2000);
data.yaw = map(analogRead(A1), 0, 1023, 0, 180);
data.roll = map(analogRead(A2), 0, 1023, 0, 180);
data.pitch = map(analogRead(A3), 0, 1023, 0, 180);
radio.write(&data, sizeof(Data_Package));
}
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h> //Include the Wire.h library so we can communicate with the gyro
#include <EEPROM.h> //Include the EEPROM.h library so we can store information onto the EEPROM
#include <Servo.h>
byte last_channel_1, last_channel_2, last_channel_3, last_channel_4;
byte lowByte, highByte, type, gyro_address, error, clockspeed_ok;
byte channel_1_assign, channel_2_assign, channel_3_assign, channel_4_assign;
byte roll_axis, pitch_axis, yaw_axis;
byte receiver_check_byte, gyro_check_byte;
volatile int receiver_input_channel_1, receiver_input_channel_2, receiver_input_channel_3, receiver_input_channel_4;
int center_channel_1, center_channel_2, center_channel_3, center_channel_4;
int high_channel_1, high_channel_2, high_channel_3, high_channel_4;
int low_channel_1, low_channel_2, low_channel_3, low_channel_4;
int address, cal_int;
unsigned long timer, timer_1, timer_2, timer_3, timer_4, current_time;
float gyro_pitch, gyro_roll, gyro_yaw;
float gyro_roll_cal, gyro_pitch_cal, gyro_yaw_cal;
//Servo ESC1; // create servo object to control the ESC
Servo myservo;
Servo ESC2;
Servo ESC3;
Servo ESC4;
RF24 radio(2, 3); // nRF24L01 (CE, CSN)
const byte endereco[6] = "00001";
// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Data_Package {
int accel;
int yaw;
int roll;
int pitch;
};
Data_Package data; //Create a variable with the above structure
void setup(){
Serial.begin(9600);
// ESC1.attach(8,1000,2000);
myservo.attach(8);
ESC2.attach(9,1000,2000);
ESC3.attach(10,1000,2000);
ESC4.attach(1,1000,2000);
radio.begin();
radio.openReadingPipe(0, endereco);
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.setPALevel(RF24_PA_LOW);
radio.startListening();
PCICR |= (1 << PCIE0); // set PCIE0 to enable PCMSK0 scan
PCMSK0 |= (1 << PCINT0); // set PCINT0 (digital input 8) to trigger an interrupt on state change
PCMSK0 |= (1 << PCINT1); // set PCINT1 (digital input 9)to trigger an interrupt on state change
PCMSK0 |= (1 << PCINT2); // set PCINT2 (digital input 10)to trigger an interrupt on state change
PCMSK0 |= (1 << PCINT17); // set PCINT3 (digital input 1)to trigger an interrupt on state change
/* Wire.begin(); //Start the I2C as master
Serial.begin(57600); //Start the serial connetion @ 57600bps
delay(250); //Give the gyro time to start */
}
void loop(){
if (radio.available()) {
radio.read(&data, sizeof(Data_Package)); // Read the whole data and store it into the 'data' structure
//ESC1.write(data.accel);
myservo.writeMicroseconds(data.accel);
ESC2.write(data.yaw);
ESC3.write(data.roll);
ESC4.write(data.pitch);
Serial.println(receiver_input_channel_1);
/* delay(1000);
Serial.println(receiver_input_channel_2);
delay(1000);
Serial.println(receiver_input_channel_3);
delay(1000);
Serial.println(receiver_input_channel_4);*/
}
else { Serial.println("sem conexao");
}
}
ISR(PCINT0_vect){
current_time = micros();
//Channel 1=========================================
if(PINB & B00000001){ //Is input 8 high?
if(last_channel_1 == 0){ //Input 8 changed from 0 to 1
last_channel_1 = 1; //Remember current input state
timer_1 = current_time; //Set timer_1 to current_time
}
}
else if(last_channel_1 == 1){ //Input 8 is not high and changed from 1 to 0
last_channel_1 = 0; //Remember current input state
receiver_input_channel_1 = current_time - timer_1; //Channel 1 is current_time - timer_1
}
/*//Channel 2=========================================
if(PINB & B00000010 ){ //Is input 9 high?
if(last_channel_2 == 0){ //Input 9 changed from 0 to 1
last_channel_2 = 1; //Remember current input state
timer_2 = current_time; //Set timer_2 to current_time
}
}
else if(last_channel_2 == 1){ //Input 9 is not high and changed from 1 to 0
last_channel_2 = 0; //Remember current input state
receiver_input_channel_2 = current_time - timer_2; //Channel 2 is current_time - timer_2
}
//Channel 3=========================================
if(PINB & B00000100 ){ //Is input 10 high?
if(last_channel_3 == 0){ //Input 10 changed from 0 to 1
last_channel_3 = 1; //Remember current input state
timer_3 = current_time; //Set timer_3 to current_time
}
}
else if(last_channel_3 == 1){ //Input 10 is not high and changed from 1 to 0
last_channel_3 = 0; //Remember current input state
receiver_input_channel_3 = current_time - timer_3; //Channel 3 is current_time - timer_3
}
//Channel 4=========================================
if(PIND & B00000010 ){ //Is input 1 high?
if(last_channel_4 == 0){ //Input 1 changed from 0 to 1
last_channel_4 = 1; //Remember current input state
timer_4 = current_time; //Set timer_4 to current_time
}
}
else if(last_channel_4 == 1){ //Input 11 is not high and changed from 1 to 0
last_channel_4 = 0; //Remember current input state
receiver_input_channel_4 = current_time - timer_4; //Channel 4 is current_time - timer_4
}*/
}