I’ve been trying to combine two seperate sketches in to one for a simple piano style keyboard using a Due MIDI over USB.
My problem is with the the transpose feature using two buttons, one for octave down and one for octave up.
I’m using the MIDIUSB Library by Gary Grewal on GitHub;
Also the uniquePress Button Library by Alexander Brevig, updated by Terry King;
http://arduino-info.wikispaces.com/HAL-LibrariesUpdates
And the sketch by Arturo Guadalupi;
My coding skills are not great, as you find below. I’ve kept the sketch small just to get it working before moving on to more keys etc. Other then my poor coding I’m a little lost with the words I need to use for the MIDIUSB Library, such as the transpose. The only way I think I can get this to work regarding the transpose, is to write-off all the transpose1, if, else, transpose2, etc and change them to actual key notes?
Currently I have the notePitches;
C5, D5b, D5, E5b, E5, F5, G5b, G5, A5b, A5, B5b, B5. One octave.
So my thinking is, when I press the octave down button I change the notePitches to;
C4, D4b, D4, E4b, E4, F4, G4b, G4, A4b, A4, B4b, B4. One octave below and do similar for the octave up button.
Can anyone point me in the direction for some documentation on how to use the MIDIUSB library or a list of the words I should be using with this library.
Like I say, my coding is something to be desired, I need to start adding comments, but I’m having fun in experimenting and learning from my failings.
Regards.
Dizzwold.
/*
This examples shows how to make a simple seven keys MIDI keyboard with volume control
Created: 4/10/2015
Author: Arturo Guadalupi <a.guadalupi@arduino.cc>
http://www.arduino.cc/en/Tutorial/MidiDevice
*/
#include <Button.h>
#include "MIDIUSB.h"
#include "PitchToNote.h"
#define NUM_BUTTONS 12
Button button20 = Button(2, PULLUP);
Button button21 = Button(3, PULLUP);
const int InputTriggerTranspose1 = 2;
const int InputTriggerTranspose2 = 3;
byte transpose1 = 0;
byte transpose2 = 0;
const uint8_t button1 = 53;
const uint8_t button2 = 52;
const uint8_t button3 = 51;
const uint8_t button4 = 50;
const uint8_t button5 = 49;
const uint8_t button6 = 48;
const uint8_t button7 = 47;
const uint8_t button8 = 46;
const uint8_t button9 = 45;
const uint8_t button10 = 44;
const uint8_t button11 = 43;
const uint8_t button12 = 42;
//const int intensityPot = 127; //A0 input
const uint8_t buttons[NUM_BUTTONS] = {button1, button2, button3, button4, button5, button6, button7, button8, button9, button10, button11, button12};
const byte notePitches[NUM_BUTTONS] = {pitchC5, pitchD5b, pitchD5, pitchE5b, pitchE5, pitchF5, pitchG5b, pitchG5, pitchA5b, pitchA5, pitchB5b, pitchB5};
uint8_t notesTime[NUM_BUTTONS];
uint8_t pressedButtons = 0x00;
uint8_t previousButtons = 0x00;
uint8_t intensity = 127;
//uint8_t intensity;
void setup() {
for (int i = 0; i < NUM_BUTTONS; i++)
pinMode(buttons[i], INPUT_PULLUP);
pinMode(2, INPUT);
pinMode(3, INPUT);
}
byte pressCount = 0;
void loop() {
readButtons();
readIntensity();
playNotes();
if(button20.uniquePress())
pressCount++;
switch(pressCount)
{case 1:
transpose1 = digitalRead(InputTriggerTranspose1);
if (transpose1 == HIGH) {
transpose1 = -12;}
else {
transpose1 = 0;}
pressCount = 0;
break;
}
if(button21.uniquePress())
pressCount++;
switch(pressCount)
{case 1:
transpose2 = digitalRead(InputTriggerTranspose2);
if (transpose2 == HIGH) {
transpose2 = +12;}
else {
transpose2 = 0;
pressCount = 0;
break;
}
}
}
// 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(50);
}
else
bitWrite(pressedButtons, i, 0);
}
}
void readIntensity()
{
//int val = analogRead(intensityPot);
//intensity = (uint8_t) (map(val, 0, 1023, 0, 127));
int val ();
}
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], intensity);
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);
}