Grazie del consiglio :), ormai avevo abbandonato l'idea che si potesse fare via software.
Ho adattato un po' di codice che ho trovato su internet http://arduino.cc/en/Tutorial/Debounce Home · thomasfredericks/Bounce2 Wiki · GitHub e adesso funziona :D.
const int buttonPinUp = A2; //bottone su
const int buttonPinDown = A1; //bottone giu
const unsigned long time_delay=10;
const int pinCount =7; //l'ultimo pin usato a partire da 2 comprso
int octave=0;
struct luca{
int Fotopin;
int Fotovalue;
int Note;
int Stato;
unsigned long current_time;
unsigned long tempo_accumulato;
unsigned long tempo;
} Foto[pinCount]; //array di struct di tipo "luca" lunga "pinCount"
int buttonStateUp= 0;
int buttonStateDown= 0;
int buttonconstup=0;
int buttonconstdown=0;
void setup() {
// Set MIDI baud rate:
Serial.begin(9600);
//Set tutti i pin in INPUT mode
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
Foto[thisPin].Fotopin=thisPin+2;
pinMode(Foto[thisPin].Fotopin, INPUT);
Foto[thisPin].Note=50+thisPin;
Foto[thisPin].Stato=0;
Foto[thisPin].tempo_accumulato=0;
Foto[thisPin].tempo=0;
Foto[thisPin].current_time=0;
}
pinMode(buttonPinUp, INPUT);
pinMode(buttonPinDown, INPUT);
}
//fine setup
//*******************************************************
int velocity = 100;//velocity of MIDI notes, must be between 0 and 127
//higher velocity usually makes MIDI instruments louder
//******Funzione MIDIMessage******//
void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
Serial.write(command);//send note on or note off command
Serial.write(MIDInote);//send pitch data
Serial.write(MIDIvelocity);//send velocity data
}
//*******Fine funzione MIDIMessage*********//
int noteON = 144;//144 = 10010000 in binary, note on command
int noteOFF = 128;//128 = 10000000 in binary, note off command
void loop(){
for (int i = 0; i < pinCount; i++) {
Foto[i].current_time=millis();
Foto[i].Fotovalue= digitalRead(Foto[i].Fotopin);
if (Foto[i].Fotovalue == HIGH){
Foto[i].tempo=millis()-Foto[i].current_time+Foto[i].tempo_accumulato;
if ((Foto[i].tempo>=time_delay)&&(Foto[i].Stato==0))
{
MIDImessage(noteON, Foto[i].Note+octave, velocity);//turn note on
// Serial.print(Foto[i].Stato);
Foto[i].Stato=1;
}
else{
Foto[i].tempo_accumulato=Foto[i].tempo;
}
}
else if((Foto[i].Fotovalue == LOW)&&(Foto[i].Stato==1)){
Foto[i].tempo_accumulato=0;
MIDImessage(noteOFF, Foto[i].Note+octave, velocity);
Foto[i].Stato=0;
}
else {
Foto[i].tempo_accumulato=0;
}
}
}