Hello,
i created a small programms that holds a midi note via sustain, how can I stop that note properly?
in my first programm i didnt use functions and was able to stop that note,,
but now I use functions and (importantly) I play notes on Midi Channel 1 and Channel 2.
this is my complete programm, i already have the lastnoteplayed like in the example but
couldnt transfer it properly to my new addtitions:
//Buttons
const int switchPin1 = 31;
const int switchPin2 = 32;
// Indicator LED
const int LEDpin1 = 41;
const int LEDpin2 = 42;
//Button Zeit
const int timePin1 = 30;
const int middleC = 60; // Middle C (MIDI note value 60) is the lowest note we'll play
// Variables:
byte note1 = 0x2A;// The MIDI note value to be played
byte note2 = 0x2A;// The MIDI note value to be played
byte kanal = 0xB1;
int AnalogValue1 = 0;
int AnalogValue2 = 0;// value from the analog input
int lastNotePlayed1 = 0; // note turned on when you press the switch
int lastNotePlayed2 = 0; // note turned on when you press the switch
int lastSwitchState = 0; // state of the switch during previous time through the main loop
int currentSwitchState1 = 0;
int currentSwitchState2 = 0;
int currentTimeState = 0;
int aktiv = 0;
int buttonCounter = 0;
int delaypause = 1000;
unsigned long timercount = 0;
unsigned long timer_start = 0;
unsigned long wartezeit = 5000; //1000ms = 1 sekunde
unsigned long currentTime = 1000;
void setup() {
// set the states of the I/O pins:
pinMode(switchPin1, INPUT);
pinMode(switchPin2, INPUT);
pinMode(LEDpin1, OUTPUT);
pinMode(LEDpin2, OUTPUT);
pinMode(timePin1, INPUT);
// Set MIDI baud rate:
Serial.begin(31250);
blink(3);
timer_start = millis();
}
void loop() {
currentSwitchState1 = digitalRead(switchPin1);
currentSwitchState2 = digitalRead(switchPin2);
currentTimeState = digitalRead(timePin1);
if (currentTimeState == 1) {
currentTime = currentTime + 1000;
}
if (currentSwitchState1 == 1) {
play(1, currentTime);
digitalWrite(LEDpin1, HIGH);
} else {
playoff(lastNotePlayed1, 1);
}
if (currentSwitchState2 == 1) {
play(2, currentTime);
digitalWrite(LEDpin1, HIGH);
} else {
playoff(lastNotePlayed2, 2);
}
}
void play(int tracknr, int currentTime) {
switch (tracknr) {
case 1:
AnalogValue1 = analogRead(0);
note1 = AnalogValue1/8;
noteOn(0x80, lastNotePlayed1, 0x00);
noteOn(0xB0, 64, 127);
noteOn(0x90, note1, 0x7F);
lastNotePlayed1 = note1;
digitalWrite(LEDpin1, HIGH);
delay(currentTime);
blink(2);
digitalWrite(LEDpin1, HIGH);
break;
case 2:
AnalogValue2 = analogRead(1);
note2 = AnalogValue2/8;
noteOn(0x81, lastNotePlayed2, 0x00);
noteOn(0xB1, 64, 127);
noteOn(0x91, note2, 0x7F);
lastNotePlayed2 = note2;
digitalWrite(LEDpin2, HIGH);
delay(currentTime);
blink(2);
digitalWrite(LEDpin2, HIGH);
break;
}
}
void playoff(int lastNotePlayed, int tracknr) {
switch (tracknr) {
case 1:
noteOn(0x80, lastNotePlayed, 0x00);
//blink(1);
digitalWrite(LEDpin1, LOW);
break;
case 2:
noteOn(0x81, lastNotePlayed, 0x00);
//blink(1);
digitalWrite(LEDpin2, LOW);
break;
}
}
// 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(byte cmd, byte data1, byte data2) {
Serial.print(cmd, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE);
}
// Blinks an LED 3 times
void blink(int howManyTimes){
int i;
for (i=0; i< howManyTimes; i++) {
digitalWrite(LEDpin1, HIGH);
digitalWrite(LEDpin2, HIGH);
delay(100);
digitalWrite(LEDpin1, LOW);
digitalWrite(LEDpin2, LOW);
delay(100);
}
}
thnx for your help.