I saw this library and wanted to see if I could use it.
The hardware is already done. I have an arduino leonardo, a driver for my motor along with a motor and also a rotary encoder. I just can't figure out how to use it to actually drive the motors. I don't really get what the pins do in the example code.
#include "Joystick.h"
//X-axis & Y-axis REQUIRED
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
JOYSTICK_TYPE_MULTI_AXIS, //JOYSTICK, GAMEPAD, MULTI_AXIS
4, 0, //Button Count, Hat Switch Count
true, true, true, //X,Y,Z
false, false, false, //Rx,Ry,Rz
false, false, false, //Rudder,Throttle,Accelerator
false, false); //Brake,Steering
Gains mygains[2];
EffectParams myeffectparams[2];
int32_t forces[2] = {0};
void setup() {
pinMode(A2, INPUT);
pinMode(9, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
//Joystick.setXAxisRange(0, 1023);
//Steering wheel
Joystick.setXAxisRange(-512, 512);
//set X Axis gains
mygains[0].totalGain = 100;//0-100
mygains[0].springGain = 100;//0-100
//enable gains REQUIRED
Joystick.setGains(mygains);
Joystick.begin();
}
void loop() {
int value = analogRead(A2);
//set X Axis Spring Effect Param
//myeffectparams[0].springMaxPosition = 1023;
//myeffectparams[0].springPosition = value;//0-1023
//Steering wheel
myeffectparams[0].springMaxPosition = 512;
myeffectparams[0].springPosition = value - 512; //-512-512
//Send HID data to PC
//Joystick.setXAxis(value);
//Recv HID-PID data from PC and caculate forces
//Steering wheel
Joystick.setXAxis(value - 512);
Joystick.setEffectParams(myeffectparams);
Joystick.getForce(forces);
if (forces[0] > 0) {
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
analogWrite(9, abs(forces[0]));
} else {
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
analogWrite(9, abs(forces[0]));
}
delay(1);
}
I thought 6 and 7 are for the motor but I'm not sure since they don't really change their values.
If anyone knows what I should do to make it work or a different library for FFB then that would be awesome.