rf433ctl passende werte fuer led
[svn42.git] / rf433ctl / rf433ctl.pde
index f83a680..9878159 100644 (file)
@@ -13,8 +13,8 @@
 #define BLUELED_PWM_PIN 6
 #define PHOTO_ANALOGPIN 0
 //movement is reported if during IR_SAMPLE_DURATION at least IR_TRESHOLD ir signals are detectd
-#define IR_SAMPLE_DURATION 20000
-#define IR_TRESHOLD 13000
+#define IR_SAMPLE_DURATION 15000
+#define IR_TRESHOLD 10000
 //duration PanicButton needs to be pressed before status change occurs (i.e. for two PanicButton Reports, the buttons needs to be pressed 1000 cycles, releases 1000 cycles and again pressed 1000 cycles)
 #define PB_TRESHOLD 1000
 #define PHOTO_SAMPLE_INTERVAL 4000
@@ -262,19 +262,27 @@ bool wait_millis(unsigned long ms)
 }
 
 unsigned int flash_led_time_=0;
+unsigned int flash_led_brightness_=256;
+unsigned int flash_led_delay_=8;
 void calculate_led_level(unsigned int pwm_pin)
 {
   if (flash_led_time_ == 0)
     return;
-  if (wait_millis(10))
+  if (wait_millis(flash_led_delay_))
     return;
   flash_led_time_--;
-  int c = abs(sin(float(flash_led_time_) / 100.0)) * 256;
+  int c = abs(sin(float(flash_led_time_) / 100.0)) * flash_led_brightness_;
   analogWrite(pwm_pin,c);
 }
 
-void flash_led(int times)
+void flash_led(unsigned int times, unsigned int brightness_divisor, unsigned int delay_divisor)
 {
+  unsigned int new_flash_led_brightness = 256 / brightness_divisor;
+  unsigned int new_flash_led_delay = 8 / delay_divisor;
+  if (flash_led_time_ == 0 || new_flash_led_brightness > flash_led_brightness_)
+    flash_led_brightness_=new_flash_led_brightness;
+  if (flash_led_time_ == 0 || new_flash_led_delay < flash_led_delay_)
+    flash_led_delay_=new_flash_led_delay;
   flash_led_time_ += 314*times;
 }
 
@@ -289,6 +297,8 @@ void setup()
   pinMode(PANIC_BUTTON_PIN, INPUT);      // set pin to input
   digitalWrite(PANIC_BUTTON_PIN, HIGH);  // turn on pullup resistors 
   analogWrite(BLUELED_PWM_PIN,0);
+
+  Serial.begin(9600);
   
   onewire.reset();
   onewire.reset_search();
@@ -296,9 +306,7 @@ void setup()
   //in case we change temp sensor:
   if (!dallas_sensors.getAddress(onShieldTemp, 0)) 
     Serial.println("Error: Unable to find address for Device 0"); 
-  dallas_sensors.setResolution(onShieldTemp, 9);
-  
-  Serial.begin(9600);
+  dallas_sensors.setResolution(onShieldTemp, 9);  
 }
 
 unsigned int ir_time=IR_SAMPLE_DURATION;
@@ -308,6 +316,13 @@ boolean pb_state=0;
 boolean pb_postth_state=0;
 unsigned int pb_time=0;
 
+void sensorEchoCommand(char command)
+{
+  Serial.print("Sensor ");
+  Serial.print(command);
+  Serial.print(": ");
+}
+
 void loop()
 {
   ir_time--;
@@ -321,7 +336,7 @@ void loop()
   {
     if (ir_count >= IR_TRESHOLD)
     {
-      flash_led(1);
+      flash_led(1,8,1);
       Serial.println("movement");
     }
     ir_time=IR_SAMPLE_DURATION;
@@ -334,7 +349,7 @@ void loop()
     {   
       pb_postth_state=1;
       Serial.println("PanicButton");
-      flash_led(4);
+      flash_led(7,1,2);
     }
     else if (!pb_state)
       pb_postth_state=0;
@@ -387,10 +402,15 @@ void loop()
     else if(command == 'h')
       send_frame(words[D2_OFF]);
     else if(command == 'T')
+    {
+      sensorEchoCommand(command);
       printTemperature(onShieldTemp);
+    }
     else if(command == 'P')
+    {
+      sensorEchoCommand(command);
       printLightLevel();
-
+    }
     else
       Serial.println("Error: unknown command");
   }