Elliminate superflous messages
[svn42.git] / rf433ctl / rf433ctl.pde
index 6f5ae7d..f283c5f 100644 (file)
 #define ONE_WIRE_PIN 8
 #define PANIC_BUTTON_PIN 7
 #define BLUELED_PWM_PIN 6
+#define BLUELED2_PWM_PIN 11
 #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
@@ -22,6 +23,7 @@
 OneWire  onewire(ONE_WIRE_PIN);
 DallasTemperature dallas_sensors(&onewire);
 DeviceAddress onShieldTemp = { 0x10, 0xE7, 0x77, 0xD3, 0x01, 0x08, 0x00, 0x3F };
+#define TEMPC_OFFSET_ARDUINO_GENEREATED_HEAT -4.0
 
 typedef struct {
   byte offset;
@@ -203,8 +205,8 @@ void printTemperature(DeviceAddress deviceAddress)
 {
   dallas_sensors.requestTemperatures();
   float tempC = dallas_sensors.getTempC(deviceAddress);
-  Serial.print("Temp C: ");
-  Serial.println(tempC);
+  //Serial.print("Temp C: ");
+  Serial.println(tempC TEMPC_OFFSET_ARDUINO_GENEREATED_HEAT);
   //Serial.print(" Temp F: ");
   //Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
 }
@@ -226,7 +228,7 @@ void updateLightLevel(unsigned int pin)
     return;
   
   unsigned int diff = abs(value - light_level_mean_);
-  if (diff > 250)
+  if (light_level_mean_ < 6 || diff > 250)
     light_level_mean_ = value;
   else
       light_level_mean_=(unsigned int) ( ((float) light_level_mean_) * 0.98 + ((float)value)*0.02 );
@@ -234,7 +236,7 @@ void updateLightLevel(unsigned int pin)
 
 void printLightLevel()
 {
-  Serial.print("Photo: ");
+  //Serial.print("Photo: ");
   Serial.println(light_level_mean_);
 }
 
@@ -261,19 +263,35 @@ bool wait_millis(unsigned long ms)
 }
 
 unsigned int flash_led_time_=0;
+unsigned int flash_led_brightness_=255;
+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;
-  analogWrite(pwm_pin,c);
+  int c = abs(sin(float(flash_led_time_) / 100.0)) * flash_led_brightness_;
+  //int d = abs(sin(float(flash_led_time_) / 100.0)) * flash_led_brightness_;
+  analogWrite(BLUELED2_PWM_PIN, 255-c);
+  if (flash_led_brightness_ == 255)
+  {
+    if (flash_led_time_)
+      analogWrite(BLUELED_PWM_PIN, 255-c);
+    else
+      analogWrite(BLUELED_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 = 255 / 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;
 }
 
@@ -288,6 +306,9 @@ 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);
+  analogWrite(BLUELED2_PWM_PIN,255); //pwm sink(-) instead of pwm + (better for mosfets)
+
+  Serial.begin(9600);
   
   onewire.reset();
   onewire.reset_search();
@@ -295,9 +316,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;
@@ -307,6 +326,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--;
@@ -320,7 +346,7 @@ void loop()
   {
     if (ir_count >= IR_TRESHOLD)
     {
-      flash_led(1);
+      flash_led(1,8,1);
       Serial.println("movement");
     }
     ir_time=IR_SAMPLE_DURATION;
@@ -333,7 +359,7 @@ void loop()
     {   
       pb_postth_state=1;
       Serial.println("PanicButton");
-      flash_led(7);
+      flash_led(7,1,2);
     }
     else if (!pb_state)
       pb_postth_state=0;
@@ -386,10 +412,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");
   }