/* FILENAME: OpenFlyDetector.ino AUTHOR: Orlando S. Hoilett Matthew T. Dougan DATE: Sunday, October 5, 2014 Version: 0.1 CALVARY ENGINEERING FAMILY GROUP UPDATES: Version 0.0 10/05/2014:0800> Tried using photodetectors to do detection, but I did not get the performance I liked. Version 0.1 10/05/2014:1330> Switch to simple photodiode and resistor circuit. Version 0.2 10/13/2014:2221> Made a few modifications for Instructables.com upload. DESCRIPTION: Use an photodiode with a resistor circuit to detect when an IR LED passes it. There is a detector at the top and bottom of the zipper If the top detector is triggered first and then the bottom dector is also triggered, this is a "ZIP DOWN" event If the bottom detector is triggered first and then the top dector is also triggered, this is a "ZIP UP" event If there is a "ZIP DOWN" event without a corresponding "ZIP UP" event within a certain amount of time, then trigger a buzzer to tell the wearer that his/her zipper is down DISCLAIMER This code is provided for you to freely use and modify however you chose, but please give a reference to the origianl author as a courtesy to open source developers. Have fun! */ //IR emitter variable definitions const int IR_LED = 9; //IR detector #1 (top detector) const int IR_detect_top = A0; int top_detector = 0; boolean top_detect_status = false; unsigned long top_detect_timer = 0; //IR detector #2 (bottom detector) const int IR_detect_bottom = A1; int bottom_detector = 0; boolean bottom_detect_status = false; unsigned long bottom_detect_timer = 0; const int active_detector_threshold = 90; const unsigned long next_zip_event_threshold = 3000; //in seconds unsigned long next_zip_event_timer = 0; boolean zip_UP = false; boolean zip_DOWN = false; //red LED to indicate when zipper is down const int zipper_down_indicator = 8; void setup() { //We do a few prints to the serial monitor. This was for //debugging reasons Serial.flush(); Serial.begin(9600); Serial.flush(); pinMode(IR_LED, OUTPUT); digitalWrite(IR_LED, HIGH); } void loop() { int top_detector = analogRead(IR_detect_top); int bottom_detector = analogRead(IR_detect_bottom); //for debugging purposes // Serial.print("top_detector: "); // Serial.println(top_detector); // Serial.print("bottom_detector: "); // Serial.println(bottom_detector); //for debugging purposes // Serial.println(top_detect_status); // Serial.println(bottom_detect_status); // Serial.println(zip_UP); // Serial.println(zip_DOWN); //if top and down detectors are triggered, set status indicators //to "true" and note at what time these detectors were triggered if (top_detector >= active_detector_threshold) { top_detect_status = true; top_detect_timer = millis(); } if (bottom_detector >= active_detector_threshold) { bottom_detect_status = true; bottom_detect_timer = millis(); } //if both top and bottom detector statuses are true, determine //if a zipDown or zipUp has occurred if (top_detect_status && bottom_detect_status) { //note the time when both detectors are triggered next_zip_event_timer = millis(); //top detector triggered first if (top_detect_timer < bottom_detect_timer) { zip_DOWN = true; } //times are not distinguishable else if(top_detect_timer == bottom_detect_timer) { //don't know what to do... } //bottom detector triggered first else { zip_UP = true; } //reset status variables for next "zipping" event top_detect_status = false; bottom_detect_status = false; } //if the wait time has occurred until the if (millis() - next_zip_event_timer >= next_zip_event_threshold) { //if there has not been a corresponding zipUp event with a //zipDown, then alert the user then the zipper is down if (zip_UP != zip_DOWN) { Serial.println("Your zipper is down!"); digitalWrite(zipper_down_indicator, HIGH); } else { //reset "zipping" statuses Serial.println("Your zipper is OK!"); zip_UP = false; zip_DOWN = false; digitalWrite(zipper_down_indicator, LOW); } } //for debugging purposes if (Serial.read() == '0') { Serial.end(); } delay(5); //for stability }