7.2 State changes
Let’s start by uncommenting our button code, sending a serial message when it is pressed and commenting out our potentiometer code.
/*
* 6.2.1 State Changes
* roguescience.org
*
* Turns a LED connected to digital pin 13 on based from input on digital pin 2
* and sends a message using serial communication.
*/
int ledPin = 11; //choose the pin for the LED - needs to be (3,5,6,9,10, or 11)
int buttonPin = 2; //choose the input pin for a pushbutton
int potPin = 0; //choose the input pin for a potentometer
int buttonVal = 0; //variable for reading the button status
int buttonState = 0; //variable to hold the buttons curr
int bounceCheck = 0; //variable for debouncing
int potVal = 0; //variable for reading potentiometer value
int mappedPotVal = 0; //variable for remapping pot data to 0-255
void setup() {
pinMode(ledPin, OUTPUT); //declare LED as output
pinMode(buttonPin, INPUT); //declare pushbutton as input
Serial.begin(9600); //begin serial communication at 9600 baud
}
void loop(){
buttonVal = digitalRead(buttonPin); //read input value from button
delay(10); //wait 10ms
bounceCheck = digitalRead(buttonPin); //check again
if(buttonVal == bounceCheck){ //if val is the same then not a bounce
if (buttonVal == HIGH) { //check if the input is HIGH
digitalWrite(ledPin, LOW); //turn LED OFF
Serial.println("OFF");
} else {
digitalWrite(ledPin, HIGH); //turn LED ON
Serial.println("ON");
}
}
/*
potVal = analogRead(potPin); //read input from potentiometer
mappedPotVal = map(potVal, 0, 1023, 0, 255); //map value to 0-255
Serial.println(mappedPotVal); //send potVal via serial
analogWrite(ledPin, mappedPotVal); //use PWM to set LED brightness
*/
}
Notice how the message is getting sent over and over again - this might be appropriate for some applications but in our case if we want to trigger a single MIDI event with a button press we only want it to send the message once. To do this we want to send a message only when the STATE of the button CHANGES.
/*
* 6.2.2 State Changes
* roguescience.org
*
* Turns a LED connected to digital pin 13 on based from input on digital pin 2
* and sends a message using serial communication - only sends a message when the
* state of the button changes.
*/
int ledPin = 11; //choose the pin for the LED - needs to be (3,5,6,9,10, or 11)
int buttonPin = 2; //choose the input pin for a pushbutton
int potPin = 0; //choose the input pin for a potentometer
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
int potVal = 0; //variable for reading potentiometer value
int mappedPotVal = 0; //variable for remapping pot data to 0-255
void setup() {
pinMode(ledPin, OUTPUT); //declare LED as output
pinMode(buttonPin, INPUT); //declare pushbutton as input
Serial.begin(9600); //begin serial communication at 9600 baud
}
void loop(){
buttonVal = digitalRead(buttonPin); //read input value from button
delay(10); //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, LOW); //turn LED OFF
Serial.println("OFF");
buttonState = 0;
}
if(buttonVal == LOW && buttonState == 0){
digitalWrite(ledPin, HIGH); //turn LED ON
Serial.println("ON");
buttonState = 1;
}
}
/*
potVal = analogRead(potPin); //read input from potentiometer
mappedPotVal = map(potVal, 0, 1023, 0, 255); //map value to 0-255
Serial.println(mappedPotVal); //send potVal via serial
analogWrite(ledPin, mappedPotVal); //use PWM to set LED brightness
*/
}