Skip to content

Instantly share code, notes, and snippets.

@jasonhejna
Last active December 30, 2015 04:51
Show Gist options
  • Save jasonhejna/e5111723e8aa72f5a967 to your computer and use it in GitHub Desktop.
Save jasonhejna/e5111723e8aa72f5a967 to your computer and use it in GitHub Desktop.
When you flick the lights on and off it will trigger. Arduino with a photo-resistor.
// DOUBLE TAP LIGHT 0.1
unsigned long off_timer = 18000;
int power = 3;
int sensor = 5;
int threshold = 190;
unsigned long loop_count = 0;
int sense_val = 0;
int sense_0 = 0;
int sense_1 = 0;
int diff = 0;
int diff2 = 0;
int event_count = 0;
unsigned long event_timeout = 2000000002;
unsigned long light_timeout = 2000000002;
boolean light_status = false;
void setup() {
pinMode(power, OUTPUT);
Serial.begin(9600);
}
void loop() {
sense_val = analogRead(sensor);
//Serial.println(sense_val);
diff = abs(sense_val - sense_0);
diff2 = abs(sense_val - sense_1);
if (diff > threshold || diff2 > threshold) {
// Event: light was turned on or off
if ( event_count > 0 ) {
// ITS A DOUBLE TAP
if ( light_status == false ) {
digitalWrite(power, HIGH);
light_status = true;
}
else{
digitalWrite(power, LOW);
light_status = false;
}
delay(3000);
//digitalWrite(power, LOW);
event_count = 0;
light_timeout = loop_count + off_timer;
}
event_count++;
event_timeout = loop_count + 20;
delay(50);
sense_val = analogRead(sensor);
sense_0 = sense_val;
sense_1 = sense_val;
}
if ( loop_count > event_timeout ) {
// there were no subsequent light change after the first. Reset event_count.
event_count = 0;
event_timeout = 2000000002;
}
if ( loop_count % 2 == 0 ) {
sense_0 = sense_val;
}
else {
sense_1 = sense_val;
}
if ( loop_count > light_timeout ) {
digitalWrite(power, LOW);
light_status = false;
light_timeout = 2000000002;
delay(3000);
sense_val = analogRead(sensor);
sense_0 = sense_val;
sense_1 = sense_val;
}
if ( loop_count > 2000000000 ) {
// don't exceed the max size of an unsigned long
loop_count = 0;
}
loop_count++;
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment