CC toggle on/off

Hi.

Just bought Arduino Uno and have never programmed anything before so Im happy I came this far first day! But now Im stuck and need some help… Here is the code Im using for sending CC messages. Right now button 1 is sending CC80 127 and button 2 CC80 0. BUT Im looking for a toggle function instead. So I can use the same button for on/off. Push the button once should turn the CC message on (127) and pressing the same button again should turn it off (0).

How is that done?

And second question… I need a LED that’s on when its 127 and off when its 0. Any ideas for that?

#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>

const int button1 = 2; 
const int button2 = 3; 
const int button3 = 4; 
const int button4 = 5; 

MIDI_CREATE_INSTANCE(HardwareSerial,Serial, midiOut); 

void setup() {
  pinMode(button1,INPUT); 
  pinMode(button2,INPUT); 
  pinMode(button3,INPUT); 
  pinMode(button4,INPUT); 
  pinMode(2,INPUT_PULLUP);
  pinMode(3,INPUT_PULLUP);
  pinMode(4,INPUT_PULLUP);
  pinMode(5,INPUT_PULLUP);
  Serial.begin(31250); 
}

void loop() {
  if(digitalRead(button1) == LOW) { 
    delay(10); 
    if(digitalRead(button1) == LOW) { 
      midiOut.sendControlChange(80,127,1); 
      delay(250); 
    }
  }
 
  if(digitalRead(button2) == LOW) { 
    delay(10); 
    if(digitalRead(button2) == LOW) { 
      midiOut.sendControlChange(80,0,1); 
      delay(250);
      
    }
  }
  
  if(digitalRead(button3) == LOW) { 
    delay(10); 
    if(digitalRead(button3) == LOW) { 
      midiOut.sendControlChange(81,127,1); 
      delay(250);
    }
  }

  if(digitalRead(button4) == LOW) { 
    delay(10); 
    if(digitalRead(button4) == LOW) { 
      midiOut.sendControlChange(81,0,1); 
      delay(250);
    }
  }

}

One way to do that sort of toggling is to set a flag, maybe CCFlag, to tell what you last did when the button was pressed. If the last thing you did was send CC80, 127 (and switch the LED on) then send CC80,0 (and switch the LED off) etc.

Pseudo code something like:
if button pressed and CCFlag is 0
send CC80, 127
digitalWrite(LED, HIGH)
set CCFlag = 1
else if button pressed and CCFLag is 1
send CC80, 0
digitalWrite(LED, LOW)
set CCFlag = 0

Steve

Thanks! So here is my code right now… (only one button now but will add more as soon as I get the first one working…) Still trouble with getting the led off and send CC80, 0 when pushing a second time! Any ideas whats wrong in my code?

#include <MIDI.h>

const int button1 = 2; 
const int led1 =6;   
int flag=0;   

MIDI_CREATE_INSTANCE(HardwareSerial,Serial, midiOut); 

void setup() {
  pinMode(button1,INPUT); 
  pinMode(led1,OUTPUT);  
  pinMode(2,INPUT_PULLUP);
  Serial.begin(31250); 
}

void loop() {
  if(digitalRead(button1) == LOW) { 
    if (flag==0) { 
    delay(10);
    flag=0; 
    digitalWrite(led1,HIGH);
    if(digitalRead(button1) == LOW) { 
      midiOut.sendControlChange(80,127,1); 
    }                           
    else {                        
      flag=0;                  
      digitalWrite(led1,LOW);   
      midiOut.sendControlChange(80,0,1);   
    }
  delay(250);                    
  }   
 } 
 }

How does flag ever get to be anything other than 0?

Steve

Slipstick already gave an excellent answer, but I’d like to add an alternative: the Control Surface library I maintain might be worth a look, it supports many MIDI controller features out of the box, including buttons that toggle and send MIDI CC messages.

For example:

[color=#5e6d03]#include[/color] [color=#434f54]<[/color][b][color=#d35400]Control_Surface[/color][/b][color=#434f54].[/color][color=#000000]h[/color][color=#434f54]>[/color]

[b][color=#d35400]HardwareSerialMIDI_Interface[/color][/b] [color=#00979c]midi[/color] [color=#434f54]=[/color] [color=#000000]{[/color][b][color=#d35400]Serial[/color][/b][color=#434f54],[/color] [color=#00979c]MIDI_BAUD[/color][color=#000000]}[/color][color=#000000];[/color]

[color=#434f54]// Connect a push button between Arduino pin 2 and[/color]
[color=#434f54]// ground. You don't need any external resistors, [/color]
[color=#434f54]// the internal pull-up resistor will be enabled.[/color]

[b][color=#d35400]CCButtonLatched[/color][/b] [color=#000000]button1[/color] [color=#434f54]=[/color] [color=#000000]{[/color]
  [color=#000000]2[/color][color=#434f54],[/color]              [color=#434f54]// pin number[/color]
  [color=#000000]{[/color][color=#000000]80[/color][color=#434f54],[/color] [color=#00979c]CHANNEL_1[/color][color=#000000]}[/color] [color=#434f54]// CC number and MIDI channel[/color]
[color=#000000]}[/color][color=#000000];[/color]

[color=#434f54]// Use the built-in LED to display the state of the button[/color]
[color=#00979c]const[/color] [color=#00979c]pin_t[/color] [color=#000000]ledPin[/color] [color=#434f54]=[/color] [color=#00979c]LED_BUILTIN[/color][color=#000000];[/color]

[color=#00979c]void[/color] [color=#5e6d03]setup[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [color=#434f54]// Control Surface initializes midi and button1[/color]
  [b][color=#d35400]Control_Surface[/color][/b][color=#434f54].[/color][color=#d35400]begin[/color][color=#000000]([/color][color=#000000])[/color][color=#000000];[/color]
  [color=#d35400]pinMode[/color][color=#000000]([/color][color=#000000]ledPin[/color][color=#434f54],[/color] [color=#00979c]OUTPUT[/color][color=#000000])[/color][color=#000000];[/color]
[color=#000000]}[/color]

[color=#00979c]void[/color] [color=#5e6d03]loop[/color][color=#000000]([/color][color=#000000])[/color] [color=#000000]{[/color]
  [color=#434f54]// Control Surface reads button1 and sends MIDI messages accordingly[/color]
  [b][color=#d35400]Control_Surface[/color][/b][color=#434f54].[/color][color=#5e6d03]loop[/color][color=#000000]([/color][color=#000000])[/color][color=#000000];[/color]
  [color=#5e6d03]if[/color] [color=#000000]([/color][color=#000000]button1[/color][color=#434f54].[/color][color=#000000]getButtonState[/color][color=#000000]([/color][color=#000000])[/color] [color=#434f54]==[/color] [b][color=#d35400]Button[/color][/b][color=#434f54]:[/color][color=#434f54]:[/color][color=#00979c]Falling[/color][color=#000000])[/color] [color=#434f54]// if button was pressed[/color]
    [color=#d35400]digitalWrite[/color][color=#000000]([/color][color=#000000]ledPin[/color][color=#434f54],[/color] [color=#000000]button1[/color][color=#434f54].[/color][color=#d35400]getState[/color][color=#000000]([/color][color=#000000])[/color] [color=#434f54]?[/color] [color=#00979c]HIGH[/color] [color=#434f54]:[/color] [color=#00979c]LOW[/color][color=#000000])[/color][color=#000000];[/color] [color=#434f54]// update the LED[/color]
[color=#000000]}[/color]

Pieter

Wow thanks Pieter! Thats amazing! It works great… Will modify to add more buttons. Thanks again