Created
October 20, 2014 20:33
-
-
Save Craigson/9ffbb3e60af4e6a92ff8 to your computer and use it in GitHub Desktop.
Instagram Interface Prototype - Combining sensor data
This file contains hidden or 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 | |
| //------------------------------------------------------------------ | |
| //declare global variables for button system | |
| //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; | |
| const int button6 = 7; | |
| const int button7 = 8; | |
| const int button8 = 9; | |
| const int button9 = 10; | |
| int leftProx = 0; | |
| int rightProx = 150; | |
| int buttonValue = 0; | |
| //variable for receiving handshake serial data from processing | |
| 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; | |
| ///--------------------------------------------------------- | |
| //declare global variables for proximity sensor | |
| int IRpin = A0; // IR photodiode on analog pin A0 | |
| int IRemitter = 12; // IR emitter LED on digital pin 12 | |
| int thresholdLEDpin = 13; // red LED on digital pin 13 | |
| int ambientIR; // variable to store the IR coming from the ambient | |
| int obstacleIR; // variable to store the IR coming from the object | |
| int value[10]; // variable to store the IR values | |
| int distance; // variable that will tell if there is an obstacle or not | |
| boolean swipedLeft = false; //boolean variable to establish whether or not swipe has been detected | |
| int sensorThreshold = 5; //set threshold value to detect presence of user's hand | |
| int sensorState; //the IR detectors current state (HIGH or LOW) | |
| int previousSensorState = 0; //the IR detectors previous state (HIGH or LOW) | |
| //----------------------------------------------------------- | |
| void setup(){ | |
| Serial.begin(9600); | |
| //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); | |
| pinMode(button8,INPUT); | |
| pinMode(button9,INPUT); | |
| pinMode(IRemitter,OUTPUT); // IR emitter LED on digital pin 2 | |
| digitalWrite(IRemitter,LOW);// setup IR LED as off | |
| pinMode(13, OUTPUT); // LED on pin 12 set as output | |
| digitalWrite(13, LOW); // set red LED initially LOW | |
| establishContact(); //establish contact with Processing | |
| } | |
| 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 = digitalRead(button6); | |
| int buttonState7 = digitalRead(button7); | |
| int buttonState8 = digitalRead(button8); | |
| int buttonState9 = digitalRead(button9); | |
| //-------------proximity sensor code ----------------------------------- | |
| distance = readIR(5); // calling the function that will read the distance and passing the "accuracy" to it | |
| int sensorValue = abs(distance); //creates positive values to evaluate | |
| //Serial.println(sensorValue); | |
| if (sensorValue > sensorThreshold) { //if the reading of the IR detector is above the threshold value, the user's hand is present | |
| swipedLeft = true; | |
| digitalWrite(13,HIGH); | |
| } | |
| //------------------------------------------------------------------------- | |
| //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){ | |
| //buttonValue = 0; | |
| // } | |
| 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; | |
| } | |
| } | |
| //send data to Processing as string of values | |
| //set previousButtonState to current state to prepare for next loop | |
| previousButtonState1 = buttonState1; | |
| previousButtonState2 = buttonState2; | |
| previousButtonState3 = buttonState3; | |
| previousButtonState4 = buttonState4; | |
| previousButtonState5 = buttonState5; | |
| previousButtonState6 = buttonState6; | |
| previousButtonState7 = buttonState7; | |
| previousButtonState8 = buttonState8; | |
| previousButtonState9 = buttonState9; | |
| //delay(10); | |
| //the values are stored for as long as is necessary until Processing requests | |
| //more data. Once data is requested, the stored values are sent and are then | |
| //reset to zero. this is almost like we've created an outgoing serial buffer | |
| //that resets every time it sends data. | |
| 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(); | |
| if (swipedLeft == true){ | |
| leftProx = 1; | |
| } | |
| Serial.print(buttonValue); | |
| Serial.print(","); | |
| Serial.print(leftProx); | |
| Serial.print(","); | |
| Serial.println(rightProx); | |
| swipedLeft = false; | |
| buttonValue = 0; | |
| leftProx = 0; | |
| rightProx = 0; | |
| digitalWrite(13,LOW); | |
| } | |
| //uncomment the lines below to calibrate sensor threshold value | |
| //Serial.print("Calibrating sensor value: "); | |
| //Serial.println(sensorValue); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment