Is my code right?

So I’ve been wanting to build myself a MIDI controller using the Arduino UNO and 16 LED arcade buttons. I have purchased the arduino, and am holding off until I get some issues resolved.

One of my issues is my code. I have NO idea if its right or not. So, here is some example code (The example is from Blitz City DIY):

#include "MIDIUSB.h"
#include "PitchToNote.h"
#define NUM_BUTTONS  16

const uint16_t button1 = 0;
const uint16_t button2 = 1;
const uint16_t button3 = 2;
const uint16_t button4 = 3;
const uint16_t button5 = 4;
const uint16_t button6 = 5;
const uint16_t button7 = 6;
const uint16_t button8 = 7;
const uint16_t button9 = 8;
const uint16_t button10 = 9;
const uint16_t button11 = 10;
const uint16_t button12 = 11;
const uint16_t button13 = 12;
const uint16_t button14 = 13;
const uint16_t button15 = 14;
const uint16_t button16 = 15;

const uint16_t buttons[NUM_BUTTONS] = {button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12, button13, button14, button15, button16};
const byte notePitches[NUM_BUTTONS] = {pitchC2, pitchD2b, pitchD2, pitchE2b, pitchE2, pitchF2, pitchG2b, pitchG2, pitchA2b, pitchA2, pitchB2b, pitchB2, pitchC3, pitchD3b, pitchD3, pitchE3b};

uint16_t notesTime[NUM_BUTTONS];
uint16_t pressedButtons = 0x00;
uint16_t previousButtons = 0x00;
uint16_t intensity;

void setup() {
  for (int i = 0; i < NUM_BUTTONS; i++)
    pinMode(buttons[i], INPUT_PULLUP);
}


void loop() {
  /*digitalRead(0);
  digitalRead(1);
  digitalRead(2);
  digitalRead(3);
  digitalRead(4);
  digitalRead(5);
  digitalRead(5);
  digitalRead(6);
  digitalRead(7);
  digitalRead(8);
  digitalRead(9);
  digitalRead(10);
  digitalRead(11);
  digitalRead(12);
  digitalRead(13);
  digitalRead(14);
  digitalRead(15);
  readButtons();
  */
  playNotes();
}

// First parameter is the event type (0x0B = control change).
// Second parameter is the event type, combined with the channel.
// Third parameter is the control number number (0-119).
// Fourth parameter is the control value (0-127).

void controlChange(byte channel, byte control, byte value) {
  midiEventPacket_t event = {0x0B, 0xB0 | channel, control, value};
  MidiUSB.sendMIDI(event);
}

void readButtons()
{
  for (int i = 0; i < NUM_BUTTONS; i++)
  {
    if (digitalRead(buttons[i]) == LOW)
    {
      bitWrite(pressedButtons, i, 1);
      delay(2);
    }
    else
      bitWrite(pressedButtons, i, 0);
  }
}

void playNotes()
{
  for (int i = 0; i < NUM_BUTTONS; i++)
  {
    if (bitRead(pressedButtons, i) != bitRead(previousButtons, i))
    {
      if (bitRead(pressedButtons, i))
      {
        bitWrite(previousButtons, i , 1);
        noteOn(0, notePitches[i], 100);
        MidiUSB.flush();
      }
      else
      {
        bitWrite(previousButtons, i , 0);
        noteOff(0, notePitches[i], 0);
        MidiUSB.flush();
      }
    }
  }
}

// First parameter is the event type (0x09 = note on, 0x08 = note off).
// Second parameter is note-on/note-off, combined with the channel.
// Channel can be anything between 0-15. Typically reported to the user as 1-16.
// Third parameter is the note number (48 = middle C).
// Fourth parameter is the velocity (64 = normal, 127 = fastest).

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}

My other issue is I’m not sure what to do in terms of my digital/analog pins. I’m going to have to use 2 analog pins for my arcade buttons, since the UNO only has 14. How do I read them as being digital?

lots of problems in there...

like reading a pin and doing nothing with the result...

  digitalRead(1);
  digitalRead(2);
  digitalRead(3);
  digitalRead(4);
  digitalRead(5);
  digitalRead(5);
  digitalRead(6);
  digitalRead(7);
  digitalRead(8);
  digitalRead(9);
  digitalRead(10);
  digitalRead(11);
  digitalRead(12);
  digitalRead(13);
  digitalRead(14);
  digitalRead(15);

The analog inputs are digital inputs (default after reset) until analog read or write is called. Then they become analog or PWM pins. So to use them as inputs you need do nothing unless you need to enable input pullups. To use them as digital outputs you need to set the pinMode to OUTPUT.

The Arduino Uno as 14 digital pins, but every analog input pin is also a digital (input and output) pin. That makes 20.

Can you give use more information when you as a question. Are you use this library ? https://www.arduino.cc/en/Reference/MIDIUSB With this example ? https://www.youtube.com/watch?v=OuUIEHzjo9A

This is the code that comes with the example: https://github.com/BlitzCityDIY/diyMidiFighter/blob/master/arduinoCode

Your code is not the same. Why did you change it ? The code in the example is used with a Pro Micro Arduino board. It will not work with your Arduino Uno board :(

Some Arduino boards have USB in the processor itself. Those can be used to make a mouse, or keyboard, or USB Midi device. They are the "atmega32u4 based boards or ARM boards" as you can read in link to the library.

The analog inputs are digital inputs (default after reset) until analog read or write is called. Then they become analog or PWM pins.

PWM on the analog pins ?

Total brain fart. Oops.

:o Sooo TOTAL brain fart. I didn't mean to send this lol. After giving it some thought, I mean to change my question to:

So I have these parts to make my own DJ TechTools MIDI Fighter: Arduino UNO jumper wires M-M 16 LED 30mm arcade buttons from Adafruit

And I know how to wire everything up. All of the soldering is the part I know how to do. My problem is the code.

I've been scouring the internet for HOURS trying to find at least something that was slightly useful for what I want to do, but I haven't found anything at all. That Blitz City DIY was close, just different microcontroller. If someone could help me find a bit of example code that'd be a life saver.

Also, do I need to use resistors at all for the arcade buttons? if so, what values should I purchase them at?

P.S: Guys, thanks SO much for responding to my question. Y'all are lifesavers.

-G

OK, here’s the thing. If you really want to be able to build this thing then you’re going to have to be patient and take some time to learn. That means first spending some time with a page like this and some of the button example codes and learn to read the buttons. Just buttons all by themselves, maybe printing out their status to the Serial monitor. In the process you start to learn a little about coding in C++. Then you can start reading up on Midi libraries and how to write code for a midi controller. You’ll go through dozens of examples of that, playing with each one and learning how they all work. Then finally, after you’ve got everything figured out, THEN you start putting it all together into one big project.

If that thought doesn’t appeal to you, then you hire someone to build it or buy one that’s commercially available.

Thanks so much for that reply Delta. I guess I was getting impatient and wanted this done now. I'm going to follow your advice and have patience while I learn. Thanks! -G

Sooo this is a very late reply to all of this. Thanks so much to all y'all for providing VERY useful information. I have edited my code and am 85% ;) sure it will work. I know where almost everything needs to go, except for 1 pin: the LED data pin. Any links/replies would be awesome. Again, I am a beginner with this, so if I'm being dumb, DO let me know (just please don't be rude lol) Thanks. Edit: I've heard/seen from a few different places/people that I need to use resistors on the LED pin. What strength should they be? I've heard a few different values, but just want to be 100% sure

GotCorn: I've heard/seen from a few different places/people that I need to use resistors on the LED pin. What strength should they be? I've heard a few different values, but just want to be 100% sure

Do you actually want to learn how to do this? Or just insert tab A into slot A?

The resistors used in conjunction with LEDs are typically referred to as current-limiting resistors. As the LED itself has very little resistance, connecting directly to a power supply will allow so much current to 'flow' through that the life of the LED can be measured in nanoseconds. Even the small amount of current available from an Arduino pin (+/- 40mA) is too much for your basic LED so a resistor is placed in series to "limit" the flow. You can get into some complicated math here, but it's not completely necessary. If you assume the voltage drop across the resistor to be 5V (which in reality it's less) and you want to limit the current flow through the LED to say 20mA (and the current in a series circuit is the same everywhere) then the value of the resistor, according to Ohm's Law is 5/.020 or 250.

In practice, this value can drop to about 200 (with potential risk to 150 or 120) or higher to maybe 1K (if you don't care how bright it is). If your ultimate goal is a battery powered device, tend to the high side for less drain.

GotCorn: Sooo this is a very late reply to all of this. Thanks so much to all y'all for providing VERY useful information. I have edited my code and am 85% ;) sure it will work.

That's nice, so post your improved code here so that we can help you sort out the other 15% (-ish!).

Steve

Crap, I wish I could show the code, but the original computer I coded it on has died. And, to make matters worse, I didn't even upload it to a cloud service like Google drives!! I'll have to re code it, using a few examples from people like the blitz city diy that I showed earlier on (and I WILL tweak it to work w/ Arduino Uno!) And the pushbutton example from this website. And if anybody else can think of another source, I'll definitely look into it. Thankfully, I remember some of the things that I included in the final version, so that should help. Thanks so much for all yall's amazing input! It's truly amazing how patient and polite everyone has been. Sigh if only everyone were like this... ;) Thanks again, G Another edit: I'm getting the final parts for a computer I'm building today, so I might have my example code put by the end of today or tomorrow, maybe. I've got a busy week w/ school.

But how can we know if your work is right, yiu know what your hardware and your obiective are, so you have to controll your code and ask compkete questions (thisbis ky point of view)

I’d recommend my MIDI Controller library: GitHub - tttapa/MIDI_controller: This is a library for creating a MIDI controller using an Arduino or Teensy board.

Pieter