TB6612FNG via Bluetooth


hi there,i posting here in motor section hoping answer...i have allready ask in bluetooth comunicaction section didn't answer...i don't know do..please me
using kas android bluetooth joystick aplication  am triyng control rc car,modifying , hook servo steering (horizontal axis) ,and dc motor tb6612fng (on vertical axis) ...so took code , modifying :
i try explain clear possible can understand it:
the android aplicacion send arduino via hc-06 joystick position.
using code servo move should (horizontal axis) ,
the dc motor (vertical axis of joystick) work both directions fwd /backwrd, goes standby when joystick in middle , moves slow....on serial monitor goes -255   0  255 how move joystick
code: [select]


#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 , ain2 direction pins
#define stby  7   // standby

int i=0;
byte cmd[6] = {0, 0, 0, 0, 0, 0};
servo myservox;                    
servo myy;                 // ???? not 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 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 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 reay apreciate if can me code,thank you



Arduino Forum > Using Arduino > Motors, Mechanics, and Power (Moderator: fabioc84) > TB6612FNG via Bluetooth


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support