For this project, I was using an Elegoo Car V3 and I was trying to add a gyroscope onto it. When I run the code for the car, my loop code iterates once and it is always able to read the sensor data. But after that, the program starts hanging. I traced back the problem to the Wire.endTransmission(false) line in my code. For some reason, that line was not completing the second time.
I spent a few hours trying to find a solution online. I triple checked my wiring and still, I could not find a solution to this problem.
Thank you for taking the time to help me!
This is the output of the program
RawX= 82
RawY= 241
RawZ= -1
-----------------------------------------
AngleX= 195.41
AngleY= 216.93
AngleZ= 200.14
-----------------------------------------
And this is the code
#include <Wire.h>
const int MPU_addr = 0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int minVal = 265;
int maxVal = 402;
double x;
double y;
double z;
int c = 0;
//www.elegoo.com
// Right motor truth table
//Here are some handy tables to show the various modes of operation.
// ENB IN3 IN4 Description
// LOW Not Applicable Not Applicable Motor is off
// HIGH LOW LOW Motor is stopped (brakes)
// HIGH LOW HIGH Motor is on and turning forwards
// HIGH HIGH LOW Motor is on and turning backwards
// HIGH HIGH HIGH Motor is stopped (brakes)
// define IO pin
#define ENA 5
#define ENB 6
#define IN1 7
#define IN2 8
#define IN3 9
#define IN4 11
//set car speed
#define CAR_SPEED 100
typedef unsigned char u8; //Change the name of unsigned char to u8
void forward(u8 car_speed)
{
analogWrite(ENA, car_speed);
analogWrite(ENB, car_speed);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void back(u8 car_speed)
{
analogWrite(ENA, car_speed);
analogWrite(ENB, car_speed);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void left(u8 car_speed)
{
analogWrite(ENA, 250);
analogWrite(ENB, 250);
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
void right(u8 car_speed)
{
analogWrite(ENA, 250);
analogWrite(ENB, 250);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
void stop()
{
digitalWrite(ENA, LOW);
digitalWrite(ENB, LOW);
}
int getGyroData() {
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,6,true);
AcX = Wire.read()<<8|Wire.read();
AcY = Wire.read()<<8|Wire.read();
AcZ = Wire.read()<<8|Wire.read();
int xAng = map(AcX, minVal, maxVal,-90, 90);
int yAng = map(AcY, minVal, maxVal,-90, 90);
int zAng = map(AcZ, minVal, maxVal,-90, 90);
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
Serial.print("AngleX= ");
Serial.println(x);
Serial.print("AngleY= ");
Serial.println(y);
Serial.print("AngleZ= ");
Serial.println(z);
Serial.println("-----------------------------------------");
}
void turn(int degrees) {
int *originalData;
originalData = getGyroData();
int originalTurn = originalData[2];
bool notDone = true;
while (notDone) {
int *newVal;
newVal = getGyroData();
int newTurn = newVal[2];
int change = originalTurn - newTurn;
Serial.println(change);
}
}
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
pinMode(IN1, OUTPUT);//before useing io pin, pin mode must be set first
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
Serial.begin(9600);
}
void loop(){
c++;
Serial.println(c);
Wire.beginTransmission(MPU_addr);
Serial.println(c);
Wire.write(0x43);
Serial.println(c);
Wire.endTransmission(false);
Serial.println(c);
Wire.requestFrom(MPU_addr,4,true);
AcX = Wire.read()<<8|Wire.read();
AcY = Wire.read()<<8|Wire.read();
AcZ = Wire.read()<<8|Wire.read();
Serial.println(c);
Serial.print("RawX= ");
Serial.println(AcX);
Serial.print("RawY= ");
Serial.println(AcY);
Serial.print("RawZ= ");
Serial.println(AcZ);
Serial.println("-----------------------------------------");
int xAng = map(AcX, minVal, maxVal,-90, 90);
int yAng = map(AcY, minVal, maxVal,-90, 90);
int zAng = map(AcZ, minVal, maxVal,-90, 90);
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
Serial.print("AngleX= ");
Serial.println(x);
Serial.print("AngleY= ");
Serial.println(y);
Serial.print("AngleZ= ");
Serial.println(z);
Serial.println("-----------------------------------------");
delay(500);
}
This is a schematic of the car I found
