Hi guys,
I’ve been struggling for days with this code.
Im trying to call a function based on a MIDI note the Arduino Received. I’m not far from finding the right code but I can’t find the thing I’m doing wrong here If you could help …
#include <VarSpeedServo.h>
byte type = 0;
byte incomingByte;
byte note;
byte velocity;
int chan = 1; //specify what MIDI channel we're listing to
int action=2; //0 =note off ; 1=note on ; 2= null
const int lowNote = 60; //what's the first note? 36 is C1 in Ableton
VarSpeedServo servoI1; // create servo object to control a servo that inflate baloon1
VarSpeedServo servoI2; // create servo object to control a servo that Inflate balloon2
// the loop function runs over and over again forever
void setup(){
Serial.begin(38400);
//setup our outputs
servoI1.attach(8); // Set I1 servo to digital pin 8
servoI2.attach(9); // Set I2 servo to digital pin 9
servoI1.write(30); // move to 32 degrees to close the in valve
servoI2.write(30); // move to 32 degrees to close the in valve
}
void infslow1 () { // Monte 1 lentement
servoI1.write(45, 5, true);
delay (100);
servoI1.write(32, 0, true);
}
void infslow2() { // Monte 2 lentement
servoI2.write(45, 0, true);
delay (100);
servoI2.write(32, 0, true);
}
void loop () {
if(Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// wait for as status-byte, channel 1, note on or off
if (incomingByte== 143+chan){ // note on message starting starting
action=1;
}else if (incomingByte== 127+chan){ // note off message starting
action=0;
}else if ( (action==0)&&(note==0) ){ // if we received a "note off", we wait for which note (databyte)
note=incomingByte;
note=0;
velocity=0;
action=2;
}else if ( (action==1)&&(note==0) ){ // if we received a "note on", we wait for the note (databyte)
note=incomingByte;
}else if ( (action==1)&&(note==0) ){ // if we received a "note on", we wait for the note (databyte)
velocity=incomingByte;
moveServo(note, velocity);
note=0;
velocity=0;
action=0;
}else{
//nada
}
}
}
void moveServo(byte note, byte velocity){ // call a function based on the note
switch (note){
case lowNote: //if it's not 60
infslow1 ();
break;
}
}
Do any of you guys knows where my mistakes are?
Thanks a lot for your help