0

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)

2 Answers 2

1

Beautiful work with the 3D-printed guitar! You have already spent a lot of time and effort with the physical design and construction of the controller, so I recommend you plan on spending some quality time with basic Arduino programming, including the basic pin layout for the Pro Micro and the basics of programming in the Arduino language (basically C++)

You can find all you need to know in the example programs that come with the Arduino IDE (File -> Examples - 01.Basics) and with the many available tutorials (search for Arduino tutorial). Here's one that seems to be pretty clear https://www.youtube.com/watch?v=QO_Jlz1qpDw

You'll also need to look at one of the many Arduino pinout diagrams for the Pro Micro, like this one https://learn.sparkfun.com/tutorials/pro-micro--fio-v3-hookup-guide/hardware-overview-pro-micro

From the diagram, it appears you can use pins 2-10 and 16-21 for digital I/0 (for your buttons), but you'll need to use one of 10, 18-21 for your potentiometer input. So, as one other poster already noted, you should use an array of pin numbers and then read each pin by indexing into the array.

For the potentiometer interface, one of the supplied examples in '01.Basics' is the 'AnalogReadSerial' program, which should get you started. To use this program, connect the center pin of a potientiometer to pin 18(A0), and the other two to VCC & GND. Upload the program to the Pro Micro, and you should get some console printouts that vary when you change the potientiometer setting.

Make your programs very small to start with - think of them as disposable snippets that will later be incorporated into the final program. One snippet just for the potentiometer, and another snippet just for, say, 4 buttons that connect to pins 2, 9, 16 & 21 just to make sure your array indexing scheme works. Then when all is working, the snippets can be combined into a 'final' (nothing is ever really final in the Arduino world).

And lastly, use serial 'print' statements liberally for test & debug; they will save you no end of trouble & frustration. You can comment them out after all testing has been done, or just leave them in. In most projects the Serial.print statements don't take up enough time or memory to matter.

Welcome to the Arduino world! You'll love it!

Frank

0

It seems pins 11, 12 and 13 do not exist as input pins and pins 0 and 1 are used for RX/TX which you don't want to use neither probably.

So instead of using a for loop iterating over indices, you should create an array with pin numbers and use that instead, something like:

Create globally:

int input_pins = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 15, 16, ... };

Use in loop:

for (int index = 0; index < sizeof(input_pins) / sizeof(int); index++)
{
    int currentButtonState = digitalRead(input_pins[index]);

You can use the same for to set the pin modes in the setup:

for (int index = 0; index < sizeof(input_pins) / sizeof(int); index++)
{
    pinMode(input_pins[index], INPUT_PULLUP);
}

sizeof(input_pins) / sizeof(int) will give the amount of items within the array. You can now easily change it depending on the pin numbers.

I suggest you learn a bit of coding to at least understand what you change, it will make your life much easier as you progress.

In case you want more pins beyond the actual existing pins, you need to use a multiplexer like 74HC595; there are plenty examples how to use this.

4
  • 1
    Don't you mean digitalRead(input_pins[index]) instead of just digitalRead(index)?
    – chrisl
    Nov 9, 2020 at 16:36
  • @chrisl Of course, thanks for the fix (I updated my answer). Nov 9, 2020 at 21:22
  • > I suggest you learn a bit of coding to at least understand what you change, it will make your life much easier as you progress I do eventually want to learn to code, but that's just not something I'm interested in doing (or have time for) in the immediate future. For now, I just want to get this working, so I have more time for other 3D printing projects I'm working on > In case you want more pins beyond the actual existing pins That won't be a problem for this project. At the absolute most, I need 15 input pins out of the 18 available
    – taj1994
    Nov 16, 2020 at 1:10
  • @taj1994 Ok clear for both. About the number of pins, of course you shouldn't add something complicated when you know beforehand it will be never needed. Good luck with your project. Nov 16, 2020 at 8:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.