Skip to content

Instantly share code, notes, and snippets.

@Craigson
Created October 11, 2014 23:32
Show Gist options
  • Save Craigson/c8b2fbe1eaf34f7848df to your computer and use it in GitHub Desktop.
Save Craigson/c8b2fbe1eaf34f7848df to your computer and use it in GitHub Desktop.
IR gesture detector
//code based on Ricardo Uvina's instrucables project, found here: http://www.instructables.com/id/Simple-IR-proximity-sensor-with-Arduino/?ALLSTEPS
//P-Comp mid-term
//Craig Pickard and Minju Vivan Kim
//IR swipe detector
//11 October 2014
int IRpin = A0; // IR photodiode on analog pin A0
int IRemitter = 2; // IR emitter LED on digital pin 2
int thresholdLEDpin = 12; // red LED on digital pin 12.
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 swiped = 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); // initializing Serial monitor
pinMode(IRemitter,OUTPUT); // IR emitter LED on digital pin 2
digitalWrite(IRemitter,LOW);// setup IR LED as off
pinMode(12, OUTPUT); // LED on pin 12 set as output
digitalWrite(12, LOW); // set red LED initially LOW
}
void loop(){
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
sensorState = 1;
}
else {
sensorState = 0;
}
if (sensorState != previousSensorState){ //if state change is detected, set swipe variable to true
swiped = true;
}
else {
swiped = false;
}
if (swiped == true){ //if the user has swiped, perform the following actions:
digitalWrite(13,HIGH);
Serial.print("swipe detected!");
Serial.print(" ");
Serial.print("Sensor Reading: ");
Serial.print(sensorValue);
Serial.print(" ");
Serial.print("Threshold: ");
Serial.println(sensorThreshold);
delay(500);
}
else {
digitalWrite(13,LOW);
}
//Serial.println(sensorValue);
//Serial.println(analogRead(IRpin)); // uncomment to activate the buzzer function
Serial.println(analogRead(IRpin));
}
int readIR(int times){
for(int x=0;x<times;x++){
digitalWrite(IRemitter,LOW); // turning the IR LEDs off to gauge ambient IR light from environment
delay(1); // minimum delay necessary to read values
ambientIR = analogRead(IRpin); // storing IR coming from the ambient
digitalWrite(IRemitter,HIGH); // turning the IR LEDs on to read the IR coming from the obstacle
delay(1); // minimum delay necessary to read values
obstacleIR = analogRead(IRpin); // storing IR coming from the obstacle
value[x] = ambientIR-obstacleIR; // 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"
distance+=value[x];
}
return(distance/times); // return the final value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment