Hello! So I have been trying to get a remote car to move wirelessly via an nRF24L01 using an MPU6050's accelerometer data. However, I don't understand why the values I'm getting are not moving the car.
This is the code for the glove to transmit the MPU6050's x and y axis.
Libraries: I2Cdev i2cdevlib/Arduino/I2Cdev at master · jrowberg/i2cdevlib · GitHub
MPU6050 i2cdevlib/Arduino/MPU6050 at master · jrowberg/i2cdevlib · GitHub
RF24 GitHub - nRF24/RF24: OSI Layer 2 driver for nRF24L01 on Arduino & Raspberry Pi/Linux Devices
//Glove Transmitter.
#include <SPI.h> //SPI library for communicate with the nRF24L01+
#include <RF24.h> //The main library of the nRF24L01+
#include <Wire.h> //For communicate
#include <I2Cdev.h> //For communicate with MPU6050
#include <MPU6050.h> //The main library of the MPU6050
//Defining the object to access and control the Gyro and Accelerometer (Gyro data not in use.)
MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;
//Defining packet for the direction (X axis and Y axis)
int data[2];
//Defining objects from RF24 library - 9 and 10 are a digital pin numbers to which signals CE and CSN are connected.
RF24 radio(9,10);
//Creating a pipe addresses for the communicate
const uint64_t pipe = 0xE8E8F0F0E1LL;
void setup(void){
Serial.begin(9600);
Wire.begin();
mpu.initialize(); //Initialize the MPU object
radio.begin(); //Start the nRF24 communicate
radio.openWritingPipe(pipe); //Sets the address of the receiver to which the program will send data.
}
void loop(void){
//With this function, the acceleration and gyro values of the axes are taken.
//To control the car axis differently, it is possible to change the axis name in the map command.
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
//In two-way control, the X axis (data [0]) of the MPU6050 allows the robot to move forward and backward.
//Y axis (data [0]) allows the robot to right and left turn.
data[0] = map(ax, -17000, 17000, 300, 400); //Send X axis data
data[1] = map(ay, -17000, 17000, 100, 200); //Send Y axis data
radio.write(data, sizeof(data));
Serial.println(data[0]);
Serial.println(data[1]);
delay(5000);
}
And here is the code for the car receiving the data and moving according to it.
//Car Receiver.
#include <SPI.h> //SPI library for communicate with the nRF24L01+
#include "RF24.h" //The main library of the nRF24L01+
//Define enable pins of the Motors
const int enbA = 3; //Enabling Back Motors.
const int enbB = 5; //Enabling Front Motors.
//Define control pins of the Motors
//If the motors rotate in the opposite direction, you can change the positions of the following pin numbers
const int IN1 = 4; //Back Motor (Spinning counter-clockwise -)
const int IN2 = 2; //Back Motor (Spinning clockwise +)
const int IN3 = 7; //Front Motor (Spinning clockwise+)
const int IN4 = 6; //Front Motor (Spinning counter-clockwise-)
//Define variable for the motors speeds
//I have defined a variable for each of the two motors
//This way you can synchronize the rotation speed difference between the two motors
int FrontSpeed = 130;
int BackSpeed = 255;
//Define packet for the direction (X axis and Y axis)
int data[2];
//Define object from RF24 library - 9 and 10 are a digital pin numbers to which signals CE and CSN are connected
RF24 radio(9,10);
//Create a pipe addresses for the communicate. The pipe addresses allow two arduino's to communicate with a given
//frequency address. Usually an arduino can act as 6 different recievers, so pairing the car's receiver address with
//the glove's transmitter address, it specifies that they are connected to the same frequency.
const uint64_t pipe = 0xE8E8F0F0E1LL;
void setup(){
//Define the motor pins as OUTPUT
pinMode(enbA, OUTPUT);
pinMode(enbB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
Serial.begin(9600);
radio.begin(); //Start the nRF24 communicate
radio.openReadingPipe(1, pipe); //Sets the address of the transmitter to which the program will receive data.
radio.startListening(); //This allows the arduino to recieve data from the transmitter.
}
void loop(){
if (radio.available()){
radio.read(data, sizeof(data));
//This will let the car drive forward.
if(data[0] > 380){
analogWrite(enbA, BackSpeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
}
//This will let the car drive backwards.
if(data[0] < 310){
analogWrite(enbA, FrontSpeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
}
//This will let the car turn left.
if(data[1] > 180){
analogWrite(enbA, FrontSpeed);
analogWrite(enbB, BackSpeed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
//This will let the car turn right.
if(data[1] < 110){
analogWrite(enbA, FrontSpeed);
analogWrite(enbB, BackSpeed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
//Stopping the car.
if(data[0] > 330 && data[0] < 360 && data[1] > 130 && data[1] < 160){
analogWrite(enbA, 0);
analogWrite(enbB, 0);
}
Serial.println(data[0]);
Serial.println(data[1]);
delay(2000);
}
}
Both of the codes are running on two separate Arduino Uno R3's. Everything is connected properly as I have checked the wiring very thoroughly.
The first code gets data from the MPU6050 and sends it to the receiving nRF24L01, where the second code takes the value and activates the motors to move the car. The problem is that, although I am getting the values from point A to B, it's not moving the car. so I believe it possibly has something to do with the if statements I am making.