Pages: [1]   Go Down
Author Topic: stopping a played midi note in functions  (Read 1052 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
//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.
Logged

Brazil
Offline Offline
God Member
*****
Karma: 3
Posts: 616
Wusik Dot Com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, but I don't understand what you are trying to do here. Maybe you could explain more? Tell us what is the result you want, so we can check what's going on.

If you are trying to create a midi-sustain app, its not like that, from what I see. You need a list of all on-hold notes, and release one-by-one when you hit release. Unless I'm getting things all wrong...  smiley-red

Wk
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: April 20, 2011, 02:52:56 am by Rupertt » Logged

Brazil
Offline Offline
God Member
*****
Karma: 3
Posts: 616
Wusik Dot Com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You could just send a channel-all-notes-off them. Its very simple.

http://www.omega-art.com/midi/mbytes.html

Table 3: Status Bytes 176-191; Control and Mode Changes (per channel)
123 = All notes off
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

that sounds like what I need.
How would that look for channel 1 in hex?
im a bit confused how to create the right sequence of numbers.

In my noteOn string I have to put that sequence in the first part, right?

noteOn('here', note2, 0x7F);

this seems to work:

   noteOn(0xB0, 0x7B, 0x00);

I have no idea where the 7 comes from, the B was clear from the binary string

« Last Edit: April 20, 2011, 06:31:28 am by Rupertt » Logged

Brazil
Offline Offline
God Member
*****
Karma: 3
Posts: 616
Wusik Dot Com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You need to send:

Code:
Serial.write(0xB0+channel_number);
Serial.write(0x7B);
Serial.write((byte)0x00);
Logged


Pages: [1]   Go Up
Jump to: