Created
October 23, 2014 01:46
-
-
Save Craigson/89a119425c245a081400 to your computer and use it in GitHub Desktop.
Instagram TUI - Arduino Code
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 = 0; | |
| 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; | |
| ///--------------LEFT proximity sensor----------------------- | |
| int leftIRpin = A0; // IR photodiode on analog pin A0 | |
| int leftIRemitter = 13; // IR emitter LED on digital pin 2 | |
| int leftIndicatorPin = 12; // red LED on digital pin 12. | |
| int leftAmbientIR; // variable to store the IR coming from the ambient | |
| int leftObstacleIR; // variable to store the IR coming from the object | |
| int leftValue[10]; // variable to store the IR values | |
| int leftDistance; // 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 leftSensorThreshold = 16; //set threshold value to detect presence of user's hand | |
| int leftSensorState; //the IR detectors current state (HIGH or LOW) | |
| int leftPreviousSensorState = 0; //the IR detectors previous state (HIGH or LOW) | |
| //----------------------------------------------------------- | |
| //---------------right proximity sensor--------------------- | |
| int rightIRpin = A1; // IR photodiode on analog pin A0 | |
| int rightIRemitter = 19; // IR emitter LED on digital pin 2 | |
| int rightIndicatorPin = 11; // red LED on digital pin 12. | |
| int rightAmbientIR; // variable to store the IR coming from the ambient | |
| int rightObstacleIR; // variable to store the IR coming from the object | |
| int rightValue[10]; // variable to store the IR values | |
| int rightDistance; // variable that will tell if there is an obstacle or not | |
| boolean swipedRight = false; //boolean variable to establish whether or not swipe has been detected | |
| int rightSensorThreshold = 18; //set threshold value to detect presence of user's hand | |
| int rightSensorState; //the IR detectors current state (HIGH or LOW) | |
| int rightPreviousSensorState = 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(leftIRemitter,OUTPUT); // IR emitter LED on digital pin 2 | |
| digitalWrite(leftIRemitter,LOW);// setup IR LED as off | |
| pinMode(leftIndicatorPin, OUTPUT); // LED on pin 12 set as output | |
| digitalWrite(leftIndicatorPin, LOW); // set red LED initially LOW | |
| pinMode(rightIRemitter,OUTPUT); // IR emitter LED on digital pin 2 | |
| digitalWrite(rightIRemitter,LOW);// setup IR LED as off | |
| pinMode(rightIndicatorPin, OUTPUT); // LED on pin 12 set as output | |
| digitalWrite(rightIndicatorPin, 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); | |
| //-------------LEFT proximity sensor code ----------------------------------- | |
| leftDistance = readLeftIR(5); // calling the function that will read the distance and passing the "accuracy" to it | |
| int leftSensorValue = abs(leftDistance); //creates positive values to evaluate | |
| //Serial.println(sensorValue); | |
| if (leftSensorValue > leftSensorThreshold) { //if the reading of the IR detector is above the threshold value, the user's hand is present | |
| leftSensorState = 1; | |
| swipedLeft = true; | |
| digitalWrite(leftIndicatorPin,HIGH); | |
| } | |
| //-------------RIGHT proximity sensor code ----------------------------------- | |
| rightDistance = readRightIR(5); // calling the function that will read the distance and passing the "accuracy" to it | |
| int rightSensorValue = abs(rightDistance); //creates positive values to evaluate | |
| //Serial.println(sensorValue); | |
| if (rightSensorValue > rightSensorThreshold) { //if the reading of the IR detector is above the threshold value, the user's hand is present | |
| rightSensorState = 1; | |
| swipedRight = true; | |
| digitalWrite(rightIndicatorPin,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; | |
| } | |
| } | |
| //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; | |
| } | |
| if(swipedRight == true){ | |
| rightProx = 1; | |
| } | |
| //print string of three characters to be sent to Processing, eg. "0,1,0" | |
| Serial.print(buttonValue); | |
| Serial.print(","); | |
| Serial.print(leftProx); | |
| Serial.print(","); | |
| Serial.println(rightProx); | |
| //reset all values to zero/false | |
| swipedLeft = false; | |
| swipedRight = false; | |
| buttonValue = 0; | |
| leftProx = 0; | |
| rightProx = 0; | |
| digitalWrite(12,LOW); | |
| digitalWrite(11,LOW); | |
| } | |
| //uncomment the below lines to calibrate the system | |
| //Serial.print(leftSensorValue); | |
| //Serial.print(" "); | |
| //Serial.println(rightSensorValue); | |
| } | |
| //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); | |
| } | |
| } | |
| int readRightIR(int times){ | |
| for(int x=0;x<times;x++){ | |
| digitalWrite(rightIRemitter,LOW); // turning the IR LEDs off to gauge ambient IR light from environment | |
| delay(1); // minimum delay necessary to read values | |
| rightAmbientIR = analogRead(rightIRpin); // storing IR coming from the ambient | |
| digitalWrite(rightIRemitter,HIGH); // turning the IR LEDs on to read the IR coming from the obstacle | |
| delay(1); // minimum delay necessary to read values | |
| rightObstacleIR = analogRead(rightIRpin); // storing IR coming from the obstacle | |
| rightValue[x] = rightAmbientIR-rightObstacleIR; // calculating changes in IR values and storing it for future average | |
| } | |
| for(int x=0;x<times;x++){ // calculating the average based on the "accuracy" | |
| rightDistance+=rightValue[x]; | |
| } | |
| return(rightDistance/times); // return the final value | |
| } | |
| int readLeftIR(int times){ | |
| for(int x=0;x<times;x++){ | |
| digitalWrite(leftIRemitter,LOW); // turning the IR LEDs off to gauge ambient IR light from environment | |
| delay(1); // minimum delay necessary to read values | |
| leftAmbientIR = analogRead(leftIRpin); // storing IR coming from the ambient | |
| digitalWrite(leftIRemitter,HIGH); // turning the IR LEDs on to read the IR coming from the obstacle | |
| delay(1); // minimum delay necessary to read values | |
| leftObstacleIR = analogRead(leftIRpin); // storing IR coming from the obstacle | |
| leftValue[x] = leftAmbientIR-leftObstacleIR; // calculating changes in IR values and storing it for future average | |
| } | |
| for(int x=0;x<times;x++){ // calculating the average based on the "accuracy" | |
| leftDistance+=leftValue[x]; | |
| } | |
| return(leftDistance/times); // return the final value | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment