+unsigned int light_level_mean_ = 0;
+unsigned int light_sample_time_ = 0;
+
+void updateLightLevel(unsigned int pin)
+{
+ light_sample_time_++;
+ if (light_sample_time_ < PHOTO_SAMPLE_INTERVAL)
+ return;
+ light_sample_time_ = 0;
+
+ unsigned int value = analogRead(pin);
+ if (value == light_level_mean_)
+ return;
+
+ unsigned int diff = abs(value - light_level_mean_);
+ if (diff > 100)
+ light_level_mean_ = value;
+ else
+ light_level_mean_=(unsigned int) ( ((float) light_level_mean_) * 0.90 + ((float)value)*0.10 );
+}
+
+void printLightLevel()
+{
+ //Serial.print("Photo: ");
+ Serial.println(light_level_mean_);
+}
+
+//********************************************************************//
+
+unsigned long wm_start_[3]={0,0,0};
+bool wait_millis(unsigned long *start_time, unsigned long ms)
+{
+ if (ms == 0)
+ return false;
+ else if (*start_time > 0)
+ {
+ if (millis() < *start_time || millis() > (*start_time) + ms)
+ {
+ *start_time = 0;
+ return false;
+ }
+ else
+ return true;
+ }
+ else
+ {
+ *start_time=millis();
+ return true;
+ }
+}
+#define NUM_LEDS 2
+char flash_led_pins_[NUM_LEDS]={BLUELED_PWM_PIN,PANICLED_PWM_PIN};
+unsigned int flash_led_time_[3]={0,0,0};
+unsigned int flash_led_brightness_[3]={255,255,255};
+unsigned int flash_led_delay_[3]={8,8,8};
+unsigned int flash_led_initial_delay_[3]={0,0,0};
+void calculate_led_level()
+{
+ for (int ledid = 0; ledid < NUM_LEDS; ledid++)
+ {
+ if (flash_led_time_[ledid] == 0)
+ continue;
+ if (wait_millis(wm_start_ + ledid, flash_led_initial_delay_[ledid]))
+ continue;
+ flash_led_initial_delay_[ledid]=0;
+ if (wait_millis(wm_start_ + ledid, flash_led_delay_[ledid]))
+ continue;
+ flash_led_time_[ledid]--;
+ int c = abs(sin(float(flash_led_time_[ledid]) / 100.0)) * flash_led_brightness_[ledid];
+ //int d = abs(sin(float(flash_led_time_) / 100.0)) * flash_led_brightness_;
+ analogWrite(flash_led_pins_[ledid], 255-c);
+ }
+}
+
+// id: id of LED to flash (0,1)
+// times: # of times the LED should flash
+// brightness_divisor: 1: full brightness, 2: half brightness, ...
+// delay_divisor: 1: slow... 8: fastest
+// phase_divisor: 0.. same phase; 2.. pi/2 phase, 4.. pi phase, 6.. 3pi/2 phase
+void flash_led(unsigned int id, unsigned int times, unsigned int brightness_divisor, unsigned int delay_divisor, unsigned int phase_divisor)
+{
+ if (id >= NUM_LEDS)
+ return;
+ unsigned int new_flash_led_brightness = 255;
+ unsigned int new_flash_led_delay = 8;
+ if (times == 0)
+ {
+ analogWrite(flash_led_pins_[id],255); //off
+ return;
+ }
+ if (brightness_divisor > 1) //guard against div by zero
+ new_flash_led_brightness /= brightness_divisor;
+ if (delay_divisor > 1) //guard against div by zero
+ new_flash_led_delay /= delay_divisor;
+ if (flash_led_time_[id] == 0 || new_flash_led_brightness > flash_led_brightness_[id])
+ flash_led_brightness_[id]=new_flash_led_brightness;
+ if (flash_led_time_[id] == 0 || new_flash_led_delay < flash_led_delay_[id])
+ flash_led_delay_[id]=new_flash_led_delay;
+ flash_led_time_[id] += 314*times;
+ flash_led_initial_delay_[id] = flash_led_delay_[id]*314*phase_divisor/8;
+}
+
+//********************************************************************//
+
+int save_tcnt2=0;
+int save_tccr2a=0;
+int save_tccr2b=0;
+void reset_timer2()