Hello everyone!
I'm trying to build a simple Arduino MIDI controller for my pc with just some buttons. The board I'm using is an Arduino UNO r3 and I'm using HIDUINO to interface it with the DAW I'm using on my pc.
Using the MIDI library this is the program I came up with:
#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();
// read digital pins number
int pushButton1 = 0;
int pushButton2 = 1;
int pushButton3 = 2;
int pushButton4 = 3;
int pushButton5 = 5;
int pushButton6 = 6;
int pushButton7 = 8;
int pushButton8 = 9;
//int changeButton = 12;
//int ledPin = 13;
// checks if the button is pressed
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int buttonState7 = 0;
int buttonState8 = 0;
//int buttonStateC = 0;
// play/stop notes in relation to buttons pressed
int note1 = 0;
int note2 = 0;
int note3 = 0;
int note4 = 0;
int note5 = 0;
int note6 = 0;
int note7 = 0;
int note8 = 0;
void setup() {
MIDI.begin(MIDI_CHANNEL_OMNI);
pinMode(pushButton1, INPUT_PULLUP);
pinMode(pushButton2, INPUT_PULLUP);
pinMode(pushButton3, INPUT_PULLUP);
pinMode(pushButton4, INPUT_PULLUP);
pinMode(pushButton5, INPUT_PULLUP);
pinMode(pushButton6, INPUT_PULLUP);
pinMode(pushButton7, INPUT_PULLUP);
pinMode(pushButton8, INPUT_PULLUP);
}
void loop() {
// read state of buttons
int buttonState1 = digitalRead(pushButton1);
int buttonState2 = digitalRead(pushButton2);
int buttonState3 = digitalRead(pushButton3);
int buttonState4 = digitalRead(pushButton4);
int buttonState5 = digitalRead(pushButton5);
int buttonState6 = digitalRead(pushButton6);
int buttonState7 = digitalRead(pushButton7);
int buttonState8 = digitalRead(pushButton8);
// Button 2
// when button pressed:
if (buttonState1 == HIGH) {
// if note not playing
if (note1 == 0) {
// play note (note number, velocity, channel)
// more info: http://arduinomidilib.sourceforge.net/a00001.html
// MIDI notes chart http://www.phys.unsw.edu.au/jw/notes.html
// 55 = G3, 127 = trigger note with max volume
MIDI.sendNoteOn(24,127,1);
// note is playing
note1 = 1;
}
// when button released
} else {
// if note playing
if (note1 == 1) {
// if playing - stop
MIDI.sendNoteOff(24,0,1);
}
// if button released note is off
note1 = 0;
}
if (buttonState2 == HIGH) {
if (note2 == 0) {
MIDI.sendNoteOn(25,127,1);
note2 = 1;
}
} else {
if (note2 == 1) {
MIDI.sendNoteOff(25,0,1);
}
note2 = 0;
}
if (buttonState3 == HIGH) {
if (note3 == 0) {
MIDI.sendNoteOn(26,127,1);
note3 = 1;
}
} else {
if (note3 == 1) {
MIDI.sendNoteOff(26,0,1);
}
note3 = 0;
}
if (buttonState4 == HIGH) {
if (note4 == 0) {
MIDI.sendNoteOn(27,127,1);
note4 = 1;
}
} else {
if (note4 == 1) {
MIDI.sendNoteOff(27,0,1);
}
note4 = 0;
}
if (buttonState5 == HIGH) {
if (note5 == 0) {
MIDI.sendNoteOn(28,127,1);
note5 = 1;
}
} else {
if (note5 == 1) {
MIDI.sendNoteOff(28,0,1);
}
note5 = 0;
}
if (buttonState6 == HIGH) {
if (note6 == 0) {
MIDI.sendNoteOn(29,127,1);
note6 = 1;
}
} else {
if (note6 == 1) {
MIDI.sendNoteOff(29,0,1);
}
note6 = 0;
}
if (buttonState7 == HIGH) {
if (note7 == 0) {
MIDI.sendNoteOn(30,127,1);
note7 = 1;
}
} else {
if (note7 == 1) {
MIDI.sendNoteOff(30,0,1);
}
note7 = 0;
}
if (buttonState8 == HIGH) {
if (note8 == 0) {
MIDI.sendNoteOn(31,127,1);
note8 = 1;
}
} else {
if (note8 == 1) {
MIDI.sendNoteOff(31,0,1);
}
note8 = 0;
}
delay(1);
}
The main idea is: "As long as button X is pressed send note Y"
The MIDI message is sent but just on the release of the button and not when I press it, I'm using "standard pushbuttons" (like THIS) and I don't really know what am I doing wrong.
As you can see I'm quite new to the Arduino world and this is my very first project that I'm trying to make as a real one and not just a breadboard example.
If more information are needed just hit me up, I'll reply for sure!
Thank you in advance for any reply!