Create Midi Toggle

I m building a midi foot switch and I need a Toggle CC, I need when I press button on pin 9 to send a CC # with Value 0 and if I press it again with Value 127 ( CC on/off) …
Would you help me? now I can just send one value when pressing a button.
thanks

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE(); 

byte patchNum = 0;


void setup() {

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);

MIDI.begin(MIDI_CHANNEL_OMNI);



}

void loop() {
   
 if (digitalRead(2) == LOW ){


  MIDI.sendControlChange (69,0,1);
  delay(200);
}
 if (digitalRead(3) == LOW ){


  MIDI.sendControlChange (69,1,1);
  delay(200);
}
 if (digitalRead(4) == LOW ){


  MIDI.sendControlChange (69,2,1);
  delay(200);
}
if (digitalRead(5) == LOW ){


  MIDI.sendControlChange (68,0,1);
  delay(200);
}
if (digitalRead(6) == LOW ){


  MIDI.sendControlChange (7,0,1);
  delay(200);
}
if (digitalRead(7) == LOW ){


  MIDI.sendControlChange (8,0,1);
  delay(200);
}
if (digitalRead(8) == LOW ){


  MIDI.sendControlChange (9,0,1);
  delay(200);
}
if(digitalRead(9) == LOW ){
            MIDI.sendControlChange(10,0,1);
            delay(200);
          
           
  
}
}

I try this but the midi signal blink on/off without pushing button :frowning:

if(digitalRead(9) == LOW ) ;{
            MIDI.sendControlChange(10,0,1);
            delay(200);
           digitalWrite(9,HIGH); 
              delay(200); 
        
              
               if(digitalRead(9) == HIGH ) ;{
            MIDI.sendControlChange(10,127,1);
            delay(200);
            digitalWrite(9,LOW); 
              delay(200); 
             
            }

You need to check out the StateChangeDetection example in the IDE because you want to know when the button BECOMES pressed not just that it IS pressed.

Steve

thanks a lot slipstick , its very useful, I understand what the command does.. but I m stuck trying to use it with midi.. error board is always red and I don't know how to fix... :frowning:

I can't see what changes you've made and I have no idea what "error board is always red" means. If you post your latest code and the complete error listing, if any, then someone could probably help.

Steve

this is the full code, I should have a TOGGLE CC in the last button THANKS

#include <MIDI.h>

MIDI_CREATE_DEFAULT_INSTANCE(); 

byte patchNum = 0;


void setup() {

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);

MIDI.begin(MIDI_CHANNEL_OMNI);



}

void loop() {
   
 if (digitalRead(2) == LOW ){


  MIDI.sendControlChange (69,0,1);
  delay(200);
}
 if (digitalRead(3) == LOW ){


  MIDI.sendControlChange (69,1,1);
  delay(200);
}
 if (digitalRead(4) == LOW ){


  MIDI.sendControlChange (69,2,1);
  delay(200);
}
if (digitalRead(5) == LOW ){


  MIDI.sendControlChange (68,0,1);
  delay(200);
}
if (digitalRead(6) == LOW ){


  MIDI.sendControlChange (7,0,1);
  delay(200);
}
if (digitalRead(7) == LOW ){


  MIDI.sendControlChange (8,0,1);
  delay(200);
}
if (digitalRead(8) == LOW ){


  MIDI.sendControlChange (9,0,1);
  delay(200);
}
if(digitalRead(9) == LOW ) ;{
            MIDI.sendControlChange(10,0,1);
            delay(200);
         
        
              
              
          
          
}

Is "the last button" the one on pin 9? And do you really mean you want to press it once, send CC 10, value 0 then later press it again and send CC 10, value 127?

Can you tell me where in your code you are trying to do anything like that? And where are you using the StateChangeDetection techniques?

If you just want someone to write a program for you try in Gigs and Collaborations and be prepared to pay.

Steve