#include "MeUsb.h"
MeUsb usb(10,9);
void setup()
{
Serial.begin(9600);
usb.init(USB1_0);
}
void loop()
{
if(!usb.device_online)
{
usb.probeDevice();
delay(100);
}
else
{
int len = usb.host_recv();
if(len>4){
parseJoystick(usb.RECV_BUFFER);
}
}
}
void parseJoystick(unsigned char * buf)
{
uint8_t buttonCode = buf[4]&0xff;
uint8_t buttonCode_ext = buf[5]&0xff;
uint8_t joystickCodeL_V = buf[3]&0xff;
uint8_t joystickCodeL_H = buf[2]&0xff;
uint8_t joystickCodeR_V = buf[1]&0xff;
uint8_t joystickCodeR_H = buf[0]&0xff;
uint8_t directionButtonCode = (buttonCode&0xf);
uint8_t rightButtonCode = (buttonCode&0xf0)>>4;
switch(directionButtonCode){
...
case 2:{
runMotor(MOTOR_1,100);
runMotor(MOTOR_2,100);
break;
}
...
case 6:{
runMotor(MOTOR_1,-100);
runMotor(MOTOR_2,-100);
break;
}
...
default:{
runMotor(MOTOR_1,0);
runMotor(MOTOR_2,0);
}
}
}
void runMotor(int motor,int speed){
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));
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.