#include <LiquidCrystal.h>
#define NOTE_ON 144
#define CONTROLCHANGE 176
#define ALLSOUNDOFF 120
#define SensorsNumber 6
#define Legato 10
#define Limit 710
LiquidCrystal lcd(0, 1, 2, 3, 4, 5);
const int RightSW=13, LeftSW=10, ToggleDown=12, ToggleUp=11;
int nLB=0,nLM=1,nLT=2,nRB=3,nRM=4,nRT=5,vLB=1,vLM=1,vLT=1,vRB=1,vRM=1,vRT=1;
int Input[SensorsNumber] = {0,1,2,4,5,3};
int Note[SensorsNumber] = {nRT,nRM,nRB,nLT,nLM,nLB};
int Velocity[SensorsNumber] = {vRT,vRM,vRB,vLT,vLM,vLB};
short Counter[Sensorsnumber];
void sendMidi(int cmd, int pitch, int velocity);
void Evaluate(int Value, int Note, int State, int Velocity);
void sendMidi(int cmd, int pitch, int velocity) {
Serial.write(cmd);
Serial.write(pitch);
Serial.write(velocity);
}
int Evaluate(int Pin){
int Status = 0;
if(1000 < analogRead(Input[Pin])){
Counter[Pin]++;
if(Counter[Pin] > Limit){
Counter[Pin] = Limit;
}
}else{
if(Counter[Pin] > Legato){
Status = 3;
}
Counter[Pin] = 0;
}
if(Counter[Pin] > Legato){
Status = 2;
}
else if(Counter[Pin] == Legato){
Status = 1;
}
return Status;
}
void setup() {
Serial.begin(31250);
pinMode(RightSW,INPUT);
pinMode(LeftSW,INPUT);
pinMode(ToggleDown,INPUT);
pinMode(ToggleUp,INPUT);
lcd.begin(16, 2);
sendMidi(CONTROLCHANGE, ALLSOUNDOFF, 0);
}
void loop(){
for(int n = 0; n < SensorsNumber; n++){
int Status = Evaluate(n);
if(Status == 1){
sendMidi(NOTE_ON, Note[n], Velocity[n]);
}
else if(Status == 3){
sendMidi(NOTE_ON, Note[n], 0);
}
}
}