// ================================================================== // FIRE & PIR SENSOR // // Alexander Revtsov, 14/02/2017 // ================================================================== int loga[64]= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 18, 20, 22, 25, 28, 30, 33, 36, 39, 42, 46, 49, 53, 56, 60, 64, 68, 72, 77, 81, 86, 90, 95, 100, 105, 110, 116, 121, 127, 132, 138, 144, 150, 156, 163, 169, 176, 182, 189, 196, 203, 210, 218, 225, 233, 240, 248, 255 }; int timer=10; // time of delay (msec) int light; // light of LED (0-63) int max_light = 49; // max light of LED [(max_light+min_light)<=63] int min_light = 14; // min light of LED int save_light; // save last light of LED; int last_light = 10; // last light of LED; const int sensorPin = 2; // sensorPin const int ledPin = 13; // Built-in LED const int outPin = 3; // output PWM pin const int hand_modePin = 12; // hand mode pin (0 = ON) const int ledBlinkTime = 500; // Blink one for half a second while calibrating long int max_delta = 20000L; // max time delay to OFF int i = 0; int sensor = 0; // movie sensor: False long int last_millis = 0L; long int delta = 0L; int calibrationTime = 10000; // Wait for the sensor to calibrate (20 - 60 seconds according to datasheet) // 10 Seconds in milliseconds void setup() { pinMode(sensorPin,INPUT_PULLUP); pinMode(hand_modePin, INPUT_PULLUP); pinMode(ledPin, OUTPUT); pinMode(outPin, OUTPUT); randomSeed(1); // Start random generator // We need to wait one minute for the sensor to calibrate // Get out of view of the sensor for this duration! // Blink the LED while calibrating for (unsigned int i=0; i light ON switch(sensor) { case 1: { // movie detection true digitalWrite(ledPin,HIGH); max_light = 49; min_light = 14; timer = random(120)+40; // random delay 40...160 light = random(max_light)+min_light; // LED light save_light=light; if(lightlast_light){ light--; analogWrite(outPin, loga[light]); delay (timer+40); } last_light=save_light; } break; case 0: { // movie detecion false digitalWrite(ledPin,LOW); delta=millis()-last_millis; if(delta>max_delta) { // delay to OFF max_light--; if(max_light<3) max_light=3; min_light--; if(min_light<1) min_light=1; timer = random(120)+40; // random delay 40...160 light = random(max_light)+min_light; // LED light save_light=light; if(lightlast_light){ light--; analogWrite(outPin, loga[light]); delay (timer+100); } last_light=save_light; delay(300); } else { max_light--; if(max_light<10) max_light=10; min_light--; if(min_light<5) min_light=5; timer = random(120)+40; // random delay 40...160 light = random(max_light)+min_light; // LED light save_light=light; if(lightlast_light){ light--; analogWrite(outPin, loga[light]); delay (timer+40); } last_light=save_light; }} break; }}