/* * Simple photoresistor-based motion detector * * This project is part of Accrochages * See http://accrochages.drone.ws * * (c) 2008 Sofian Audry (info@sofianaudry.com) * (c) 2008 Samuel St-Aubin (samuel.staubin@gmail.com) * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #define PHOTO_IN 0 // Photoresistor input. #define LED_OUT 13 // Led output (for demonstration). #define THRESHOLD 3 // Threshold (needs to be set by hand). /* * This function returns the motion, calculated as the difference between * two readings of the photoresistor. Motion is expressed as an integer * between -1023 and 1023. */ int motion(long time = 100) { int before = analogRead(PHOTO_IN); delay(time); return (analogRead(PHOTO_IN) - before); } void setup() { pinMode(LED_OUT, OUTPUT); digitalWrite(LED_OUT, LOW); } void loop() { int m = motion(); if (abs(m) > THRESHOLD) digitalWrite(LED_OUT, HIGH); else digitalWrite(LED_OUT, LOW); }