Diy arduino handbrake

Hey, I was trying to make a DIY handbrake a while back but it never really worked out and yesterday I remembered the project and wanted to finish it. I tried to find code for the Joystick.h library to simulate a joystick button press but I couldn't find anything so I'm here asking for help thanks in advance.

Adam

You could try putting bricks under the tires. Did not the joystick library come with example sketches? After you install most libraries, the example sketches are automatically available from the IDE menu.

well i did try to find something but the closest i got was this

#include <Joystick.h>

Joystick_ Joystick;

void setup() {
// Initialize Button Pins
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
pinMode(11, INPUT_PULLUP);
pinMode(12, INPUT_PULLUP);

// Initialize Joystick Library
Joystick.begin();
}

// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 9;

// Last state of the button
int lastButtonState[4] = {0,0,0,0};

void loop() {

// Read pin values
for (int index = 0; index < 4; index++)
{
int currentButtonState = !digitalRead(index + pinToButtonMap);
if (currentButtonState != lastButtonState[index])
{
Joystick.setButton(index, currentButtonState);
lastButtonState[index] = currentButtonState;
}
}

delay(50);
}

but this code is made for 4 buttons and it didnt really work with 1.

This kind of reply is not helpful. What happened, or didn't happen? Where did you find the sketch? Also, please edit your post and place the code inside code tags, in accordance with the forum protocol.

the code was uploaded successfully but when I checked if the code was working and if the pc was recognizing the button nothing happened. I found the code in the joystick library under the name "Joystick button"

#include <Joystick.h>

Joystick_ Joystick;

void setup() {
  // Initialize Button Pins
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);

  // Initialize Joystick Library
  Joystick.begin();
}

// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 9;

// Last state of the button
int lastButtonState[4] = {0,0,0,0};

void loop() {

  // Read pin values
  for (int index = 0; index < 4; index++)
  {
    int currentButtonState = !digitalRead(index + pinToButtonMap);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
  }

  delay(50);
} 

Thanks. Please show a diagram of your wiring.

It's very useful for you to go to "device manager" and look for the gamepad the arduino is emulating/simulating.
You can then click on "properties"

It can show you something like this


(the axis and buttons available can be changed with the Joystick Library).

And from here you can test if the buttons are reporting, and else.

I have used the Joystick library myself and, well, didn't quite like it. It packed a lot of helpful stuff, but I feel like it's overly extraneous for a library that, well, is supposed to be the interface between code (or Arduino) and the computer. Mostly compounded by my own centering and calibrating code (plus some others).

...and we are ghosted...

image

this it the wiring diagram(i wanted only one button ofc)

I have been using the task manager and the buttons didn't work(they were always off)
I think that the Joystick library is just unnecacearly complicated therefore im here

Your code doesn't reference the pin D15 that the button is connected to.

Hi,
Can I suggest you write some simple code to test your joystick and button inputs via the IDE serial monitor?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

yes that is true but i changed it before trying of course

i have tested it now as you suggested and the button works as expected with no problems therefore im trying to find out how to write the correct code for the appliance

Does that mean the problem is solved, and you don't need further help? If not, you need to post the test code that you wrote.

no, the button doesn't work correctly cause I was responding to Mr Tom and he asked me to test it with the serial monitor and so I did but the joystick part still doesn't work

I don't see the test code you wrote...

If I recall correctly you need to tell the joystick library to "upload" its current state to the computer

Look at line 437 of my code

And also, I would prefer you to use the advanced constructor (for the joystick object)
at line 33:

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID, JOYSTICK_TYPE_GAMEPAD,
                   14, 2,                  // Button Count, Hat Switch Count
                   true, true, true,     // X and Y, Z Axis
                   true, true, true,   // Rx, Ry, Rz
                   false, true,          //rudder, throttle
                   false, false, false);  //accelerator, brake, steering

previously when I was working on another project which involved servos I understood a little more of the joystick library and made new code that worked just as I wanted it to. i found out i needed to make a joystick variable in this example its called joy1


#include "Joystick.h"

Joystick_ joy1;

void setup() {
  pinMode(15, INPUT_PULLUP);
  joy1.begin();
}

void loop() {
  if (digitalRead(15) == LOW) {
    joy1.setButton(1, 1);
  } else {
    joy1.setButton(1, 0);
  }
//debounce
  delay(50);
}

an object. But it works as well.