void loop(){
if ((digitalRead(inPin)==HIGH) & (status == 0))
{
measuredPulsesOnline++;
status = 1;
delay(50);
}
else if ((digitalRead(inPin)==LOW) & (status == 1))
{
status = 0;
}
}
I would like to implement System.sleep(inPin, RISING) in the loop. inPin=D1 and this is my reed sensor. The device should fall asleep after, for example, 15 seconds, when inPin==LOW. So, it should not fall directly asleep when inPin==LOW, but after a few seconds. The device should rise when inPin==HIGH.
One thing is, you might want to use the boolean operator && instead of the binary & in your if statements.
Also what pinMode() are you using for inPin?
Since it’s a reed sensor, you’ll have to provide the level for the open sensor via pull-resistors - either INPUT_PULLUP or INPUT_PULLDOWN depending on the level your sensor will close to.
To handle your 15sec timeout, you could use millis() to capture the time the sensor first went LOW and call System.sleep() when the pin stayed low and the difference between your captured time and current millis() is greator or equal 15000
Thank you, it is pinMode(inPin, INPUT);. In this case, it is possible to provide the level of the open sensor this way, as it works. Is the following code corresponding with your advice?
if ((digitalRead(inPin)==LOW) && difference>15000 )
{
System.sleep(inPin, RISING);
}
I do not know how to define the difference. Hope you can help me any further.
I rather doubt that INPUT will not suffice. When you have no pull resistor in place your pin will be floating which means that it can trigger HIGH or LOW randomly.
uint32_t msHold = 0;
int lastState = 0;
void setup()
{
...
pinMode(inPin, INPUT_PULLDOWN); // when closing to HIGH
}
void loop()
{
int state = digitalRead(inPin);
if (state != lastState)
{
lastState = state;
if (state)
{
msHold = 0;
measuredPulsesOnline++;
}
else
{
msHold = millis();
}
delay(50); // debounce
}
if (msHold && (millis() - msHold) >= 15000)
{
System.sleep(inPin, RISING);
msHold = 0;
}
}
Thank you, I really appreciate your help! If I understand this correctly, msHold is the time since the sensor first went LOW. I think millis()-msHold will turn a negative value, as mills() is the current current time and msHold its value is bigger. Should they be switched? Or am I wrong?
You set msHold equal to millis(), then wait at least 50 miliseconds, and substract msHold from the new millis();
So basically millis_1() + 50ms = millis_2(), assuming there are no other delays inbetween. Since you’re waiting, the second millis() will always be greater than the one prior to it. In this case, a minimum of 50ms since that’s the time spent in the delay.
Plus the fact that uint32_t stands for unsigned long and unsigned math will never yield a negative number.
Nope, it’s the time when the sensor went LOW
and hence
how would a previous time (msHold) ever be greater than the current time (millis()) unless Particle had an on-board time machine (consistent unsigned math ensures the continuity of the time vector even on rollover of the variables used to represent the timestamps)