Close
0%
0%

Arduino communicates with Wireless Joystick

Similar projects worth following
Me USB Host is an adapter for USB devices. You can use it to connect a Arduino board to a USB device, such as a USB joystick, a mouse, or a thumb drive. So you can control your robot with your own joystick or something else. Now, we can add new controller method for mBot using wireless joystick.

Me USB Host's Features:

  • Main chip: CH375B
  • Supported device: HID device whose descriptor is shorter than 64 bytes
  • default baudrate is 9600

mCore ( Arduino Uno based ) can communicate to me usb host with software serial, because there is not RJ25 Port for hardware serial in mCore.

the processing : DC Motor <-- mCore <--> USB Host <--> USB 2.4G Dongle <--> Wireless Joystick

  • 1 × mBot kit a robot car for education from makeblock
  • 1 × Me USB Host an adapter for usb device from makeblock
  • 1 × 2.4G Wireless Joystick

  • Simple Code for driving a DC Motor

    indream08/16/2015 at 13:59 0 comments

    #include "MeUsb.h"
    MeUsb usb(10,9); //use software serial, setting up pin for tx,rx
    
    void setup() 
    {
       Serial.begin(9600); 
       usb.init(USB1_0);
       //initialize USB Host
    }
    void loop()
    { 
      if(!usb.device_online)
      {
        usb.probeDevice(); 
        //poll for usb device's connection
        delay(100);
      }
      else
      {
        //received data from usb device
        int len = usb.host_recv();
        if(len>4){
          parseJoystick(usb.RECV_BUFFER);
        }
      }
    }
    void parseJoystick(unsigned char * buf)
    {
          //parse joystick's data
          uint8_t buttonCode = buf[4]&0xff;
          uint8_t buttonCode_ext = buf[5]&0xff;
          uint8_t joystickCodeL_V = buf[3]&0xff; //top 0 bottom ff
          uint8_t joystickCodeL_H = buf[2]&0xff; //left 0 right ff
          uint8_t joystickCodeR_V = buf[1]&0xff; //top 0 bottom ff
          uint8_t joystickCodeR_H = buf[0]&0xff; //left 0 right ff
          uint8_t directionButtonCode = (buttonCode&0xf);
          uint8_t rightButtonCode = (buttonCode&0xf0)>>4;
          switch(directionButtonCode){
           ...
            case 2:{
              //motor run clockwise
              runMotor(MOTOR_1,100);
              runMotor(MOTOR_2,100);
              break;
            }
           ...
            case 6:{
              //motor run counterclockwise
              runMotor(MOTOR_1,-100);
              runMotor(MOTOR_2,-100);
              break;
            }
            ...
            default:{
              // release;
              runMotor(MOTOR_1,0);
              runMotor(MOTOR_2,0);
          }
        }
     } 
     void runMotor(int motor,int speed){
        //dc motor driver
          int _dirPin;
          int _pwmPin;
          if(motor==MOTOR_1){
            _dirPin = 7;
            _pwmPin = 6;
          }else if(motor==MOTOR_2){
            _dirPin = 4;
            _pwmPin = 5;
          }
          pinMode(_dirPin,OUTPUT);
          pinMode(_pwmPin,OUTPUT);
          digitalWrite(_dirPin,speed>0);
          analogWrite(_pwmPin,abs(speed));
    }

View project log

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates