GarageSensor() is being used here as a timer call back routine. I’m guessing that Serial commands do not work in this context. Try defining the variables you want to print as volatile and printing them from the loop().
Bjerknez:
I have tried that, but it does not work. It works only when i use the simple, last code.
OK.
Post the code where you attempted to print distance from the loop() as suggested, then someone can suggest further modifications. Hint: an integer consists of 4 bytes here so you should make a copy of it before attempting to use it in the loop(). You make the copy of it within a noInterrupts() / interrupts() block to prevent it being half written to from the ISR during the time you are attempting to make the copy.
I don't know Blynk but if, however, it interferes with Serial then this will anyway not work.
I don't know about Blynk, but this doesn't look like a hard problem at all - so I'm not sure what you're stuck on.
Use millis() to grab the current run time in milliseconds when the door opens, then compare that to the current value of millis(). If it's greater than 30601000, that means it's been open for 30 minutes.
I just need help to the code you just explained. I need to see it. I have tested the "blink without delay" example, but i think is it many more ways to play with millis. I just do not know where to start.
So i just need to see the code that you explained, for getting forward.
Bjerknez: I get notice when the door opening and closing, but i want to get an reminder if the door is still open after 30 minutes.
I'm not seeing how that happens. Seems to me that your sensor tests that the door is open or closed, but not openING or closING.
#include <WiFi.h>
BLYNK_READ(V1){
if(distance > 100){
Blynk.virtualWrite(V1, "GARASJEPORT ER ÅPEN!");
}
else{
Blynk.virtualWrite(V1, "GARASJEPORT ER STENGT!");
}
}
}
I'm wondering how it can be that you don't get spammed with messages; does Blynk take care of that?
Because to do the test for 30 minutes as suggested by binaryfissiongames, you will need to compare the sensor reading "now" to sensor reading "previous" in order to capture the instant at which it becomes open. And I don't see that you're doing that already.
(But it's early and I have so far had only 1 1/2 cups of coffee, so maybe I'm missing sth?)
Then unless you can invoke further voodoo with Blynk, you would need to change your code along the lines of (but not identical to) the state change detect tutorial.
Instead of checking for a state change from high to low or low to high, look for a change from <100 to >100, at which point capture millis into a variable.
(Which means you would have a variable called say distancePrevious over and above the distance you already have.)
Bjerknez:
I want a reminder that the door is open if it is, after 30 minutes, but only one time until it opens next time.
Thats exactly what binaryfissiongames said here:
binaryfissiongames:
Use millis() to grab the current run time in milliseconds when the door opens, then compare that to the current
Use the state change from < to > 100 to indicate the door has just opened.
While it's open, the state change will be seeing distancePrevious the same as distance each time thru loop() so won't be triggering a new opening event.
If I were you, I'd write a simple sketch to do nothing but read your distance sensor and show on the monitor that the door had "just opened" or "just closed". Get that working without Blynk and any other distractions getting in the way.
Then once that's working reliably, capture the opening time as suggested earlier, and then print a message to say "been open for 1 minute" or whatever.
Only then, migrate that approach to your real code.