Pylons for safety in the dark

 

With so many people walking around in the dark and looking for switches, I figure this will help when walking and bumping into things:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

code:
#define LED_PIN 3
#define SENSOR_PIN A0

#define SENSE_THRESHOLD 150
#define TRACK_THRESHOLD 230
#define DEBOUNCE_CYCLES 30
#define START_TRACKING_CYCLES 180
#define END_TRACKING_CYCLES 210
#define HAND_MINIMUM_CHANGE 15

int adc_input = 0;
int pwm_output = 0;
int stored_bright = 0xFF;
int target_bright = 0;
int hand_tracked_bright = 0;

boolean lamp_lighted = false;
boolean hand_tracking = false;

unsigned char hand_cycles = 0;
unsigned char debounce_cycles = 0;

int sample_1 = 0;
int sample_2 = 0;
int sample_3 = 0;
int sample_4 = 0;
void setup() {
analogWrite(LED_PIN, 0);
}

void loop() {
sample_1 = analogRead(SENSOR_PIN); delay(1);
sample_2 = analogRead(SENSOR_PIN); delay(1);
sample_3 = analogRead(SENSOR_PIN); delay(1);
sample_4 = analogRead(SENSOR_PIN); delay(1);
adc_input = (sample_1 + sample_2 + sample_3 + sample_4) >> 2;

if(debounce_cycles)
debounce_cycles -= 1;
else {
if(hand_tracking) {
if(adc_input > SENSE_THRESHOLD) {
if(adc_input > TRACK_THRESHOLD + 0xFF)
hand_tracked_bright = 0;
else if(adc_input < TRACK_THRESHOLD)
hand_tracked_bright = 0xff;
else
hand_tracked_bright = 0xFF – (adc_input – TRACK_THRESHOLD);

if(abs(target_bright – hand_tracked_bright) > HAND_MINIMUM_CHANGE || !lamp_lighted) {
target_bright = (target_bright + (hand_tracked_bright > 8 ? hand_tracked_bright : 8)) >> 1;
lamp_lighted = true;
}
hand_cycles = 0;
}
else {
target_bright = pwm_output;
stored_bright = pwm_output;
hand_cycles += 1;
if(hand_cycles == END_TRACKING_CYCLES) {
hand_tracking = false;
hand_cycles = 0;
}
}
}
else {
if(adc_input > SENSE_THRESHOLD) {
hand_cycles += 1;
if(hand_cycles == START_TRACKING_CYCLES) {
hand_tracking = true;
hand_cycles = 0;
}
}
else {
if(hand_cycles) {
lamp_lighted = !lamp_lighted;
target_bright = lamp_lighted ? stored_bright : 0;
debounce_cycles = DEBOUNCE_CYCLES;
}
hand_cycles = 0;
}
}
}

if(pwm_output != target_bright) {
if(pwm_output > target_bright && pwm_output > 0) –pwm_output;
if(pwm_output < target_bright && pwm_output < 0xFF) ++pwm_output;
analogWrite(LED_PIN, pwm_output);
delay((0xFF – abs(target_bright – pwm_output)) >> 5);
}
}

 

Comments are closed.