I currently have 2 cytron shield-md10 r2s that are stacked on top of each other with 2 different motors connected. I have an Arduino uno r2 at the base. I used jumper wires to connect bend the pin to the 2nd motor shield from pins 3 and 4 to 5 and 6. I am trying to get the motors to reverse however with the code I have currently it only goes forward with a positive number and with a negative number it does nothing. I currently have a joystick which I am trying to get direction and speed control from. My bad for my terrible picture. Is there something wrong with my code?
#define joystick1 A0
#define joystick2 A1
int in1 = 3;
int in2 = 4;
int in3 = 5;
int in4 = 6;
int motorSpeedA;
int motorSpeedB;
void setup() {
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
Serial.begin(9600);
}
void loop() {
int xAxis = analogRead(A1);
int yAxis = analogRead(A0);
if (yAxis < 470) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
motorSpeedA = map(yAxis, 470, 0, 0, -255);
motorSpeedB = map(yAxis, 470, 0, 0, -255);
}
else if (yAxis > 550) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
motorSpeedA = map(yAxis, 550, 1000, 0, 255);
motorSpeedB = map(yAxis, 550, 1000, 0, 255);
}
else {
motorSpeedA = 0;
motorSpeedB = 0;
}
if (xAxis < 470) {
int xMapped = map(xAxis, 470, 0, 0, 255);
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
}
}
if (xAxis > 550) {
int xMapped = map(xAxis, 550, 1023, 0, -255);
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
if (motorSpeedA > 255) {
motorSpeedA = -255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
}
}
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}
analogWrite(in1, motorSpeedA);
analogWrite(in3, motorSpeedB);
}