Skip to content

Instantly share code, notes, and snippets.

@Craigson
Created October 18, 2014 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Craigson/252c511ebcfea991a400 to your computer and use it in GitHub Desktop.
Save Craigson/252c511ebcfea991a400 to your computer and use it in GitHub Desktop.
push button grid prototype
//P-comp midterm
//Instagram grid-button prototype
//5 October 2014
//Craig Pickard and Minju Viviana Kim
//assign variables for each button that represents a tile in the grid
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;
const int button5 = 6;
//create a variable that stores data relating to which button has been pressed
int buttonValue = 0;
//set button states for each tile to false
int previousButtonState1 = LOW;
int previousButtonState2 = LOW;
int previousButtonState3 = LOW;
int previousButtonState4 = LOW;
int previousButtonState5 = LOW;
int previousButtonState6 = LOW;
int previousButtonState7 = LOW;
int previousButtonState8 = LOW;
int previousButtonState9 = LOW;
void setup(){
//declare all buttons as digital inputs
pinMode(button1,INPUT);
pinMode(button2,INPUT);
pinMode(button3,INPUT);
pinMode(button4,INPUT);
pinMode(button5,INPUT);
Serial.begin(9600);
}
void loop(){
//read the button state of each tile
int buttonState1 = digitalRead(button1);
int buttonState2 = digitalRead(button2);
int buttonState3 = digitalRead(button3);
int buttonState4 = digitalRead(button4);
int buttonState5 = digitalRead(button5);
int buttonState6 = 0; //phantom button
int buttonState7 = 0; //phantom button
int buttonState8 = 0; //phantom button
int buttonState9 = 0; //phantom button
//check if current button states are different to previous states
//if (buttonState1 == LOW && buttonState2 == LOW && buttonState3 == LOW && buttonState4 == LOW && buttonState5 == LOW && buttonState6 == LOW && buttonState7 == LOW && buttonState8 == LOW && buttonState9 == LOW){
//Serial.print("0");
//Serial.print(",");
if (buttonState1 != previousButtonState1){
if(buttonState1 == HIGH){
buttonValue = 1;
}
}
if (buttonState2 != previousButtonState2){
if(buttonState2 == HIGH){
buttonValue = 2;
}
}
if (buttonState3 != previousButtonState3){
if(buttonState3 == HIGH){
buttonValue = 3;
}
}
if (buttonState4 != previousButtonState4){
if(buttonState4 == HIGH){
buttonValue = 4;
}
}
if (buttonState5 != previousButtonState5){
if(buttonState5 == HIGH){
buttonValue = 5;
}
}
Serial.println(buttonValue);
//set the previous button state variable of each button as the current value,
//to be used in the next cycle of the loop.
previousButtonState1 = buttonState1;
previousButtonState2 = buttonState2;
previousButtonState3 = buttonState3;
previousButtonState4 = buttonState4;
previousButtonState5 = buttonState5;
//delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment