So for this project I'm working on I'm trying to map out two motors onto a single joystick using two L293D H-bridges, I feel like there is something in my code that I'm missing here. I tried modeling this off of the sketch from Jeremy Blum's Exploring Arduino book from page 78, however this sketch uses a potentiometer instead of a joystick but I feel the general aspects should carry over. I also need yet to map out the x-axis of the joystick input to enable turning.
#include <Wire.h>
#include <Servo.h>
// i2c address of the mpu‐6050
const int MPU_ADDR = 0x68;
// variables
int16_t xAccel, yAccel, zAccel, temp, xGyro, yGyro, zGyro;
const int TILT = 9;
const int G = 10;
Servo myServo;
Servo Myservo;
int val = 0;
int v = 0;
int VRy = A1; // Read Analog value
int VRx = A0;
int SW = 12;
int yPos = 0;
int xPos = 0;
signed char SW_state = 0;
signed char mapY = 0; // This will be used for mapping the values to limited values for deciding position
signed char mapX = 0;
const int EN = 5; // right side
const int MC1 = 8;
const int MC2 = 2;
const int EN2 = 3; //left side
const int MC3 = 4;
const int MC4 = 7;
int velocity = 0;
void setup()
{
// initialize the i2c library
Wire.begin();
// begin communication with the mpu‐6050
Wire.beginTransmission(MPU_ADDR);
// write to the power management register
Wire.write(0x6B);
// wake up the mpu‐6050
Wire.write(0);
// end communication with the mpu‐6050
Wire.endTransmission(true);
// initialize the serial port
Serial.begin(9600);
myServo.attach(TILT);
Myservo.attach(G);
pinMode(VRy, INPUT);
pinMode(VRx, INPUT);
pinMode(SW, INPUT_PULLUP);
//constrain(mapY, -512,512);
//constrain(mapX, -512, 512);
pinMode(EN, OUTPUT);
pinMode(MC1, OUTPUT);
pinMode(MC2, OUTPUT);
pinMode(EN2, OUTPUT);
pinMode(MC3, OUTPUT);
pinMode(MC4, OUTPUT);
brake();
}
void loop()
{
// read the mpu data
//readMpuData();
/*
* put your code for the digital letter scale here
*/
// delay
//delay(500);
// begin communication with the mpu‐6050
Wire.beginTransmission(MPU_ADDR);
// write to the first data register
Wire.write(0x3B);
// end communication
Wire.endTransmission(false);
// request fourteen register reads from the mpu‐6050
Wire.requestFrom(MPU_ADDR, 14, true);
// read registers 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
xAccel = Wire.read() << 8 | Wire.read();
// read registers 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
yAccel = Wire.read() << 8 | Wire.read();
// read registers 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
zAccel = Wire.read() << 8 | Wire.read();
// read registers 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
temp = Wire.read() << 8 | Wire.read();
// convert the temperature to celsius
temp = ((temp / 340.00) + 36.53);
// read registers 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
xGyro = Wire.read() << 8 | Wire.read();
// read registers 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
yGyro = Wire.read() << 8 | Wire.read();
// read registers 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)
zGyro = Wire.read() << 8 | Wire.read();
// print out the results
Serial.print("AX = "); Serial.print(xAccel); // X Raw Value
Serial.print(" | AY = "); Serial.print(yAccel); // Y Raw Value
Serial.print(" | AZ = "); Serial.print(zAccel); // Z Raw Value
Serial.print(" | C = "); Serial.print(temp);
Serial.print(" | GX = "); Serial.print(xGyro);
Serial.print(" | GY = "); Serial.print(yGyro);
Serial.print(" | GZ = "); Serial.println(zGyro);
delay(50);
yAccel = map(yAccel, 750, 11000, 0, 190);
myServo.write(yAccel);
delay(50);
if (val > 0){
Myservo.write(90);
delay(10);
}
else if (val = 0){
Myservo.write(0);
delay(10);
}
yPos = analogRead(VRy);
xPos = analogRead(VRx);
if (yPos > 562){
velocity = map(yPos, 563, 1023, 0, 255);
forward(velocity);
}
else if (yPos < 462){
velocity = map(yPos, 461, 0, 0, 255);
reverse(velocity);
}
else{
brake();
}
//yPos = analogRead(VRy); // This will read Vertical Motion value
//SW_state = digitalRead(SW);
//mapY = map(yPos, 0, 1023, -512, 512); // here joystic's mean position is 512 which is mapped to 0
// position 0 (Extreme bottom) is mapped to -512
// position 1023 (Extreme Up) is mapped to 512
}
void readMpuData()
{
}
void forward (int rate)
{
digitalWrite(EN, LOW);
digitalWrite(MC1, HIGH);
digitalWrite(MC2, LOW);
analogWrite(EN, rate);
digitalWrite(EN2, LOW);
digitalWrite(MC3, HIGH);
digitalWrite(MC4, LOW);
analogWrite(EN2, rate);
}
void reverse (int rate)
{
digitalWrite(EN, LOW);
digitalWrite(MC1, LOW);
digitalWrite(MC2, HIGH);
analogWrite(EN, rate);
digitalWrite(EN2, LOW);
digitalWrite(MC3, LOW);
digitalWrite(MC4, HIGH);
analogWrite(EN2, rate);
}
void brake ()
{
digitalWrite(EN, LOW);
digitalWrite(MC1, LOW);
digitalWrite(MC2, LOW);
digitalWrite(EN, HIGH);
digitalWrite(EN2, LOW);
digitalWrite(MC3, LOW);
digitalWrite(MC4, LOW);
digitalWrite(EN2, HIGH);
}