Sensor taking the status of a blinking led of an alarm system



  • Hi,

    my alarmsystem does not tell anything else about the status then blinking an led.

    LED Blinking: Alarmsystem active (1 Sec. on, 1 Sec off,...)
    LED OFF: Alarmsystem deacticated

    I would like to read this blinking led with a light sensor and send the status "active" or "inactive" to my controller.

    Any idea how the code could look?

    many thanks

    Petz



  • Ok, I'm going to do it like this:

    Taking a Photoresistor and taking readings vor 10 sec. after that I sum up all readings. If light was blinking the sum is much higher than if not blinking and so I determine the status. Active - not Active. Done



  • It depend if it's in battery or not. If not on battery you can save de "millis()" of each turn on and if the curent millis()-lastTurnOn >5000 your led do not turn on since 5seconds. Of you can also add an interrupt on the photoresistor, save the millis, go to sleep with the interrupt and à timer. Each interrupt will update the current millis and if you don't have a interrupt for 5secinds, the sleep timer will wake the arduino and the same calcul to know that the millis don't change since 5seconds.

    I don't know if it's clear ;D?


  • Contest Winner

    @Meister_Petz

    I would open the alarm interface and solder in a couple of small wires... you don't want any ambient light n the photocell and covering the led seems ugly...

    then use the method @Tibus mentioned... use an interrupt on CHANGE and a timer to transmit off status if the timer expires.

    It think it would be prettier and more reliable. If you are living with a spouse or significant other, you will see the benefits!

    PS you can power the whole thing from the alarm interface ... try checking the power voltage to the interface. all you need is at least 3V3 and a few caps. you may be able to fit the pro-mini and the radio inside the interface!



  • @Tibus
    being new at programmig for arduino, I'm not really sure how to put this into code 😞
    could you give me an code example how you do that?

    @BulldogLowell
    I agree! When the warranty runs out, I'll take the opening and soldering approach 😉 But even at the moment it's not that bad, because the Alarmterminal is hidden and for switching it on, I use my controller.

    Many thanks for your help!
    Petz


  • Contest Winner

    @Meister_Petz

    if you need it to run on battery, then you may not want to bother with sleep, since you are looking for the non-occurrence of something. 😉

    the interrupt method will wake the MCU every time the led flashes...

    you could start with code like this (untested):

    EDITED

    #include <MySensor.h>  
    #include <SPI.h>
    
    typedef enum{
      NOT_ARMED = 0, ARMED
    } 
    AlarmState;
    
    AlarmState state;
    
    AlarmState lastAlarmState;
    
    unsigned long lastFlashTime;
    
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your light sensor
    
    #define CHILD_ID 1   // Id of the sensor child
    
    MySensor gw;
    MyMessage msg(CHILD_ID, V_TRIPPED);
    
    void setup()  
    { 
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input 
      gw.begin();
      gw.sendSketchInfo("Motion Sensor", "1.0");
      delay(1000);
      gw.present(CHILD_ID, S_MOTION);
      delay(1000);
    }
    
    void loop()     
    {     
      // Read digital light sensor value
      if (digitalRead(DIGITAL_INPUT_SENSOR) == HIGH)
      {
        lastFlashTime = millis();
        state = ARMED;
        if (lastAlarmState == NOT_ARMED)
        {
          gw.send(msg.set(ARMED));  // Send tripped value to gw 
        } 
      }
      if (lastFlashTime - millis() > 5000UL && state == ARMED) // you may have to adjust... this is 5 seconds
      {
        state = NOT_ARMED;
        if (lastAlarmState == ARMED)
        {
          gw.send(msg.set(NOT_ARMED));  // Send not tripped value to gw 
        }
      }
      lastAlarmState = state;
    }


  • Many Thanks for the code, I will start to play around asap 😉


  • Contest Winner

    @Meister_Petz

    OK, just check the edit... so it is not banging away at your gateway!!!



  • btw, thats was my working apporach before:
    is there a nice way to add code? Do I really have to add 4 spaces before every line?


  • Hero Member

    is there a nice way to add code? Do I really have to add 4 spaces before every line?

    Yes, that is the way to format code.

    But ... knowing some tools there is no need to do it by hand. In vi/vim:

    :%s/^/    /

  • Admin

    @daulagari

    or just surround code-block with 3 backtick-characters. This character ----> `

    #include <hehe.h>
    
    printf();
    
    

  • Contest Winner

    @Meister_Petz

    or in your arduino editor, just grab all the code (CTRL+A) and then increase indent twice... on my mack it is (COMMAND+ ]) That moves all the code right four spaces... see below:

    Screen Shot 2015-01-22 at 9.36.15 PM.png



  • MANY THANKS!

    That is what I did until now without Millis and interupts,...

    In this code I determine blinks by the difference between 2 following readings. Is there a better way?

    And I simplified the message sent to the gateway quite a bit. Do I produce any errors with this?

      /* Small sketch to determine the State of a Olympia Protect Alarmsystem
      0. When UNARMED   : LED off - DISPLAY off
      1. When ARMED     : LED blinks
      2. When ACTIVATING: LED blinks - DISPLAY blinks (check if before State was ARMED)
      3. When TRIGGERED : LED blinks - DISPLAY blinks (check if before State was ARMED)
      */
      
      #include <MySensor.h>
      #include <SPI.h>
      #define CHILD_ID 3
      MySensor gw;
      MyMessage msg(CHILD_ID,V_TRIPPED);
      
      const int phCell1Pin = 0; // Photocell 1 on PIN 0 - Display
      const int phCell2Pin = 1; // Photocell 2 on PIN 1 - LED
      const int phCell1Sensitivity = 100; // sets the minimum difference between two readings to determine blinking of Display
      const int phCell2Sensitivity = 100; // sets the minimum difference between two readings to determine blinking of LED
      const int sleepTime = 7000; // 7 Sec. between Reading Cycles
      int armingTime = 30; // Seconds set until arming is done
      
      
      int DisplayState = 0;
      int LEDState = 0;
      int AlarmSystemState = 4; // (0 = Off, 1 = On, 2 = Activating, 3 = Triggered)
      int AlarmSystemStateOld = 0;
      int myRound1[10] = {0,0,0,0,0,0,0,0,0,0}; // Array to store Readings
      int myRound2[10] = {0,0,0,0,0,0,0,0,0,0}; // Array to store Readings
      int goSleep = 0;
      int i = 0;
      int i1 = 0;
      int i2 = 0;
      
      void setup(void) {
        gw.begin();
        gw.present(CHILD_ID, S_DOOR);
      }
       
      void loop(void) {
           if (i < 10){ // 10 Readings
             myRound1[i] = analogRead(phCell1Pin); // read Photosensor Display 
             //Serial.print("Display PhCell:"); Serial.println(myRound1[i]);
             myRound2[i] = analogRead(phCell2Pin); // read Photosensor LED 
             //Serial.print("LED     PhCell:"); Serial.println(myRound2[i]);
             
             if (i > 0){ // compare 2 following readings
               // Display
               if ((myRound1[i] - myRound1[i-1]) > phCell1Sensitivity){i1++;}
               if ((myRound1[i-1] - myRound1[i]) > phCell1Sensitivity){i1++;}
               // LED
               if ((myRound2[i] - myRound2[i-1]) > phCell2Sensitivity){i2++;}
               if ((myRound2[i-1] - myRound2[i]) > phCell2Sensitivity){i2++;}
             }
             i++;
             
           } else {
             
             // Display
             if (i1 > 2){ // if there are more than 3 Readings which are assumed blinks - set DisplayState to blink
               DisplayState = 1; //Serial.println("Display blinkt - State 1");
             } else {
               DisplayState = 0; // Serial.println("Display dunkel - State 0");
             }
             
             //LED
             if (i2 > 2){ // if there are more than 3 Readings which are assumed blinks - set LEDState to blink
               LEDState = 1; //Serial.println("LED blinkt - State 1");
             } else {
               LEDState = 0; // Serial.println("LED dunkel - State 0");
             }
      
             goSleep = 1; // now transmit if necessary and wait before starting again
             // Reset
             i = 0;
             i1 = 0;
             i2 = 0;
             int myRound1[10] = {0,0,0,0,0,0,0,0,0,0};
             int myRound2[10] = {0,0,0,0,0,0,0,0,0,0};
           }
      
           // send to Sleep
           if (goSleep == 1){
             if (DisplayState == 0 && LEDState == 0){
               AlarmSystemState = 0; // OFF
             } else if (DisplayState == 0 && LEDState == 1){
               AlarmSystemState = 1; // ON
             } else if (DisplayState == 1 && LEDState == 1 && AlarmSystemStateOld == 0){
               AlarmSystemState = 2; // Actviating
               //delay(70000); // wait 70 Seconds until Activating is done 
             } else if (DisplayState == 1 && LEDState == 1 && AlarmSystemStateOld == 1){
               AlarmSystemState = 3; // TRIGGERED
             } else {
              AlarmSystemState = AlarmSystemStateOld;
             }
             
      
             // Send State change to Gateway
             //Serial.print("AlarmSystemState Änderung:");Serial.println(AlarmSystemState);
             gw.send(msg.set(AlarmSystemState));
             
             if (AlarmSystemState == 2){ // Delay until Arming is done before next read
               armingTime = (armingTime * 1000)+1000;
               delay(armingTime);
             }
             
             AlarmSystemStateOld = AlarmSystemState;
             delay(sleepTime);
             goSleep = 0;
           } else {
             delay(575);
           }
      }


  • @BulldogLowell said:

    if you need it to run on battery, then you may not want to bother with sleep, since you are looking for the non-occurrence of something.

    the interrupt method will wake the MCU every time the led flashes...

    you could start with code like this (untested):

    why not use sleep. Yes it will wake the MCU every time the led flashes but it 'll consume less power when it sleep. you can go from 7ma to 10ua when it sleep. Even for one seconds it's a benefit no?



  • Why not measure when there is voltage to the led??? Much more reliable and simple


  • Contest Winner

    @Tibus said:

    why not use sleep.

    you are correct, of course. I look at it as a two step approach:

    1. get it to work
    2. get it to work better than before


  • @Tibus
    I agree about sleeping. But for one thing I don't have the slightest clue how working with interupts works (yet) and the Arduino is hook on the power of the Alarmsystem 😉

    BTW my Alarmsystem Powersupply produces 6V. Would it make more sense to use a 5V Step Down Regulator and give power to pin 27 (5V) than 6V to PIN 30 (VIN)?

    Many Thanks

    Petz


  • Contest Winner

    @Meister_Petz

    Pro-Mini? I'd just take the 6V to the RAW pin.

    6V to Vin on a nano.



  • Nano - so VIN! Thanks!


Log in to reply
 

Suggested Topics

  • 1
  • 1
  • 5
  • 2
  • 6
  • 1

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts