I have this 4wd car platform and Im using these as the Rx and Tx sketches. The car uses an Arduino UNO with an L293D driver shield with a RF433Mhz Rx module. The UNO is powered from a 9V barrel jack and the Motors are powered from 4xAA battery pack. The Rx is a nano with a 433Mhz Rx and an MPU6050 module, powered from a mobile phone battery pack.
The problem
If I tilt the MPU6050 left or right, the car correctly turns either 1 wheel or the other, to make it turn. As soon as I return to the "flat" position it stops turning. But when I tilt the MPU6050 to the back or front, it correctly runs forward or backward but it never stops after that.
Here is the Rx:
And the Tx code:
If you think it necessary to post pictures of the car and wiring I will, but I dont think it would be very clear. Id upload a sketch but I dont think the wiring is the issue because it seems to me its a coding issue rather than a wiring issue.
The problem
If I tilt the MPU6050 left or right, the car correctly turns either 1 wheel or the other, to make it turn. As soon as I return to the "flat" position it stops turning. But when I tilt the MPU6050 to the back or front, it correctly runs forward or backward but it never stops after that.
Here is the Rx:
Code:
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library
#include <SPI.h>
#include <AFMotor.h>
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
//Define packet for the direction (X axis and Y axis)
int data[2];
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor3(3, MOTOR12_64KHZ); // create motor #3, 64KHz pwm
AF_DCMotor motor4(4, MOTOR12_64KHZ); // create motor #4, 64KHz pwm
String str_out;
void setup() {
Serial.begin(9600);
rf_driver.init();
motor1.setSpeed(200); // set the speed to 200/255
motor2.setSpeed(200); // set the speed to 200/255
motor3.setSpeed(200); // set the speed to 200/255
motor4.setSpeed(200); // set the speed to 200/255
}
void loop() {
// Set buffer to size of expected message
int receivedData[3] = {0};
uint8_t buflen = sizeof(receivedData);
if (rf_driver.recv((uint8_t*)receivedData, &buflen)) {
//if data is not an array, use &receivedData
for (byte i = 0; i < 3; i++) {
}
if(receivedData[0] > 380){
left();
Serial.println("LEFT"); //motor back left
}
if(receivedData[0] < 310){
right();
Serial.println("RIGHT"); //motor back right
}
if(receivedData[1] > 180){
forward();
Serial.println("FORWARD"); //motor bl and br direction 1
}
if(receivedData[1] < 110){
backward();
Serial.println("BACKWARD"); //motor bl and br direction 2
}
if(receivedData[0] > 330 && receivedData[0] < 360 && receivedData[1] > 130 && receivedData[1] < 160){
//brake();
stopCar();
Serial.println("STOP");
}
}
}
void forward(){
Serial.println("running forward");
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
}
void backward(){
Serial.println("running backward");
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
}
void left(){
motor3.run(FORWARD);
}
void right(){
motor4.run(FORWARD);
}
void brake(){
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
void stopCar(){
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
Code:
// Include RadioHead Amplitude Shift Keying Library
#include <RH_ASK.h>
// Include dependant SPI Library
#include <SPI.h>
#include "Wire.h" //For communicate
#include "I2Cdev.h" //For communicate with MPU6050
#include "MPU6050.h" //The main library of the MPU6050
//Define the object to access and cotrol the Gyro and Accelerometer (We don't use the Gyro data)
MPU6050 mpu;
int16_t ax, ay, az;
int16_t gx, gy, gz;
//Define packet for the direction (X axis and Y axis)
int data[2];
// Create Amplitude Shift Keying Object
RH_ASK rf_driver;
void setup() {
Serial.begin(9600);
mpu.initialize(); //Initialize the MPU object
// Initialize ASK Object
rf_driver.init();
}
void loop() {
delay(2000);
//With this function, the acceleration and gyro values of the axes are taken.
//If you want to control the car axis differently, you can 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 aX axis data
data[1] = map(ay, -17000, 17000, 100, 200); //Send aY axis data
rf_driver.send((const uint8_t*)data, sizeof(data));
//(uint8_t *)msg, strlen(msg));
rf_driver.waitPacketSent();
Serial.println(data[0]);
Serial.println(data[1]);
}