Sensor taking the status of a blinking led of an alarm system
-
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/^/ / -
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/^/ / -
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?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:

-
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
-
@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?
@Tibus said:
why not use sleep.
you are correct, of course. I look at it as a two step approach:
- get it to work
- 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
-
@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
-
Nano - so VIN! Thanks!