I am a noob regarding this and wanted some advice.
I have got a electric gate which I want to monitor if it's open or closed.
If it is open for longer then 30 seconds it should send a sms to my phone. If it is closed before then it should not send any sms message.
I have got the code to read the switch status and I have played around with the code to send the sms which works, but I am having trouble getting both of the codes to work together.
My second problem is the code for the 30 second delay before sending the sms message and cancelling the sending of the sms message if the gate is closed before 30 seconds. Not sure how to program this.
Maybe some advise could point me in the right direction.
Thanks for your reply. I am using the Arduino Nano for this project. ATmega328P
The code so far is all ok and does not come up with errors if I verify, but to get this 30 second check and delay in there is a huge blank whole for me.
Not sure if this code is even correct or if it can be stripped a bit to make it even less. I might have inserted some unnecessary code.
Basic idea is -
Keep a millis() timer running - start the timeout when the seitch is opened, then continue on your way...
Keep testing the elapsed timie until the required durarion (30s) has elapsed... continue with whatver else needs to be done...
If the switch is still open when the timed duration has passed, you've reachd the time when you need to send a message.
Remember to keep a flag reminder, so the SMS message isn't re-sent over and over again - until the switch is closed, and the send-cycle re-set.
Using millis timers can be really useful - for many reasons...
For example, you might want the SMS to go out after the full 30 seconds, but perhaps a buzzer or light could warn of the open gate from 15 seconds onwards.
Another way to say the same thing as lastchancename is:
with the millis() timer running:
set a variable to 30
Each second, decrement the 30 if the gate is open, reset to 30 if the gate is closed
If the variable reaches 0 send your sms and perhaps a flag that will inhibit another SMS code until the gate has closed.
I don't want to suggest you not use an Arduino, however you might look at a home automation controller. You can do a lot more.
I use a Hubitat and am very happy with the things I can do.
This will help me a lot. Will build this into the code and play around with it. I think this was the answer I was looking for but did not have enough info.