Created
October 8, 2014 22:26
-
-
Save Craigson/e2992d6569d87d64c630 to your computer and use it in GitHub Desktop.
Instagram Multi button (Arduino)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//P-comp midterm | |
//Instagram grid-button prototype | |
//5 October 2014 | |
//Craig Pickard and Minju Viviana Kim | |
boolean checkButtons = false; | |
//assign variables for each grid tile | |
const int button1 = 2; | |
const int button2 = 3; | |
const int button3 = 4; | |
const int button4 = 5; | |
const int button5 = 6; | |
const int button6 = 7; | |
const int button7 = 8; | |
int leftProx = 135; | |
int rightProx = 150; | |
int buttonValue = 0; | |
int inByte; | |
//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 tiles as digital inputs | |
pinMode(button1,INPUT); | |
pinMode(button2,INPUT); | |
pinMode(button3,INPUT); | |
pinMode(button4,INPUT); | |
pinMode(button5,INPUT); | |
pinMode(button6,INPUT); | |
pinMode(button7,INPUT); | |
Serial.begin(9600); | |
establishContact(); //establish contact with Processing | |
} | |
void loop(){ | |
if (Serial.available() > 0) { //When processing responds after initial contact | |
//data is sent serially to arduino, which causes | |
//the conditional statement to evaluate to true | |
inByte = Serial.read(); | |
//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 = digitalRead(button6); | |
int buttonState7 = digitalRead(button7); | |
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; | |
} | |
} | |
if (buttonState6 != previousButtonState6){ | |
if(buttonState6 == HIGH){ | |
buttonValue = 6; | |
} | |
} | |
if (buttonState7 != previousButtonState7){ | |
if(buttonState7 == HIGH){ | |
buttonValue = 7; | |
} | |
} | |
if (buttonState8 != previousButtonState8){ | |
if(buttonState8 == HIGH){ | |
buttonValue = 8; | |
} | |
} | |
if (buttonState9 != previousButtonState9){ | |
if(buttonState9 == HIGH){ | |
buttonValue = 9; | |
} | |
} | |
Serial.print(buttonValue); | |
Serial.print(","); | |
Serial.print(leftProx); | |
Serial.print(","); | |
Serial.println(rightProx); | |
//send data to Processing as string of values | |
previousButtonState1 = buttonState1; | |
previousButtonState2 = buttonState2; | |
previousButtonState3 = buttonState3; | |
previousButtonState4 = buttonState4; | |
previousButtonState5 = buttonState5; | |
previousButtonState6 = buttonState6; | |
previousButtonState7 = buttonState7; | |
previousButtonState8 = buttonState8; | |
previousButtonState9 = buttonState9; | |
//delay(10); | |
} | |
} | |
//create a method that causes Processing to request new data only when | |
//it is ready to receive it. | |
void establishContact(){ | |
while(Serial.available() <= 0){ | |
Serial.println("begin"); //send message to begin communication | |
delay(300); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment