Led und PhotoDiodeo
[svn42.git] / rf433ctl / rf433ctl.pde
index 6b541e2..e6ba1bd 100644 (file)
 #define IR_MOVEMENT_PIN 9
 #define ONE_WIRE_PIN 8
 #define PANIC_BUTTON_PIN 7
+#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
 //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
 
 OneWire  onewire(ONE_WIRE_PIN);
 DallasTemperature dallas_sensors(&onewire);
@@ -208,6 +211,54 @@ void printTemperature(DeviceAddress deviceAddress)
 
 //********************************************************************//
 
+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 > 250)
+    light_level_mean_ = value;
+  else
+      light_level_mean_=(unsigned int) ( ((float) light_level_mean_) * 0.98 + ((float)value)*0.02 );
+}
+
+void printLightLevel()
+{
+  Serial.print("Photo: ");
+  Serial.println(light_level_mean_);
+}
+
+//********************************************************************//
+
+unsigned int flash_led_time_=0;
+void calculate_led_level(unsigned int pwm_pin)
+{
+  if (flash_led_time_ == 0)
+    return;
+  if (millis() % 10)
+    return;
+  flash_led_time_--;
+  int c = abs(sin(float(flash_led_time_) / 100.0)) * 256;
+  analogWrite(pwm_pin,c);
+}
+
+void flash_led(int times)
+{
+  flash_led_time_=314*times;
+}
+
+//********************************************************************//
+
 void setup()
 {
   pinMode(RF_DATA_OUT_PIN, OUTPUT);
@@ -216,13 +267,14 @@ void setup()
   digitalWrite(IR_MOVEMENT_PIN, LOW);  // turn off pullup resistors  
   pinMode(PANIC_BUTTON_PIN, INPUT);      // set pin to input
   digitalWrite(PANIC_BUTTON_PIN, HIGH);  // turn on pullup resistors 
+  analogWrite(BLUELED_PWM_PIN,0);
   
   onewire.reset();
   onewire.reset_search();
   dallas_sensors.begin();
   //in case we change temp sensor:
   if (!dallas_sensors.getAddress(onShieldTemp, 0)) 
-    Serial.println("Unable to find address for Device 0"); 
+    Serial.println("Error: Unable to find address for Device 0"); 
   dallas_sensors.setResolution(onShieldTemp, 9);
   
   Serial.begin(9600);
@@ -247,7 +299,10 @@ void loop()
   if (ir_time == 0)
   {
     if (ir_count >= IR_TRESHOLD)
+    {
+      flash_led(1);
       Serial.println("movement");
+    }
     ir_time=IR_SAMPLE_DURATION;
     ir_count=0;
   }
@@ -258,6 +313,7 @@ void loop()
     {   
       pb_postth_state=1;
       Serial.println("PanicButton");
+      flash_led(7);
     }
     else if (!pb_state)
       pb_postth_state=0;
@@ -267,47 +323,52 @@ void loop()
     pb_time=0;
     pb_last_state=pb_state;
   }
-    
+  
+  updateLightLevel(PHOTO_ANALOGPIN);
+  calculate_led_level(BLUELED_PWM_PIN);
+  
   if(Serial.available()) {
     char command = Serial.read();
     
-    if(command == 'q')
+    if(command == 'A')
       send_frame(words[A1_ON]);
     else if(command == 'a')
       send_frame(words[A1_OFF]);
-    else if(command == 'w')
+    else if(command == 'B')
       send_frame(words[A2_ON]);
-    else if(command == 's')
+    else if(command == 'b')
       send_frame(words[A2_OFF]);
 
-    else if(command == 'e')
+    else if(command == 'C')
       send_frame(words[B1_ON]);
-    else if(command == 'd')
+    else if(command == 'c')
       send_frame(words[B1_OFF]);
-    else if(command == 'r')
+    else if(command == 'D')
       send_frame(words[B2_ON]);
-    else if(command == 'f')
+    else if(command == 'd')
       send_frame(words[B2_OFF]);
 
-    else if(command == 't')
+    else if(command == 'E')
       send_frame(words[C1_ON]);
-    else if(command == 'g')
+    else if(command == 'e')
       send_frame(words[C1_OFF]);
-    else if(command == 'z')
+    else if(command == 'F')
       send_frame(words[C2_ON]);
-    else if(command == 'h')
+    else if(command == 'f')
       send_frame(words[C2_OFF]);
 
-    else if(command == 'u')
+    else if(command == 'G')
       send_frame(words[D1_ON]);
-    else if(command == 'j')
+    else if(command == 'g')
       send_frame(words[D1_OFF]);
-    else if(command == 'i')
+    else if(command == 'H')
       send_frame(words[D2_ON]);
-    else if(command == 'k')
+    else if(command == 'h')
       send_frame(words[D2_OFF]);
     else if(command == 'T')
       printTemperature(onShieldTemp);
+    else if(command == 'P')
+      printLightLevel();
 
     else
       Serial.println("Error: unknown command");