Introduction: MBot Controlled by Wireless Joystick Using Me USB Host
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.
Step 1: Prepare
Step 2: Connection
USB Host's Features:
1、Main chip: CH375B
2、Supported device: HID device whose descriptor is shorter than 64 bytes
3、default baudrate is 9600
mCore 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
Step 3: Coding
simple code for testing dc motor controll
#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)); }
Step 4: Full Example&USB Library for Arduino
full source code for example: https://github.com/xeecos/Me-USB-Host