//some random cc numbers:
#define SAVE_CC 120
#define CHANNELMODE_CC 119
#define FULLLEGATO_CC 118
#define OCTAVE_CC 117
#define STICK_CC 116
//midi in variables
byte state = 0;
byte statusByte;
byte dataByte1;
byte dataByte2;
// One digit 7 segment LED display demo.
// Displays digit 0 - 9 and decimal point
// Prepare array of 7-Arduino pins which we need to interfaced with
// 7segments
// Define os pinos dos footswitchs
#define UP_BUTTON 12
#define DOWN_BUTTON 13
int buttonUpState = 0;
int buttonDownState = 0;
int lastButtonUpState = 0;
int lastButtonDownState = 0;
int currentProgram = 0; // Variável para armazenar o programa atual
int initProgram = 0; // Variável para armazenar o programa inicial
int maxProgram = 23; // Variável para armazenar o máximo de programas
int segPins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // { a b c d e f g . )
//Truth table for driving the 7segments LED
byte segCode[27][8] = {
//a b c d e f g .
{ 1, 0, 0, 1, 1, 1, 1, 1}, // 1
{ 0, 0, 1, 0, 0, 1, 0, 1}, // 2
{ 0, 0, 0, 0, 1, 1, 0, 1}, // 3
{ 1, 0, 0, 1, 1, 0, 0, 1}, // 4
{ 0, 1, 0, 0, 1, 0, 0, 1}, // 5
{ 0, 1, 0, 0, 0, 0, 0, 1}, // 6
{ 0, 0, 0, 1, 1, 1, 1, 1}, // 7
{ 0, 0, 0, 0, 0, 0, 0, 1}, // 8
{ 0, 0, 0, 0, 1, 0, 0, 1}, // 9
{ 0, 0, 0, 0, 0, 0, 1, 0}, // 0.
{ 1, 0, 0, 1, 1, 1, 1, 0}, // 1.
{ 0, 0, 1, 0, 0, 1, 0, 0}, // 2.
{ 0, 0, 0, 0, 1, 1, 0, 0}, // 3.
{ 1, 0, 0, 1, 1, 0, 0, 0}, // 4.
{ 0, 1, 0, 0, 1, 0, 0, 0}, // 5.
{ 0, 1, 0, 0, 0, 0, 0, 0}, // 6.
{ 0, 0, 0, 1, 1, 1, 1, 0}, // 7.
{ 0, 0, 0, 0, 0, 0, 0, 0}, // 8.
{ 0, 0, 0, 0, 1, 0, 0, 0}, // 9.
{ 0, 0, 0, 1, 0, 0, 0, 1}, // A
{ 1, 1, 0, 0, 0, 0, 0, 1}, // b
{ 0, 1, 1, 0, 0, 0, 1, 1}, // C
{ 1, 0, 0, 0, 0, 1, 0, 1}, // d
{ 0, 1, 1, 0, 0, 0, 0, 1}, // E
{ 0, 1, 1, 1, 0, 0, 0, 1}, // F
{ 1, 1, 1, 1, 1, 1, 1, 0}, // .
{ 1, 1, 1, 1, 1, 1, 1, 1}, // Off
};
void displayDigit(int digit)
{
for (int i = 0; i < 8; i++)
{
digitalWrite(segPins[i], segCode[digit][i]); //passing the value pin array
}
}
void setup()
{
//start serial with MIDI baud rate 31250 or another standard speed for debugging
Serial.begin(31250);
// Inicializa os pinos dos botões como entradas
pinMode(UP_BUTTON, INPUT_PULLUP);
pinMode(DOWN_BUTTON, INPUT_PULLUP);
for (int i = 0; i < 8; i++)
{
pinMode(segPins[i], OUTPUT);// declare Arduino pin as an output
}
displayDigit(currentProgram);
}
void loop()
{
buttonUpState = digitalRead(UP_BUTTON);
buttonDownState = digitalRead(DOWN_BUTTON);
if (buttonUpState != lastButtonUpState) {
if (buttonUpState == HIGH) {
// Verifica se ainda há programas para descer
if (currentProgram < maxProgram)
{
// Decrementa o programa atual
currentProgram++;
}
else
{
// Vai para o último programa
currentProgram = initProgram;
}
}
lastButtonUpState = buttonUpState;
displayDigit(currentProgram);
delay(50);
}
if (buttonDownState != lastButtonDownState) {
if (buttonDownState == HIGH) {
// Verifica se ainda há programas para descer
if (currentProgram > initProgram)
{
// Decrementa o programa atual
currentProgram--;
}
else
{
// Vai para o último programa
currentProgram = maxProgram;
}
}
lastButtonDownState = buttonDownState;
displayDigit(currentProgram);
delay(50);
}
// put your main code here, to run repeatedly:
MidiIn();
}
void MidiIn()
{
if (Serial.available() > 0)
{
// read the incoming byte:
byte incomingByte = Serial.read();
switch (state)
{
case 0:
if (incomingByte > 0x7F && incomingByte < 0xF0 )
{ // this reads all statusbytes, realtime stuff is ignored
statusByte = incomingByte;
state=1;
}
if (incomingByte == 0xF0) state=3;
case 1:
// get first databyte
if(incomingByte < 0x80)
{
dataByte1 = incomingByte;
//check for two byte messages...
if ((statusByte & 0xF0) == 0xC0 | (statusByte & 0xF0) == 0xD0)
{
midiParser();
state = 0; //reset state machine
}
else state = 2;
}
else
{
state = 0; // reset state machine as this should be a note number
}
break;
case 2:
// get second databyte
if(incomingByte < 0x80)
{
dataByte2 = incomingByte;
}
midiParser();
state = 0; // reset state machine to start
break;
//stay here for sysex, wait for it to end...
case 3:
if (incomingByte == 0xF7) state=0;
if(incomingByte > 0x7F && incomingByte < 0xF0)
{
statusByte = incomingByte;
state=1;
}
break;
}
}
}
void midiParser()
{
switch (statusByte)
{
case 0xC0: //program change
//do something when a program change arrives, prg nr is dataByte1
break;
case 0xB0: //cc messages
switch (dataByte1)
{
case SAVE_CC:
//do something here
break;
case CHANNELMODE_CC:
//do something else
case STICK_CC:
//again something on this cc
break;
case FULLLEGATO_CC:
//you get the idea
break;
case OCTAVE_CC:
//octaves!!!
break;
}
}
}