Midi Traktor DJ - Hold Toggle Button Code Problem

Hi Again,

I've read a lot on Arduino and Midi, so I thought i would give this a try.

so from all the examples i found i've build a small Midi Controller with two buttons and a potentiometer.

but I run into some coding problems. but first my setup and code:

I'm using this controller with Traktor Pro and have

one button set for the CUE, (BUTTON A) this button is mapped to HOLD, so when it's released Traktor stops playing

one button set for FX 1 ON/OFF, (BUTTON B) this button is set to TOGGLE, so when pressed it turns the FX on and when pressed again it should switch OFF

one Potentiometer to control amount of FX, this potentiometer is set to DIRECT

////////////////////////////////////////////////////////////////////////////
// this constant won't change:
const int buttonPinA = 10;    
const int ledPinA = 13;       

// Variables will change:

int buttonStateA = 0;         
int lastButtonStateA = 0;     
////////////////////////////////////////////////////////////////////////////
// this constant won't change:
const int buttonPinB = 9;   
const int ledPinB = 12;       

// Variables will change:

int buttonStateB = 0;        
int lastButtonStateB = 0;   
////////////////////////////////////////////////////////////////////////////
// this constant won't change:
const int  buttonPinC = 0;   
const int ledPinC = 11;       

// Variables will change:

int buttonStateC = 0;         
int lastButtonStateC = 0;     
////////////////////////////////////////////////////////////////////////////

void setup() {
  Serial.begin(31250);

 pinMode(buttonPinA, INPUT);
  pinMode(buttonPinB, INPUT);

 pinMode(ledPinA, OUTPUT);
  pinMode(ledPinB, OUTPUT); 
  pinMode(ledPinC  , OUTPUT);

}


void loop() {
  ////////////////////////////////////////////////////////////////////////////
  buttonStateA = digitalRead(buttonPinA);

  if (buttonStateA != lastButtonStateA) {
    if (buttonStateA == HIGH) {
          digitalWrite(ledPinA, HIGH);
           Serial.print(0x90, BYTE);
   Serial.print(1, BYTE);
   Serial.print(127, BYTE);
    } 
    else {
digitalWrite(ledPinA, LOW);
 Serial.print(0x90, BYTE);
   Serial.print(1, BYTE);
   Serial.print(0x00, BYTE);
    }

    lastButtonStateA = buttonStateA;
  }
  
  ////////////////////////////////////////////////////////////////////////////
  buttonStateB = digitalRead(buttonPinB);

  if (buttonStateB != lastButtonStateB) {
    if (buttonStateB == HIGH) {
          digitalWrite(ledPinB, HIGH);
           Serial.print(0x90, BYTE);
   Serial.print(0, BYTE);
   Serial.print(127, BYTE);
    } 
    else {
digitalWrite(ledPinB, LOW); Serial.print(0x90, BYTE);
   Serial.print(0, BYTE);
   Serial.print(0x00, BYTE);
    }

    lastButtonStateB = buttonStateB;
  }

  ////////////////////////////////////////////////////////////////////////////

  buttonStateC = analogRead(buttonPinC);

  if (buttonStateC != lastButtonStateC) {
  
digitalWrite(ledPinC,HIGH);
digitalWrite(ledPinC,LOW);
Serial.print(0x90, BYTE);
   Serial.print(2, BYTE);
   Serial.print(buttonStateC/8, BYTE);
    
    } 
    else {
digitalWrite(ledPinC,LOW);

    }

    lastButtonStateC = buttonStateC;
}

Now the problem is the BUTTON B that controls the FX ON/OFF does not TOGGLE,

So i removed this part of the code

Serial.print(0x90, BYTE);
   Serial.print(0, BYTE);
   Serial.print(0x00, BYTE);

so it wouldn't send a stop signal, it kinda off works but i get a lot of missed presses.

i hope this is all clear enough, any help would be appreciated

EDIT

ok i seem to found something, it's the same issue i got with my nana kontrol, i need to make a TOGGLE switch instead of a MOMENTARY switch... still don't know the coding solution...

EDIT well after a day of trial and error, still nothing

but i found that the code, is sending to much midi messages.

I used midi monitor to look at the output and when a button is pressed it gives about 6 times the message it was supposed to...

so it must have something to do with the save/change state..

Perhaps a few well placed delay()'s would do the trick.. or you can keep track of the time that has elapsed and trigger midi events at specific intervals.

Hello everybody,

i found a solution! @ http://roguescience.org/wordpress/?page_id=24

here is the code as it is now:

//Button 1
int ledPin = 12;                      //led output 
int buttonPin = 10;                   //button input pin             
int buttonVal = 0;                    //variable for reading the button status
int buttonState = 0;                  //variable to hold the buttons current state
int bounceCheck = 0;                  //variable for debouncing

//Button 2
int BledPin = 13;                      //led output 
int BbuttonPin = 9;                   //button input pin             
int BbuttonVal = 0;                    //variable for reading the button status
int BbuttonState = 0;                  //variable to hold the buttons current state
int BbounceCheck = 0;                  //variable for debouncing

//POTMETER

int potPin = 0;                  //choose the input pin for a potentometer
int potVal = 0;                //variable for reading potentiometer value
int mappedPotVal = 0;          //variable for holding remapped pot value
int prevPotVal = 0;               //variable for storing our prev pot value
int THRESHOLD = 2;            //threshold amount


void setup() {
  pinMode(ledPin, OUTPUT);            //declare LED as output
  pinMode(buttonPin, INPUT);          //declare pushbutton as input
  Serial.begin(31250);                //MIDI communicates at 31250 baud
}

void loop()
{
  ////////////////////////////TOGGLE////////////////////////////////////
  buttonVal = digitalRead(buttonPin);     //read input value from button
  delay(5);                              //wait 10ms
  bounceCheck = digitalRead(buttonPin);   //check again
  
  
  if(buttonVal == bounceCheck){ //if val is the same then not a bounce
                                if (buttonVal == HIGH && buttonState == 1) {    //check if the input is HIGH
                                                                                digitalWrite(ledPin, HIGH);         //turn LED OFF
                                                                                midiOUT(0x90, 0, 127);              //Note ON (CH 1), C1, velocity 127
                                                                                buttonState = 0;
                                                                            }
                                if(buttonVal == LOW && buttonState == 0){
                                                                              digitalWrite(ledPin, LOW);       //turn LED ON
                                                                              midiOUT(0x90, 0, 0);           //Note ON (CH 1), middle C1, velocity 0
                                                                              buttonState = 1;
                                                                         }
                               }

   
  ////////////////////////////MOMENTARY////////////////////////////////////
  BbuttonVal = digitalRead(BbuttonPin);     //read input value from button

  BbounceCheck = digitalRead(BbuttonPin);   //check again
  
  
  if(BbuttonVal == BbounceCheck){ //if val is the same then not a bounce
                                if (BbuttonVal == HIGH && BbuttonState == 1) {    //check if the input is HIGH
                                                                                digitalWrite(BledPin, HIGH);         //turn LED OFF
                                                                                midiOUT(0x90, 1, 127);              //Note ON (CH 1), middle C#1, velocity 127
                                                                                BbuttonState = 0;
                                                                            }
                                if(BbuttonVal == LOW && BbuttonState == 0){
                                                                              digitalWrite(BledPin, LOW);       //turn LED ON
                                                                              midiOUT(0x90, 1, 0);           //Note ON (CH 1), middle C, velocity 0
                                                                              BbuttonState = 1;
                                                                         }
                               }

   

////////////////////////////////POT-METER///////////////////////////////////
 potVal = analogRead(potPin);         //read input from potentiometer
 mappedPotVal = map(potVal, 0, 1023, 0, 127);  //map value to 0-127
   if(abs(mappedPotVal - prevPotVal) >= THRESHOLD){
                                                     midiOUT(0xB0, 7, mappedPotVal); //Control Change (Ch 1), Controller 7 - default control for volume.
                                                     digitalWrite(ledPin, HIGH);
                                                     prevPotVal = mappedPotVal;
                                                   }
   else{
          digitalWrite(ledPin, LOW);
       }
}

void midiOUT(char command, char value1, char value2) {
  Serial.print(command, BYTE);
  Serial.print(value1, BYTE);
  Serial.print(value2, BYTE);

}

and this is my temporary box: