//#include <midi_serialization.h>
#include <usbmidi.h>
const int ANALOG_PIN_COUNT = 1;
const int BUTTON_PIN_COUNT = 7;
const int BANK = 4;
int analogPins[ANALOG_PIN_COUNT] = { A7 };
int buttonPin[BUTTON_PIN_COUNT] = { 1, 2, 3, 4, 5, 6, 7 };
int buttonState[2 * BUTTON_PIN_COUNT];
int buttonDown[BUTTON_PIN_COUNT];
int ccValues;
int readCC(int pin) {
// Convert from 10bit value to 7bit.
return analogRead(pin) >> 3;
}
int isButtonDown(int pin)
{
if (digitalRead(pin) == 0)
return 0;
else
return 1;
}
// plays a MIDI note. Doesn't check to see that cmd is greater than 127, or that data values are less than 127:
void noteOn(int cmd, int pitch, int velocity)
{
Serial.write(cmd);
Serial.write(pitch);
Serial.write(velocity);
}
void setup()
{
for (int i = 0; i < BUTTON_PIN_COUNT; i++)
{
pinMode(buttonPin[i], INPUT_PULLUP);
// digitalWrite(buttonPin[i], HIGH);
// buttonDown[i] = isButtonDown(buttonPins[i]);
pinMode(buttonPin[i] + BUTTON_PIN_COUNT, OUTPUT); // Vert
pinMode(buttonPin[i] + 2 * BUTTON_PIN_COUNT, OUTPUT); // Rouge
//buttonState[i] = 0; // BANK A
//buttonState[i + BUTTON_PIN_COUNT] = 0; // BANK B
}
for (int i=0; i<ANALOG_PIN_COUNT; i++)
{
pinMode(analogPins[i], INPUT); // Pédale d'expression
ccValues = readCC(analogPins[i]);
}
pinMode(0, OUTPUT);
pinMode(19, OUTPUT);
//digitalWrite(buttonPin[BANK] + BUTTON_PIN_COUNT, LOW); // Jaune
//digitalWrite(buttonPin[0] ,LOW); // Vert
// initialize digital pin LED_BUILTIN as an output.
//pinMode(LED_BUILTIN, OUTPUT);
// Set MIDI baud rate:
Serial.begin(115200); // 115200 (Serial MIDI USB), 31250 (MIDI Connector Application)
}
void loop()
{
int value = readCC(analogPins[0]);
// Send CC only if th has changed.
if (ccValues != value)
{
noteOn(0x90, value, 127);
ccValues= value;
}
for (int i=0; i<BUTTON_PIN_COUNT; i++)
{
int down = isButtonDown(buttonPin[i]);
if (down != buttonDown[i])
{
buttonDown[i] = isButtonDown(buttonPin[i]);
if (!buttonDown[i])
{
if (i != BANK)
{
noteOn(0x90, i + buttonState[BANK] * BUTTON_PIN_COUNT, 127);
buttonState[i + buttonState[BANK] * BUTTON_PIN_COUNT] = !buttonState[i + buttonState[BANK] * BUTTON_PIN_COUNT];
if(buttonState[i + buttonState[BANK] * BUTTON_PIN_COUNT] != 0)
{
if(buttonState[BANK] == 0)
{
digitalWrite(buttonPin[i] + BUTTON_PIN_COUNT, HIGH); // Vert ON
if (i < BANK)
digitalWrite(buttonPin[i] + 2 * BUTTON_PIN_COUNT, LOW); // Rouge OFF
else
{
if (i == BANK + 1)
digitalWrite(0, LOW); // Rouge ON
if (i == BANK + 2)
digitalWrite(19, LOW); // Rouge ON
}
}
else
{
digitalWrite(buttonPin[i] + BUTTON_PIN_COUNT, LOW); // Vert OFF
if (i < BANK)
digitalWrite(buttonPin[i] + 2 * BUTTON_PIN_COUNT, HIGH); // Rouge ON
else
{
if (i == BANK + 1)
digitalWrite(0, HIGH); // Rouge ON
if (i == BANK + 2)
digitalWrite(19, HIGH); // Rouge ON
}
}
}
else
{
digitalWrite(buttonPin[i] + BUTTON_PIN_COUNT, LOW); // Vert OFF
//digitalWrite(buttonPin[i] + 2 * BUTTON_PIN_COUNT, LOW); // Rouge OFF
if (i < BANK)
digitalWrite(buttonPin[i] + 2 * BUTTON_PIN_COUNT, LOW); // Rouge OFF
else
{
if (i == BANK + 1)
digitalWrite(0, LOW); // Rouge ON
if (i == BANK + 2)
digitalWrite(19, LOW); // Rouge ON
}
}
}
else
{
buttonState[BANK] = !buttonState[BANK];
if(buttonState[BANK] != 0)
{
digitalWrite(buttonPin[BANK] + BUTTON_PIN_COUNT , HIGH); // Jaune BANK ON
for (int j=0; j<BUTTON_PIN_COUNT; j++)
{
if (j != BANK)
{
digitalWrite(buttonPin[j] + BUTTON_PIN_COUNT, LOW); // Vert OFF
if(buttonState[j + BUTTON_PIN_COUNT])
digitalWrite(buttonPin[j] + 2 * BUTTON_PIN_COUNT, HIGH); // Rouge ON
else
digitalWrite(buttonPin[j] + 2 * BUTTON_PIN_COUNT, LOW); // Rouge OFF
}
}
}
else
{
digitalWrite(buttonPin[BANK] + BUTTON_PIN_COUNT , LOW); // Jaune BANK OFF
for (int j=0 ; j<BUTTON_PIN_COUNT; j++)
{
if (j != BANK )
{
digitalWrite(buttonPin[j] + 2 * BUTTON_PIN_COUNT, LOW); // Rouge OFF
if(buttonState[j])
digitalWrite(buttonPin[j] + BUTTON_PIN_COUNT, HIGH); // Vert ON
else
digitalWrite(buttonPin[j] + BUTTON_PIN_COUNT, LOW); // Vert OFF
}
}
}
}
}
}
}
delay(50);
}