Buttons & Switches
Last updated: Oct 2, 2019
Breadboard demo
I used to play alto saxophone in middle school band. I never had a solo and I haven’t revisited the instrument since, but the language of staffs and notes and the interface of the instrument have stuck with me. The saxophone player blows into a mouthpiece and presses keys to alter the airflow within the [horn?]—the sound itself comes from the reed, but the pitch is altered by which holes are covered or open along the length of the sax. The keys and the reed mean the interface has a digital quality to it—the note coming out is either a C or a C#—but it’s obviously still analog: the performer’s mouth does a lot of the work, and really, key-input isn’t always as cut and dry as it seems either. Excellent saxophone players can squeeze really beautiful wails and screams out of it. If the mic’s close you can hear the keys clack.
So I wanted to replicate some of that—keys that only have a binary position, but that are interrelated in the results they produce. I wrote a quick demo of what I wanted to get done in Pico-8, which is designed for quick prototyping of game-style interfaces.
When I got home, I dug out my breadboard and set up the same interface. I wrote up some code to account for the states of the three buttons and set to testing:
bool playing = false;
bool red = false;
bool yellow = false;
bool blue = false;
void setup() {
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(8, OUTPUT);
}
void loop() {
// code for inputs
if (!digitalRead(2)){
red = true;
} else {
red = false;
}
if (!digitalRead(3)){
blue=true;
} else {
blue = false;
}
if (!digitalRead(4)){
yellow=true;
} else {
yellow = false;
}
//code for outputs
if(red){
tone(8,440);
} else if (yellow){
tone(8,494);
}
}
But this worked in a pretty weird way: pushing the red button and yellow button, I could get both sounds (an A and a B) to play, but I couldn’t get it to line up with the way I wanted the inputs to work. After getting to this point with my program, I realized what was going on: where I had used a function in Pico-8 that returned whether the button was held down or not, the push button switches on my breadboard were only “returning” whether they had been pushed or not.
Conclusions?
What I came up with definitely doesn’t work as written. So, what steps are there that could bring this project towards success?
- Swap out the push-buttons for proximity sensors (and hope their proximity to each other doesn’t cause trouble)?
- Write (or find) a Button class with its own isDown method that keeps track of the button’s state and fires from when it changes until it changes again?
- Find physical buttons that fire more like the program I wrote expects them to?