I'm new to using Arduino, and know next to nothing about programming. I'm currently trying to add a potentiometer, and possibly a few more button inputs to my 3D-printed guitar controller project
It uses an Arduino Pro Micro, the code is just an example file from this joystick library that I mostly just played around with using trial and error, and this pinout diagram from Sparkfun, with this being what I ended up with:
#include <Joystick.h>
Joystick_ Joystick;
void setup() {
// Initialize Button Pins
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
pinMode(7, INPUT_PULLUP);
pinMode(8, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
// Initialize Joystick Library
Joystick.begin();
}
// Constant that maps the phyical pin to the joystick button.
const int pinToButtonMap = 0;
// Last state of the button
int lastButtonState[11] = {0,0,0,0,0,0,0,0,0,0,0};
void loop() {
// Read pin values
for (int index = 0; index <= 10; index++)
{
int currentButtonState = !digitalRead(index + pinToButtonMap);
if (currentButtonState != lastButtonState[index])
{
Joystick.setButton(index, currentButtonState);
lastButtonState[index] = currentButtonState;
}
}
delay(10);
}
This is the original example code that I used:
// Simple example application that shows how to read four Arduino
// digital pins and map them to the USB Joystick library.
//
// Ground digital pins 9, 10, 11, and 12 to press the joystick
// buttons 0, 1, 2, and 3.
//
// NOTE: This sketch file is for use with Arduino Leonardo and
// Arduino Micro only.
//
// by Matthew Heironimus
// 2015-11-20
//--------------------------------------------------------------------
#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);
}
I started this project in late February/early March, but only got to the Arduino part recently. I've had my v1 working for a little over a month using this code, and I've been working on the v2 design (adding a whammy bar and repositioning the start and select buttons) since then. I only just got to the point where I need to change the code last week.
From the comments in the example and some trial and error, I figured out that "const int pinToButtonMap = 9" is the lowest-numbered pin that it will use, and "for (int index = 0; index < 4; index++)" seems to determine the rest of the pins, so I changed 9 to 0, and "< 4" to "< 18" (the number of usable pins), and it kind of worked. Pins 0-10 and 14-16 now worked as button inputs, but button 14 (which would've been on pin 13, which doesn't exist on the Pro Micro) reads as constantly pressed, and pins A0-A3 (digital pins 18-21) don't read anything. If instead, I change it to "< 19", "< 20", etc. It just stops working completely, and if I change it to "< 11" or "<= 10" (my current code), all the pins from 0-10 work, and there are no phantom button presses
Also, I'm honestly not planning on learning any kind of real programming anytime soon. Maybe in the future, but for now, I just need enough to get this working with a 10k potentiometer (the same type that official Guitar Hero controllers use for their whammy bars) and possibly a few more button inputs (I might add a tilt sensor and maybe a D-pad in a future version)