help with switch code

Hi, so i am putting together a simple midi controller that will be sending midi notes on and off with switches.

The problem i always run in to is how to to get the button to just send one "off" command with the else function.
Using the Button Library it is easy to send just on "on" command with the uniquePress function.
But then the problem is when the button is not being pressed the else function is in effect and it constantly sends out a note off message over and over and over...
I need it to just send one note off message when i release the button.

here is a sample of the code:

#include <MIDI.h>
#include <Button.h>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Button test = Button(2,PULLUP); //switch on pin 2

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void setup()
{
 MIDI.begin();
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void loop()
 {
     if(test.uniquePress()){
    MIDI.sendNoteOn(36,127,1);  // Send a Note (pitch 16, velo 127 on channel 1)
     }
[glow]   else {MIDI.sendNoteOff(36,0,1);}[/glow]
      }

the highlighted part is what needs to be sent only one time per button release.

Give this a try...

#include <MIDI.h>
#include <Button.h>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Button test = Button(2,PULLUP); //switch on pin 2
[glow]boolean NeedNoteOff;[/glow]

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void setup()
{
 MIDI.begin();
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void loop()
 {
     if(test.uniquePress()){
    MIDI.sendNoteOn(36,127,1);  // Send a Note (pitch 16, velo 127 on channel 1)
    [glow]NeedNoteOff = true;[/glow]
     }
   else {
    [glow]if ( NeedNoteOff )[/glow]
    [glow]{[/glow]
      MIDI.sendNoteOff(36,0,1);}
      [glow]NeedNoteOff = false;[/glow]
    [glow]}[/glow]
      }

Thank you for the help, i tried adding those lines.
When the button is pressed it instantly sends the note on then note off message (before i let go of the button). I am pretty sure that is because of the uniquePress function in the Button Library.

when i change uniquePress to isPressed (as shown below) it will send out multiple on messages while the button is pressed and then when it is released it sends out just the one off message.
So now the off function is working great, i just need to stop the on message from being sent over and over.

#include <MIDI.h>
#include <Button.h>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Button test = Button(8,PULLUP); //switch on pin 2
boolean NeedNoteOff;

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void setup()
{
 MIDI.begin();
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void loop()
 {
     if(test.[glow]isPressed[/glow]()){
    MIDI.sendNoteOn(36,127,1);  // Send a Note (pitch 16, velo 127 on channel 1)
    NeedNoteOff = true;
     }
   else {
    if ( NeedNoteOff )
    {
      MIDI.sendNoteOff(36,0,1);}
      NeedNoteOff = false;
    }
      }

I'll give it another go...

#include <MIDI.h>
#include <Button.h>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Button test = Button(2,PULLUP); //switch on pin 2
boolean NeedNoteOff;

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void setup()
{
 MIDI.begin();
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void loop()
 {
     if(test.uniquePress()){
    MIDI.sendNoteOn(36,127,1);  // Send a Note (pitch 16, velo 127 on channel 1)
    NeedNoteOff = true;
     }
   else {
    if ( [glow]! test.isPressed() &&[/glow] NeedNoteOff )
    {
      MIDI.sendNoteOff(36,0,1);}
      NeedNoteOff = false;
    }
      }

hmm that fails to send the note off message now.

That looks suspiciously like a bug in the Button library.

If I have time over the next 24 hours, I'll look at it. In any case, you may want to PM AlphaBeta and ask if he has time to look at it.

In the mean time, give this a try...

#include <MIDI.h>
#include <Button.h>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Button test = Button(8,PULLUP); //switch on pin 2
boolean NeedNoteOff;
boolean NeedNoteOn = true;

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void setup()
{
 MIDI.begin();
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void loop()
{
  if(test.isPressed())
  {
    if ( NeedNoteOn )
    {     
      MIDI.sendNoteOn(36,127,1);  // Send a Note (pitch 16, velo 127 on channel 1)
      NeedNoteOff = true;
      NeedNoteOn = false;
    }
  }
  else 
  {
    if ( NeedNoteOff )
    {
      MIDI.sendNoteOff(36,0,1);}
      NeedNoteOff = false;
      NeedNoteOn = true;
    }
  } 
}

cool deal, ill try to get a hold of him.
thanks a lot for the help!

thats it!!!!!!!!
awesome! thanks so much, that last version works great!

spookybonus, if you don't mind, please try this version...

#include <MIDI.h>
#include <Button.h>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Button test = Button(2,PULLUP); //switch on pin 2
boolean NeedNoteOff;

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void setup()
{
  MIDI.begin();
}

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void loop()
{
  if ( test.uniquePress() )
  {
    MIDI.sendNoteOn(36,127,1);  // Send a Note (pitch 16, velo 127 on channel 1)
    NeedNoteOff = true;
  }
  else 
  {
    if ( (test.isPressed() == 0) && NeedNoteOff )
    {
      MIDI.sendNoteOff(36,0,1);
      NeedNoteOff = false;
    }
  }
}

thanks coding,
the new version works, but playing the note quickly gives unreliable results. it seems to work every time if i wait maybe a half a second between pushes.

the previous sketch seems to work flawlessly even with very rapid button pushes.

There was a poorly placed brace in this version...
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1270107733/3#3

My apologies to AlphaBeta. The bug is in my eyeballs not his Button class.