Skip to content

Instantly share code, notes, and snippets.

@crsnbrt
Last active August 29, 2015 14:07
Show Gist options
  • Save crsnbrt/7b5d7a36a3b90861eb62 to your computer and use it in GitHub Desktop.
Save crsnbrt/7b5d7a36a3b90861eb62 to your computer and use it in GitHub Desktop.
Arduino sketch for intercepting digital inputs of four buttons and broadcasting a corresponding serial value via Bluetooth.
const char BUTTON_CHARS[] = {(char)48, (char)49, (char)50, (char)51};
const int BUTTON_COUNT = 4;
const int BUTTON_PINS[] = {4, 5, 6, 7};
int button_states[] = {0, 0, 0, 0};
int button_last_states[] = {0, 0, 0, 0};
void setup() {
pinMode(BUTTON_PINS[0], INPUT);
pinMode(BUTTON_PINS[1], INPUT);
pinMode(BUTTON_PINS[2], INPUT);
pinMode(BUTTON_PINS[3], INPUT);
Serial.begin(115200);
}
void loop(){
for(int i=0; i<BUTTON_COUNT; i++){
button_states[i] = digitalRead(BUTTON_PINS[i]);
if(!isOtherButtonPressed(i) && button_last_states[i] != button_states[i]){
if(button_states[i] == HIGH){
Serial.write(BUTTON_CHARS[i]);
}
}
}
updateLast();
}
void updateLast(){
for(int i=0; i<BUTTON_COUNT; i++){
button_last_states[i] = button_states[i];
}
}
boolean isOtherButtonPressed(int index){
for(int i=0; i<BUTTON_COUNT; i++){
if(i!= index && button_states[i] == HIGH){
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment