Hi there,i am posting here in Motor Section hoping to get a answer...i have allready ask for help in Bluetooth comunicaction Section but i didn't get the answer...i don't know what to do..please help me
Using KAS Android Bluetooth Joystick aplication i am triyng to control a RC car,modifying it and hook up a servo for steering (Horizontal Axis) ,and a DC Motor with TB6612FNG (on Vertical Axis) ...so i took the code and modifying it :
I will try to explain it the clear as possible so can everybody understand it:
The Android aplicacion send to the Arduino via HC-06 the Joystick position.
Using this code now the Servo move as it should (Horizontal Axis) ,
The DC Motor (Vertical Axis of joystick) work both directions FWD /BACKWRD, goes to StandBy when the joystick is in middle , BUT it moves very slow....on the Serial Monitor it goes from -255 0 255 exactly how i move the joystick
#include <Servo.h>
boolean DEBUG = true;
#define pinServo_X 9
#define PWM_Y 3
#define STX 0x02
#define ETX 0x03
#define MAX_Y 255 // PWM limit
#define ZERO_Y 0
#define MIN_Y -255
#define AIN1 4
#define AIN2 5 // AIN1 and AIN2 as Direction pins
#define STBY 7 // StandBy
int i=0;
byte cmd[6] = {0, 0, 0, 0, 0, 0};
Servo myservoX;
Servo myY; // ???? it is not a servo
byte buttonStatus = 0;
byte dataByte = 0;
long previousMillis = 0;
boolean setButtonFeedback = false;
long interval = 1000;
void setup() {
Serial.begin(9600);
myservoX.attach(pinServo_X);
myY.attach(PWM_Y);
pinMode(AIN1,OUTPUT);
pinMode(AIN2,OUTPUT);
pinMode(PWM_Y,OUTPUT);
pinMode(STBY,OUTPUT);
}
void loop() {
if(Serial.available()) { // data received from smartphone
delay(5);
cmd[0] = Serial.read();
if(cmd[0] == STX) {
i=1;
while(Serial.available()) {
cmd[i] = Serial.read();
if(cmd[i] == ETX) {
if(i==2 && cmd[1]>48) break; // Button data
if(i==5 && cmd[1]<3 && cmd[3]<3) break; // Joystick data
}
if(i>5) break;
i++;
}
if(i==5) setServoPosition(cmd); // 5 Bytes
else Serial.println("Communication error");
}
} else {
long currentMillis = millis();
if(setButtonFeedback == true) { // allow momentary button visual effect (500 ms)
previousMillis = currentMillis - interval*0.5;
setButtonFeedback = false;
}
}
delay(5);
}
void setServoPosition(byte data[5]) {
int joyX = (data[1]<<7) + data[2];
int joyY = (data[3]<<7) + data[4];
joyX = joyX - 200; // Offset to avoid
joyY = joyY - 200; // transmitting negative numbers
joyX = map(joyX, -180, 180, 180, 0); // (-180/+180 JBC range)
joyY = map(joyY, -100, +100, -255, 255);
joyY+=ZERO_Y;
joyY = constrain(joyY, MIN_Y, MAX_Y);
myservoX.write(joyX);
myY.write(joyY);
digitalWrite(STBY, HIGH); //disable standby
if(DEBUG) {
//Serial.print(joyX); Serial.print(", ");
Serial.println(joyY);}
if(joyY > 0) {
digitalWrite (AIN1,HIGH);
digitalWrite (AIN2,LOW);
} else if(joyY < 0) {
digitalWrite (AIN1,LOW);
digitalWrite (AIN2,HIGH);
} else if(joyY == 0) {
digitalWrite(STBY, LOW); //enable standby
analogWrite(PWM_Y,joyY);
}
}
I would reay apreciate if you can help me with the code,Thank you