2022-01-15T16:00:00Z
Hi everyone, I am new to Arduino programming and have recently run into a couple of issues while trying to set up a two way communication between an Arduino UNO with a NRF24L01 module and a RF-NANO. The schematics of my set-up and my code are included below.
On the right side of the diagram, a brushless motor and a MPU6050 gyroscope is attached to an RF-NANO module. On the left side of the diagram, a potentiometer and NRF24L01 module is connected to an Arduino UNO module. I am trying to remotely control the rotation speed of the brushless motor with the potentiometer while wirelessly receiving gyroscopic data from the MPU6050 module, but with the current set-up and programming, it the two-way connection only works sometimes and appears to be unreliable. I haven't found any helpful suggestions besides switching to a steady external power source, which I've done, and I don't really know where else to look for answers. Any sort of advice and criticism is appreciated! (My code is based on the source code on ACBR)
Schematics:
CODE FOR RF-NANO:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
#include <Servo.h>
#define MPU 0x68
#define CE_PIN 10
#define CSN_PIN 9
const byte address[][6] = {"00001", "00002"};
double AcX,AcY,AcZ;
float Pitch, Roll;
float GYRO[2]; // two element array holding the pitch and roll data
int potValue; //value from the analog pin
Servo ESC; //electronic speed controller of brushless motor
RF24 radio(CE_PIN, CSN_PIN); // activate the radio
void setup()
{
Serial.begin(9600);
ESC.attach(9, 1000, 2000);//(pin, min pulse width, max pulse width in miliseconds)
radio.begin();
radio.openWritingPipe(address[1]);
radio.openReadingPipe(1, address[0]);
init_MPU();
}
void loop()
{
delay(5);
radio.stopListening();
radio.write( GYRO, sizeof(GYRO) );
FunctionsMPU(); // returning our accelerometer data AcX, AcY, AcZ.
Roll = FunctionsPitchRoll(AcX, AcY, AcZ);
Pitch = FunctionsPitchRoll(AcY, AcX, AcZ);
GYRO[0] = Pitch;
GYRO[1] = Roll;
delay(5);
radio.startListening();
if (radio.available())
{
radio.read(&potValue, sizeof(potValue));
ESC.write(potValue);
Serial.println(potValue); //send signal to ESC
}
}
void init_MPU()
{
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
delay(1000);
}
double FunctionsPitchRoll(double A, double B, double C)
{
double DatoA, DatoB, Value;
DatoA = A;
DatoB = (B*B) + (C*C);
DatoB = sqrt(DatoB);
Value = atan2(DatoA, DatoB);
Value = Value * 180/3.14;
return (float)Value;
}
void FunctionsMPU()
{
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU,6,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
}
CODE FOR UNO:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
#define CE_PIN 10
#define CSN_PIN 9
const byte address[][6] = {"00001", "00002"};
float GYRO[2]; // two element array holding the pitch and roll data
int potValue; //potentiometer value from the analog pin
RF24 radio(CE_PIN, CSN_PIN); // activate the radio
void setup()
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address[0]);
radio.openReadingPipe(1, address[1]);
}
void loop()
{
delay(5);
radio.startListening();
if ( radio.available() )
{
bool done = false;
while (!done)
{
done = radio.read(GYRO, sizeof(GYRO));
Serial.print(GYRO[0]);
Serial.print("\t");
Serial.println(GYRO[1]);
Serial.print("\n");
}
}
else
{
Serial.println("GYRO Error!");
}
delay(5);
radio.stopListening();
potValue = analogRead(A0);
potValue = map(potValue, 0, 1023, 0, 180); // mapping potentiometer value
Serial.println(potValue);
radio.write(&potValue, sizeof(potValue));
}