How can I make a FFB steering wheel in terms of code?

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.

Break down the problem into individual steps, and master each one separately. For example, you could start by figuring out how to drive the motor. The second step would be to learn how to read the encoder, and eventually, to implement speed control.

There are many examples and tutorials for each of those individual steps on the web.

What I meant was i don't know what the pins in the code are supposed to do. When I tried to test it, forces didn't ever change. Now I tried changing some things and Windows recognizes it now. Apparently it wouldn't recognize multi axes.
On GitHub it explains how to set it up internally but then fails to mention what the pins in the example code are specifically for. So I thought maybe someone here used this library themselves and knows how to set it up.

  pinMode(A2, INPUT); // potentiometer for steering wheel
  pinMode(9, OUTPUT); // PWM for intensity of feedback for driver module
  pinMode(6, OUTPUT); // next two pins are to setup direction of rotation for driver module
  pinMode(7, OUTPUT); 

Oh wow, yeah that makes sense. I'm way too tired if I didn't see this myself. Changed some stuff since I'm using an encoder but now I know to put 9 on motor driver enable. Thanks a lot.

Hey, I’m using a bts7690 motor driver and still can’t wrap my head around these connections. Pins 6 and 7 go to R_EN and L_EN, right? As they control the direction of rotation. Still got two PWM pins: L and R. Which one connects to pin 9 output? Can you share your final code and the connection layout please? Thanks in advance

You should probably make your own topic because this one was already answered. In my case I had an L298N so I can't help too much, but here maybe this helps with connections a bit:


And also: https://www.instructables.com/Motor-Driver-BTS7960-43A/

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.