Hi I’m after some help please. I’m completely new to coding and have always wanted to make my own midi fighter. I’ve finally made a start and I have almost finished my housing for 16 arcade buttons and a Teensy 3.2. It’s made up from two pieces of wood top and bottom with an acrylic band in the middle which will be lit up by LEDs.
I’ve got the code sorted (copy & pasted) for the buttons to send midi to my DAW that’s all tickity boo.
My aim is to have the 5 LEDs on all the time then go off when any of the 16 buttons are pressed. I just can’t get my head around what I need to enter and where.
Any help would be greatly appreciated.
Heres my code so far.
#include "MIDIUSB.h"
#include "PitchToNote.h"
#define NUM_BUTTONS 16
const uint16_t button1 = 2;
const uint16_t button2 = 3;
const uint16_t button3 = 4;
const uint16_t button4 = 5;
const uint16_t button5 = 6;
const uint16_t button6 = 7;
const uint16_t button7 = 8;
const uint16_t button8 = 9;
const uint16_t button9 = 10;
const uint16_t button10 = 14;
const uint16_t button11 = 15;
const uint16_t button12 = 16;
const uint16_t button13 = 18;
const uint16_t button14 = 19;
const uint16_t button15 = 20;
const uint16_t button16 = 21;
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);
}
{
pinMode(4, OUTPUT); //Use digital out 4 for the LED
usbMIDI.setHandleNoteOn(OnNoteOn); //Specify which function to handle NoteOn events
usbMIDI.setHandleNoteOff(OnNoteOff); //Specify which function to handle NoteOff events
}
void loop() {
readButtons();
playNotes();
}
{
usbMIDI.read(); //Start listen for MIDI events from USB
}
// 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);
}
{ //This is the function that handles NoteOn events
if(note == 60){ //If the note was C4 = 60
digitalWrite(4, HIGH); //5V ON
}
void noteOff(byte channel, byte pitch, byte velocity) {
midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
MidiUSB.sendMIDI(noteOff);
}
void OnNoteOff(byte channel, byte note, byte velocity){ //This is the function that handles NoteOff events
digitalWrite(4, LOW); //5V OFF, doesn't matter what note you release
}