stopping a played midi note in functions

Hello,

the note is set by a potentiometer ( note1 = AnalogValue1/8; ), each switch is for one note ( play(1, currentTime); ).
When I press a switch the note is played and a variable is set that should contain the current note (lastNotePlayed1 = note1;),
now when I want to turn of the note i use "noteOn(0x80, lastNotePlayed, 0x00);" to stop the note.

I removed all the functions and the note does not stop at all,
is this the correct command to stop a playing note ?

"noteOn(0x80, lastNotePlayed, 0x00);"

Is there a way to stop all notes on that channel?

another problem is that now it sends a constant note of message, but it should only
if i turn the switch off

greetings

here my latest version without functions:

//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 lastSwitchState1 = 0;    // state of the switch during previous time through the main loop
int lastSwitchState2 = 0; 
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) {
          AnalogValue1 = analogRead(0);
          note1 = AnalogValue1/8;
          noteOn(0x80, lastNotePlayed1, 0x00);
          noteOn(0xB0, 64, 127);
          noteOn(0x90, note1, 0x7F);
          lastNotePlayed1 = note1;
          digitalWrite(LEDpin1, HIGH);
          delay(100);
          digitalWrite(LEDpin1, LOW);
          delay(currentTime);
          lastSwitchState1 = 1;
         // blink(2);
          digitalWrite(LEDpin1, HIGH);;
  } 
  if (currentSwitchState1 == 0 && lastSwitchState1 == 1) {
         noteOn(0x80, lastNotePlayed1, 0x00);
        //blink(1);
        digitalWrite(LEDpin1, LOW); 
        lastSwitchState1 = 0;       
  } 
    
 if (currentSwitchState2 == 1) {
          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);
          delay(100);
          digitalWrite(LEDpin2, LOW);
         // blink(2);
         digitalWrite(LEDpin2, HIGH);
 } 
   if (currentSwitchState2 == 0 && lastSwitchState2 == 1) {
        noteOn(0x81, lastNotePlayed2, 0x00);
        //blink(1);
        digitalWrite(LEDpin2, LOW); 
        lastSwitchState2 = 0;
    } 
 
}

//  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);
  }
}