Hey Guys,
I hope you can help me shed some light on my project.
I have an Arduino Unu R2 + USB host Shield + Bluetooth Dongle + Ps3 controller
I'm trying to make a remote controlled tank (tamyia dual motor gearbox + tamyia chasis) using the above + L293D motor driver for this . The l293d is connected to a proto board on top of the usb hoist shield and i've checked there are no pin errors.
Actually the thing is i've managed to put it all together and it "sort of " works. Sort of because the tank will move forward when i move both joysticks forward, backward when i move the joysticks backwards and controll each track sepparetly when i move just one joystick. Weird thing is that when i try to go in reverse with one track or try to make the tank spin around his axis the motors stall, like the PWM value is to low. This happens with both tracks, and only in these situations. After i try one of the above (reverse or axis spin) each motor taken seprately moves forward ok but if i try it again with both of them it stalls even in forward now .
I've tried to serial print the values that i'm getting when using my code, but when i try to add serial print to my code the PS3 controller just won't connect to the dongle anymore. When the serial print is removed from the code the controller connects in 2 seconds. I've tried 5 different dongles so i know that isn't the issue but anyway i don't think the dongle is the problem since it always connects as long as i don't have the serial print in the sketch.
i've pasted my code bellow, maybe you can take a look and let me knopw if i did something wrong (total noob in programming so i won't be amazed if this is the problem):
#include <PS3BT.h>
USB Usb;
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
PS3BT PS3(&Btd); // This will just create the instanceint motor1Pin = 2; // H-bridge leg 1
int motor2Pin = 4; // H-bridge leg 2
int speedPin = 5; // H-bridge enable pinint speedPin2 = 3 ; //H-bridge enable pin 2
int motor1Pin2 = A0 ; //H-bridge leg 1 for 2nd motor
int motor2Pin2 = A1 ; //H-bridge leg 2 for 2nd motorint j = 0;
int k = 0;void setup() {
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(speedPin, OUTPUT);pinMode(motor1Pin2, OUTPUT);
pinMode(motor2Pin2, OUTPUT);
pinMode(speedPin2, OUTPUT);Serial.begin(115200);
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while(1); //halt
}
Serial.print(F("\r\nPS3 Bluetooth Library Started"));
}void loop() {
Usb.Task();
int Ly = (map(PS3.getAnalogHat(LeftHatY), 0, 255, 0, 1023));
int Ry = (map(PS3.getAnalogHat(RightHatY), 0, 255, 0, 1023));
j = Ly - 517; // 120 is center positions - how far from center
j = abs(j);
if (j >= 510){
j = 510; //the most the PWM pin can do is 255
}
if (j <=200 && j>=10){
j=200; //below 100 PWM the motor makes a high pich sound and does not move
}
if (j <=10){
j=0; // below 10 the joystick is very close to center
}if
(Ly >= 630 ){
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
analogWrite (speedPin, j/2);}
if
(Ly <= 450){
digitalWrite(motor1Pin, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin, LOW); // set leg 2 of the H-bridge low
analogWrite (speedPin, j/2);
}k = Ry - 517; // 120 is center positions - how far from center
k = abs(k);
if (k >= 510){
k = 510; //the most the PWM pin can do is 255
}
if (k <=200 && k>=10){
k=200; //below 100 PWM the motor makes a high pich sound and does not move
}
if (k <=10){
k=0; // below 10 the joystick is very close to center
}if
(Ry >= 630){
digitalWrite(motor1Pin2, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin2, HIGH); // set leg 2 of the H-bridge high
analogWrite (speedPin2, k/2);}
if
(Ry <= 450){
digitalWrite(motor1Pin2, HIGH); // set leg 1 of the H-bridge high
digitalWrite(motor2Pin2, LOW); // set leg 2 of the H-bridge low
analogWrite (speedPin2, k/2);
}if ((Ly <=600 && Ly >= 450) && (Ry <=600 && Ry >= 450)) {
analogWrite(speedPin, 0); // turn off if the joysticks is in the center
analogWrite(speedPin2, 0);
//if (Ry <=600 && Ry >= 450) {
//analogWrite(speedPin2, 0); // turn off if the koystick is in the center
}//Serial.print(Ly); // send numbers to PC so you can see what it going on
//Serial.print(",");
//Serial.println(j);
//delay (200);
}