I need someone to help me with coding my Arduino Uno. I've built a gesture controlled RF robotic car that is controlled by the Uno and a MPU6050 6 axis accelerometer. Everything is wired and working fine. Ive loaded a code onto the arduino that gives me readings from the MPU6050 and im getting constant values that tells me the MPU6050 is working and what direction its in, but im struggling with the coding part of the project. The main basics are MPU to Uno to encoder to Rf transmitter and the receiver is Rf reciver to decoder to l293D motor driver. I'll upload any documents if needed
int xPin=A4; //X axis input
int yPin=A5; //Y axis input
int zPin=A1; //Z axis input(not used)
int Q1=10,Q2=11,Q3=12,Q4=13; //Output pins to be connected to 10, 11, 12, 13 of Decoder IC
long x; //Variabe for storing X coordinates
long y; //Variabe for storing Y coordinates
long z; //Variabe for storing Z coordinates
void setup()
{
Serial.begin(9600);
Serial.println("Initialize MPU6050");
pinMode(Q1,OUTPUT);
pinMode(Q2,OUTPUT);
pinMode(Q3,OUTPUT);
pinMode(Q4,OUTPUT);
}
void loop()
{
x = analogRead(xPin); //Reads X coordinates
y = analogRead(yPin); //Reads Y coordinates
z = analogRead(zPin); //Reads Z coordinates (Not Used)
if(x<5000) // Change the value for adjusting sensitivity
forward();
else if(x>5000) // Change the value for adjusting sensitivity
backward();
else if(y>5000) // Change the value for adjusting sensitivity
right();
else if(y<5000) // Change the value for adjusting sensitivity
left();
else
stop_();
}
void stop_()
{
Serial.println("");
Serial.println("STOP");
digitalWrite(Q1,LOW);
digitalWrite(Q2,LOW);
digitalWrite(Q3,LOW);
digitalWrite(Q4,LOW);
}
void forward()
{
Serial.println("");
Serial.println("Forward");
digitalWrite(Q1,HIGH);
digitalWrite(Q2,LOW);
digitalWrite(Q3,HIGH);
digitalWrite(Q4,LOW);
}
void backward()
{
Serial.println("");
Serial.println("Backward");
digitalWrite(Q1,LOW);
digitalWrite(Q2,HIGH);
digitalWrite(Q3,LOW);
digitalWrite(Q4,HIGH);
}
void left()
{
Serial.println("");
Serial.println("Left");
digitalWrite(Q1,LOW);
digitalWrite(Q2,HIGH);
digitalWrite(Q3,HIGH);
digitalWrite(Q4,LOW);
}
void right()
{
Serial.println("");
Serial.println("Right");
digitalWrite(Q1,HIGH);
digitalWrite(Q2,LOW);
digitalWrite(Q3,LOW);
digitalWrite(Q4,HIGH);
}
This is what i've written so far but when i go into serial monitor all it reads is a single variable forward while the MPU6050 is placed flat. Im unsure if what i have written is at all correct.
Appologies Jr, im unsure of how to delete my other post ?
Wow ok thanks so Detla_G the problem that im stuck with is that ive put a value test onto the Arduino Uno and it gives me a bunch of readings way over 1023 as shown below:
These values came from this code:
Its just an example code that gives me the real time readings from the MPU6050
There is a bunch of these example codes and this one is the one ive got on the Arduino Uno
#include<Wire.h>
const int MPU_addr=0x68; // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true); // request a total of 14 registers
AcX=Wire.read()<<8|Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY=Wire.read()<<8|Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ=Wire.read()<<8|Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp=Wire.read()<<8|Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX=Wire.read()<<8|Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY=Wire.read()<<8|Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ=Wire.read()<<8|Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
Serial.print("AcX = "); Serial.print(AcX);
Serial.print(" | AcY = "); Serial.print(AcY);
Serial.print(" | AcZ = "); Serial.print(AcZ);
Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53); //equation for temperature in degrees C from datasheet
Serial.print(" | GyX = "); Serial.print(GyX);
Serial.print(" | GyY = "); Serial.print(GyY);
Serial.print(" | GyZ = "); Serial.println(GyZ);
delay(333);
}
So ive edited my first application code, based on the Wire.h library do you think this will work?
int xPin=A4; //X axis input
int yPin=A5; //Y axis input
int zPin=A1; //Z axis input(not used)
int Q1=10,Q2=11,Q3=12,Q4=13; //Output pins to be connected to 10, 11, 12, 13 of Decoder IC
long x; //Variabe for storing X coordinates
long y; //Variabe for storing Y coordinates
long z; //Variabe for storing Z coordinates
#include <Wire.h>
void setup()
{
delay(100) //allow sensor to get ready
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
pinMode(Q1,OUTPUT);
pinMode(Q2,OUTPUT);
pinMode(Q3,OUTPUT);
pinMode(Q4,OUTPUT);
}
void loop()
{
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
x = Wire.read(xPin); //Reads X coordinates
y = Wire.read(yPin); //Reads Y coordinates
z = Wire.read(zPin); //Reads Z coordinates (Not Used)
if(x<500) // Change the value for adjusting sensitivity
forward();
else if(x>500) // Change the value for adjusting sensitivity
backward();
else if(y>500) // Change the value for adjusting sensitivity
right();
else if(y<500) // Change the value for adjusting sensitivity
left();
else
stop_();
}
void stop_()
{
Serial.println("");
Serial.println("STOP");
digitalWrite(Q1,LOW);
digitalWrite(Q2,LOW);
digitalWrite(Q3,LOW);
digitalWrite(Q4,LOW);
}
void forward()
{
Serial.println("");
Serial.println("Forward");
digitalWrite(Q1,HIGH);
digitalWrite(Q2,LOW);
digitalWrite(Q3,HIGH);
digitalWrite(Q4,LOW);
}
void backward()
{
Serial.println("");
Serial.println("Backward");
digitalWrite(Q1,LOW);
digitalWrite(Q2,HIGH);
digitalWrite(Q3,LOW);
digitalWrite(Q4,HIGH);
}
void left()
{
Serial.println("");
Serial.println("Left");
digitalWrite(Q1,LOW);
digitalWrite(Q2,HIGH);
digitalWrite(Q3,HIGH);
digitalWrite(Q4,LOW);
}
void right()
{
Serial.println("");
Serial.println("Right");
digitalWrite(Q1,HIGH);
digitalWrite(Q2,LOW);
digitalWrite(Q3,LOW);
digitalWrite(Q4,HIGH);
}