Help with USB leonardo button box

Hi, i've made a USB button box for flight sim, i use 2 pots, 3 on-off-on switches, 6 on-on switches, all in a 3x3 button matrix. I'm using a Arduino Leonardo board, i used this code (in the bottom of this page), after uploading it, it says done uploading and done compilng, but it gives me this error (in the bottom of the page) and it makes the usb disconnected sound. It gets recognised in the USB game controllers page but nothing works, can anyone help me? I've been working on this project for many hours and i'm desperately trying to get it to work.

Error:

Connecting to programmer: .

Found programmer: Id = "CATERIN"; type = S

Software Version = 1.0; No Hardware Version given.

Programmer supports auto addr increment.

Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:

Device code: 0x44

Code:

#include <Keypad.h>
#include <Joystick.h>
//DEFINITIONS
#define ENABLE_PULLUPS
#define NUMROTARIES 0 //replace "?" with number of rotary encoders you are using
#define NUMBUTTONS 21 //replace "?"with number of buttons you are using
#define NUMROWS 3 //replace "?" with number of rows you have
#define NUMCOLS 3 //replace "?" with number of columns you have
//BUTTON MATRIX
//first change number of rows and columns to match your button matrix,
//then replace all "?" with numbers (starting from 0)
byte buttons[NUMROWS][NUMCOLS] = {
  {0,1,2},
  {3,4,5},
  {6,7,8}
};

#define DIR_CCW 0x10
#define DIR_CW 0x20
#define R_START 0x0
#ifdef HALF_STEP
#define R_CCW_BEGIN 0x1
#define R_CW_BEGIN 0x2
#define R_START_M 0x3
#define R_CW_BEGIN_M 0x4
#define R_CCW_BEGIN_M 0x5
const unsigned char ttable[6][4] = {
  // R_START (00)
  {R_START_M,            R_CW_BEGIN,     R_CCW_BEGIN,  R_START},
  // R_CCW_BEGIN
  {R_START_M | DIR_CCW, R_START,        R_CCW_BEGIN,  R_START},
  // R_CW_BEGIN
  {R_START_M | DIR_CW,  R_CW_BEGIN,     R_START,      R_START},
  // R_START_M (11)
  {R_START_M,            R_CCW_BEGIN_M,  R_CW_BEGIN_M, R_START},
  // R_CW_BEGIN_M
  {R_START_M,            R_START_M,      R_CW_BEGIN_M, R_START | DIR_CW},
  // R_CCW_BEGIN_M
  {R_START_M,            R_CCW_BEGIN_M,  R_START_M,    R_START | DIR_CCW},
};
#else
#define R_CW_FINAL 0x1
#define R_CW_BEGIN 0x2
#define R_CW_NEXT 0x3
#define R_CCW_BEGIN 0x4
#define R_CCW_FINAL 0x5
#define R_CCW_NEXT 0x6
const unsigned char ttable[7][4] = {
  // R_START
  {R_START,    R_CW_BEGIN,  R_CCW_BEGIN, R_START},
  // R_CW_FINAL
  {R_CW_NEXT,  R_START,     R_CW_FINAL,  R_START | DIR_CW},
  // R_CW_BEGIN
  {R_CW_NEXT,  R_CW_BEGIN,  R_START,     R_START},
  // R_CW_NEXT
  {R_CW_NEXT,  R_CW_BEGIN,  R_CW_FINAL,  R_START},
  // R_CCW_BEGIN
  {R_CCW_NEXT, R_START,     R_CCW_BEGIN, R_START},
  // R_CCW_FINAL
  {R_CCW_NEXT, R_CCW_FINAL, R_START,     R_START | DIR_CCW},
  // R_CCW_NEXT
  {R_CCW_NEXT, R_CCW_FINAL, R_CCW_BEGIN, R_START},
};
#endif
//BUTTON MATRIX PART 2
byte rowPins[NUMROWS] = {23,22,21}; //change "?" to the pins the rows of your button matrix are connected to
byte colPins[NUMCOLS] = {8,9,10}; //change "?" to the pins the rows of your button matrix are connected to
Keypad buttbx = Keypad( makeKeymap(buttons), rowPins, colPins, NUMROWS, NUMCOLS);
//JOYSTICK SETTINGS
Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,
  JOYSTICK_TYPE_JOYSTICK,
32, //number of buttons
0, //number of hat switches
  //Set as many axis to "true" as you have potentiometers for
  false, // y axis
  false, // x axis
  true, // z axis
  true, // rx axis
  false, // ry axis
  false, // rz axis
  false, // rudder
  false, // throttle
  false, // accelerator
  false, // brake
  false); // steering wheel
const int numReadings = 20;

int readings[numReadings];      // the readings from the analog input
int index = 0;              // the index of the current reading
int total = 0;                  // the running total
int currentOutputLevel = 0;
//POTENTIOMETERS PART 1
//add all the axis' which are enabled above
int zAxis_ = 0;
int RxAxis_ = 0;  

//POTENTIOMETERS  PART 2
//Which pins are your potentiometers connected to?
int potentiometerPin1 = 18; //Change "?" to the pin your potentiometer is connected to
int potentiometerPin2 = 19;
const bool initAutoSendState = true;

void loop() {
CheckAllButtons();
CheckAllPotentiometers();

}
//POTENTIOMETERS PART 3
//change the details to match teh details above for each potentiometer you are using
void CheckAllPotentiometers(){

  //potentiometer 1
  currentOutputLevel = getAverageOutput(potentiometerPin1);
  zAxis_ = map(currentOutputLevel,0,1023,0,255);
Joystick.setZAxis(zAxis_);
  //potentiometer 2
  currentOutputLevel = getAverageOutput(potentiometerPin2);
  RxAxis_ = map(currentOutputLevel,0,1023,0,255);
Joystick.setRxAxis(RxAxis_);

}
int getAverageOutput(int pinToRead){
  index = 0;
  total = 0;

while (index < numReadings){
readings[index] = analogRead(pinToRead);
total = total + readings[index];
index = index + 1;
//delay (1);
  }
return total / numReadings;
}

void CheckAllButtons(void) {
if (buttbx.getKeys())
{
for (int i=0; i<LIST_MAX; i++)  
{
if ( buttbx.key[i].stateChanged )  
{
switch (buttbx.key[i].kstate) {
case PRESSED:
case HOLD:
Joystick.setButton(buttbx.key[i].kchar, 1);
break;
case RELEASED:
case IDLE:
Joystick.setButton(buttbx.key[i].kchar, 0);
break;
}
}  
}
}
};
void setup() {
  // put your setup code here, to run once:
}

I've tried it with an older IDE version, doesn't give me the error, but still doesn't work

Don't be fooled by the fact that it is displayed in red; it's not an error but usual output in IDE 2.x.

As this is no longer an upload error, I've moved your topic to a more suitable location on the forum.

The phrase "does not work" does not state anything except that the code does not do what you expect it to do. Can you provide a description of which part does not work, what you expect that part to do and what it actually does?

PS
Thanks for using code tags in your first post :+1:

Hi, thank you for the fast response, if i go on game controllers and i try pushing switches or turning pots nothing happens, that is what i i'm trying to figure out
image

Hi @bachecario.

You forgot to add a call to Joystick.begin() in your sketch. This function initializes the library, so you must always include it in your sketches that use the "Joystick" library.

Add that function call to the setup function of your sketch and then upload the sketch to the Leonardo again. Hopefully it will start working as expected this time.

You can learn how to use the "Joystick" library from studying the example sketches accessible from the File > Examples > Joystick menu in Arduino IDE, which demonstrate the library usage. There is also some documentation in the readme file here:

https://github.com/MHeironimus/ArduinoJoystickLibrary/tree/master?tab=readme-ov-file#joystick-library-api

Now it kinda works ._.
The buttons stay pressed 3 at a time and go off when i press buttons but not in a normal way.
The axis' work like a charm but, and it's nice, but i want everything to work

There are the photos of the game controller windows, the wiring and the connections.



image2

The connections to the board image and the link for the video to show you the bug

https://drive.google.com/drive/folders/1FZ_to-zVPX9_SlYaVAyzeEYXs5D-Gt78?usp=sharing

It is definitely progress at least!

My advice is to create a simple sketch that only tests the functionality of the switch matrix. Forget about the potentiometers and the game controller emulation until you are certain the matrix and your code for reading it work perfectly. Add some Serial.println() statements at strategic locations in in the sketch and then run the sketch with Tools > Serial Monitor open. That will allow you to see what is happening in your program.

1 Like

I see some numbers coming like this, but i don't know what it means
6

Did you write the code? If so, you really should be able to understand the meaning of the output from that sketch. It would be more clear if you add additional Serial.print statements to the sketch to provide more context for the information.

If you want help, you should post the test sketch you are using.

That is the test sketch that i'm using

/*
 Analog input reads an analog input on analog in 0, prints the value out.
 created 24 March 2006
 by Tom Igoe
 */

int analogValue = 0;    // variable to hold the analog value

void setup() {
  // open the serial port at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog input on pin 0:
  analogValue = analogRead(0);

  // print it out in many formats:
  Serial.println(analogValue);       // print as an ASCII-encoded decimal
  Serial.println(analogValue, DEC);  // print as an ASCII-encoded decimal
  Serial.println(analogValue, HEX);  // print as an ASCII-encoded hexadecimal
  Serial.println(analogValue, OCT);  // print as an ASCII-encoded octal
  Serial.println(analogValue, BIN);  // print as an ASCII-encoded binary

  // delay 10 milliseconds before the next reading:
  delay(10);
}

Then you have the answer right here at the top of the sketch:

This is not an appropriate test sketch for a switch matrix.

I'm not able to provide more assistance with this project but I'm certain that if you take a methodical approach and have enough persistence you will achieve success eventually. Hopefully the other forum helpers will be able to answer any further questions you might have.

Would you make a drawing of your wire connections?

This sketch is only reading Analog Pin A0. The values you see are all the same, but in different counting systems.

Your button box is connected to USB.

Show the link to the original project.

Hi, this is the wiring, hope you understand it, and a clear video of the error, i've made it all up by kinda following this 2 tutorials:https://www.youtube.com/watch?v=wkY1NsbWj5I& and https://www.youtube.com/watch?v=Z7Sc4MJ8RPM&.
If you can help me by voice maybe on arduino's discord or something i would prefer, your choice.

Wiring and video

Your video is not able to be understood. It looks like you are running the "BUTTONBOX TEST" sketch from the AMSTUDIO video, but I see nothing remarkable.

The sketch you showed above (post 14) is not the BUTTONBOX TEST sketch.

If you are using the BUTTONBOX TEST sketch and find a button is not working in the sketch, you should verify the connections are correct.

You have an interesting project.

The video shows that every time i press a random button, it could be working or not, and if it works it register 3 presses at a time, that is what i'm trying to solve. I'm using the script that is in the description of the video, obviously i changed it to be working with my button box and it "kinda" does. I've spent the whole day desperately trying to solve the 1 button press 3 randomicaly registerd problem.

Try downloading the video from drive, on the preview it's in 240p but after dowload it's 720p and clear.

This will probably be needing a "debounce" routine in your code for button presses (any button, even the encoder). The first build-video mentions that... near the rotary encoders section. A pushbutton/encoder creates hundreds of mini-press/release pulses until the button is fully pressed or released. I think your button box is working fine, but you need to read about "arduino button debounce" and I think the first build-video mentions there might be some debounce code in the "more" section of the video.

To understand debouncing a button, make a one-button project and read about debounce here (below). After you get familiar with one button, you can try the same on your buttonbox code:

https://docs.arduino.cc/built-in-examples/digital/Debounce/

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