Hi,
I'm new to Arduino and fairly new to programming. I've been doing ok but have got stuck. I know how to send midi but I'm trying to send send three midi control messages using one button. It's the button release that i'm stuck on.
I'd like the button to:
- Send Message-1 on button press and then,
- Send a single Message-2 and single Message-3 when the button is released.
Any programming advice would be much appreciated.
Ok thanks... I had read that post.
However I currently have no code to show as I'm not sure how to approach the problem.
I thought maybe there was a simple approach to triggering an action (send a message etc) when a button is released.
I'll guess I'll just persevere.
See File->Examples->02.Digital->StateChangeDetection. Use that code as a template to do things when a button is pressed or released:
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
// Send Message 1
} else {
// if the current state is LOW then the button went from on to off:
// Send Message 2
// Send Message 3
}
@johnwasser
Thank you! I suspected it was a simple as this. I'll try this tonight!
In the meantime I'd written in notepad this more complicated thing below as I was worried about sending message 2 and 3 continuously while the button was off. I haven't tried it yet.
int State = 0;
int buttonOne = 6; // assign button pin to variable
void setup()
{
Serial.begin(9600);
pinMode(buttonOne,INPUT); // setup button as input
}
void loop()
{
if (digitalRead(buttonOne) == HIGH) {
Serial.println("Message 1");
State = 1;
delay(1000); // Wait for 1000 millisecond(s)
}
if (digitalRead(buttonOne) == LOW && State == 1) {
Serial.println("Message 2");
State = 0;
delay(1000); // Wait for 1000 millisecond(s)
}
}
}
Thank you for your kind help.
This is how I handle buttons:
You can have one variable for the state of a button, every loop you can poll the new state and compare it with the previous state and with that you can know know the button changed and when it is released
bool Button1State;
void loop()
{
bool new_state = digitalRead(Button1Pin);
if(new_state != Button1State)
{
//Handle here the button change
state = new_state;
if( state )
{
//ButtonPressed
}
else
{
//ButtonReleased
}
}
}
Hope this helps!
This is great too. I think I have heaps to go on with this evening.
Thanks everyone for your help! This has been my first time asking for assistance and you've been so helpful!
Cheers!