Button on/off over serial TX/MIDI

Hi there everyone, i'm trying to send a message trough TX serial ouput. the two codes i got for my project are this ones.

void setup()
   Serial.begin(31250);       // Default speed of MIDI serial port
   pinMode(13, OUTPUT);       // Light LED on pin 13 to notify of readynes
   digitalWrite(13, HIGH);

int iAn0Val, iAn1Val, iAn2Val, iAn3Val, iAn4Val, iAn5Val, iAn6Val, iAn7Val, iAn8Val;

void loop()
   //Slider 1
   int iAn0ValPrev = iAn0Val; //Get the previous value of slider 1
   iAn0Val = analogRead(0)/8;
   analogPinMidiTX(1,iAn0Val,iAn0ValPrev); //TX the value of slider 1
   //Slider 2
   int iAn1ValPrev = iAn1Val; //Get the previous value of slider 2
   iAn1Val = analogRead(1)/8;
   analogPinMidiTX(2,iAn1Val,iAn1ValPrev); //TX the value of slider 2
    //Slider 3
   int iAn2ValPrev = iAn2Val; //Get the previous value of slider 3
   iAn2Val = analogRead(2)/8;
   analogPinMidiTX(3,iAn2Val,iAn2ValPrev); //TX the value of slider 3
    //Slider 4
   int iAn3ValPrev = iAn3Val; //Get the previous value of slider 4
   iAn3Val = analogRead(3)/8;
   analogPinMidiTX(4,iAn3Val,iAn3ValPrev); //TX the value of slider 4
    //Slider 5
   int iAn4ValPrev = iAn4Val; //Get the previous value of slider 5
   iAn4Val = analogRead(4)/8;
   analogPinMidiTX(5,iAn4Val,iAn4ValPrev); //TX the value of slider 5
   //Slider 6
   int iAn5ValPrev = iAn5Val; //Get the previous value of slider 6
   iAn5Val = analogRead(5)/8;
   analogPinMidiTX(6,iAn5Val,iAn5ValPrev); //TX the value of slider 6
   //Slider 7
   int iAn6ValPrev = iAn6Val; //Get the previous value of slider 7
   iAn6Val = analogRead(6)/8;
   analogPinMidiTX(7,iAn6Val,iAn6ValPrev); //TX the value of slider 7
   //Slider 8
   int iAn7ValPrev = iAn7Val; //Get the previous value of slider 8
   iAn7Val = analogRead(7)/8;
   analogPinMidiTX(8,iAn7Val,iAn7ValPrev); //TX the value of slider 8

void analogPinMidiTX(int iChan, int iVal, int iValPrev)
  //only TX the value over midi if it is changed, as to prevent spamming the midi port and thus confusing the receiving application in learning mode
  if(iValPrev != iVal)

void MidiTX(unsigned char MESSAGE, unsigned char CONTROL, unsigned char VALUE) //pass values out through standard Midi Command

at this time i am able to send the value of a 4K7 potentiometer over midi to control Pan in Logic Pro 9, but now i have 8 buttons (bang tipe not trigger) and i want to send the message in the same way, but at the sime time i want this bang buttons to be trigger so that when i press 1 time it gives high and it stay HIGH, and only get's LOW when i press again.

Here is the Code i have for the digital button read.

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH);  
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 

Thanks in Advance

First off post code in a code box. Modify that post, select the code and hit the #icon.

Second say what the code actually does without us having to guess. Then we stand a better chance of seeing where you are wrong.

What hardware MIDI interface are you using?

Alright thanks for the advice.

The first code regarding serial communication reads 8 potentiometers and send's the midi value trough TX, after that i have it connected to a midi cable that connects to an audio interface ( alesis i/o 26 ) and so far so good i have the values, and everything is going good pretty much like i wanted.

But now i dont have any ideia how to read the digital button (with the second code i posted) and send that value over midi, since i have buttons that switch on, and when i take my finger it goes off, mostly like a BANG. And i want to convert it to a real switch [ press 1 time > ON, press again >OFF ] the problem is the buttons probably gives 127 when its pressed and 0 with no finger on.

So any clues on how this should work??

and so far so good i have the values, and everything is going good pretty much like i wanted.

Well no, you are swamping your MIDI receiver with lots of redundant code by sending the same value over and over. Make a record of the number you last sent it and only send it again when that number changes.

And i want to convert it to a real switch [ press 1 time > ON, press again >OFF ]

That is not a real switch, that is known as a latching switch or a push on / push off switch. You haven't got one of these so you will have to do it in software. Again use a variable to remember what state you last sent, then when you detect a button push again, send the inverse of the state. Note you will have to remember the last state of the button and only send stuff when the last state was up and the current state is down.

Well do you have any example of to save the last position and not sending until it moves again?

val = analogRead(pin);
If(abs(val - lastVal) > 2) { 
// send to midi here
lastVal = val;

This sends stuff only if the value is changed by two. This is done to suppress the natural jitter you get with any A/D.

How do you code to make the switch ON when its pressed and OFF when not pressed. on the digital input. Applyed on this code.

Same thing, you remember the previous value of the button and only send it out to MIDI if it has changed:-

val = digitalRead(pin);
if(val != lastVal) {
// send MIDI value for button
lastVal = val;

Now my arduino mega allways stays with the TX light on, how do we fix this?