After a lot of days... This is the best I've done!! There is still some problem of rate.. but.. it works!!
Has someone any idea how to improve the code?
/*
MIDI On/Off Messages
By Amanda Ghassaei
July 2012
http://www.instructables.com/id/Send-and-Receive-MIDI-with-Arduino/
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*/
int noteOFF = 128;//128
const int analogInPin = A0;
const int POT_THRESHOLD = 3;// in +
const int HISTORY_BUFFER_LENGTH = 6;// in +
int velocity = 0;//velocity of MIDI notes, must be between 0 and 127
//higher velocity usually makes MIDI instruments louder
int sensorValue = 0;
int ControlChange = 176;//176 = in binary, controlchange command
int NoteOn =145;
//int noteOFF = 128;//128 = 10000000 in binary, note off command
static int s_history[HISTORY_BUFFER_LENGTH];// in +
//BUTTONS
int buttonState1;
int val1;
int buttonState2;
int val2;
int buttonState3;
int val3;
int led = 13; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5;//led
void setup() {
// Set MIDI baud rate:
Serial.begin(57600);
// Initialize he buffer
for(int i=0; i<HISTORY_BUFFER_LENGTH; i++)// in +
{// in +
s_history[i] = -1;// in +
}// in +
//BUTTON
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(led, OUTPUT);//led
buttonState1 = digitalRead(2);//HIGH
buttonState2 = digitalRead(3); //HIGH
buttonState3 = digitalRead(4); //HIGH
}
void loop() {
val1 =digitalRead(2);
if(val1 != buttonState1){
if (val1 ==LOW) {
//Send an ASCII 'A',
MIDImessage(145, 60 ,100);
}}
else{MIDImessage(145, 60 ,0);}
val2 = digitalRead(3);
if (val2 != buttonState2){
if (val2 ==LOW){
MIDImessage(146, 61,100);
}
else{MIDImessage(146, 61,0);}
}
val3 = digitalRead(4);
if (val3 != buttonState3){
if (val3 == LOW){
MIDImessage(147, 62,100);
}
else{MIDImessage(147, 62,0);}
}
delay(50);
buttonState1 = val1;
buttonState2 = val2;
buttonState3 = val3;
static int s_nLastPotValue = 0; //in +
static int s_nLastMappedValue = 0;//in +
int nCurrentPotValue = analogRead(A0);//in +
if(abs(nCurrentPotValue - s_nLastPotValue) < POT_THRESHOLD)//in +
return;//in +
s_nLastPotValue = nCurrentPotValue;//in +
int nMappedValue = map(nCurrentPotValue, 0, 1023, 0, 255); //in + // Map the value to 0-255
if(nMappedValue == s_nLastMappedValue)//in +
return;//in +
for(int i=0; i<HISTORY_BUFFER_LENGTH; i++)//in +
{//in +
if(s_history[i] == nMappedValue)//in +
return;//in +
}//in +
memcpy(&s_history[0], &s_history[1], sizeof(int) * (HISTORY_BUFFER_LENGTH - 1));//in +
s_history[HISTORY_BUFFER_LENGTH - 1] = nMappedValue;//in +
s_nLastMappedValue = nMappedValue;//in +
// sensorValue = analogRead (analogInPin);
velocity = map (nCurrentPotValue, 0, 1023, 0, 127);
MIDImessage(176, 50, velocity);//turn note on
delay(20);
//MIDImessage(noteOFF, 50, velocity);//turn note off
//wait 200ms until triggering next n
}
//send MIDI message
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
}