morning folks,
I'm trying to use MIDI.sendControlChange(); in the 'MIDI library' to toggle a button on and off (let's say a 'MUTE' button for argument's sake) inside Ableton via Hiduino (USB connection). However, all i can manage to do so far is control it momentarily. So when I press the button it becomes 'ON' in Ableton but then when I let go it becomes 'OFF' again. What I want is to press the button once and the 'MUTE' in ableton becomes 'ON' and then press it a second time and the 'MUTE' becomes 'OFF'.
The appoach i've been attempting is by creating an integer variable called toggleState and then incorperating that into the conditions in my 'if' statements as you can see in my code below. However for some reason this code isn't actually sending anything at all. Even though When I did the exact same code minus the 'toggleState' variable and conditions it actually does send the signal only as a momentary instead of a toggle.
#include <MIDI.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <midi_Settings.h>
MIDI_CREATE_DEFAULT_INSTANCE();
const int buttonPin = 2;
int buttonState = 0;
int lastButtonState = 0;
int toggleState = 0;
void setup() {
MIDI.begin(1);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
buttonState = digitalRead(buttonPin);
if(buttonState != lastButtonState && buttonState == 1 && toggleState == 0) {
MIDI.sendControlChange(1, 127, 1);
lastButtonState = buttonState;
toggleState = 1;
}
if(buttonState != lastButtonState && buttonState == 1 && toggleState == 1) {
MIDI.sendControlChange(1, 0, 1);
lastButtonState = buttonState;
toggleState = 0;
}
delay(50);
}
There is nothing complicated about the hardware side of things so I'm guessing there is nothing wrong there but just in case here's a fritzing image for your reference.
Thanks a lot