HOME  - -  Many other Arduino "How Tos", etc, at my older site

Arduino: "Watching" a switch or input.

The easy way to do it. The bad way to do it.

(filename: aht2door_watch.htm)

I once taught in a classroom next to a door leading to the outside. Pupils were always leaving it open. I wanted the school to put an automatic closer on it, so my classroom wouldn't be so COLD in winter. This was 1980. I made an electronic "thing" to record how often, how long, when the door was left open.

It would have been easier to do today!

Here's how you MIGHT do it... but what's here is a POOR way to do it... but it will lead to another essay that builds on this poor start.

First I'd attach a micro-switch to the door. There are fancier solutions, of course. But all you need is the equivalent of a "doorbell-type" switch. (I.e. a "momentary" switch. Press it, and it connects two wires. Release it, and they are disconnected. (Or vice versa.))

It could be a switch that worked either way. For the ultimate solution I'm working towards here, a switch that is OPEN when the door is closed/ CLOSED (i.e. wires connected) when the door is open suits me best.

That switch will be a part of my ultimate solution.

Level Zero "Solution"

Connect one side of the switch to the ground rail (zero volts) of an Arduino.

Connect the other side to an input, one that has a pull-up resistor on it.

Tell the Arduino to go round and round a loop, checking the state of the input, and printing a * on the serial monitor when the door is open, and a . when the door is shut.

A pattern of ****** s and ...... s will build up on the serial monitor, and from, that, you can see when the door was open, when it was shut.

(You can of course "watch" anything that has only two states, and can be "connected" to a momentary switch, or equivalent.)

But.

That really isn't going to be very satisfactory.

You'll get HUNDREDS of **** s and .... s every minute!

So. Stick a delay(5000); on the loop! It will cause the program to do nothing for 5 seconds each time execution reaches that statement.

Fewer *** s and ... s.

But. If the door is opened and then shut again during one of the 5 second "time out"s, the event won't be shown on the screen.

AND, unless you get clever, it is hard to have the Arduino doing other things while this is going on.

Bad solution.

It is called "polling", as in taking a "poll". And there are times when that is a good solution.

Interrupts!

Part of the better solution to this design goal is the INTERRUPT. They are a little arcane and scary. But Worth It.

The way we'll use an interrupt here has two benefits...

Previously we looked at the LEVEL of the input. Is it high? Low? Now. We had to go back, and LOOK, again and again.

Give the job to an interrupt, and the system "watches" for us, all the time. Once it has been "turned on" (by stuff early in our code) it doesn't depend on things in our code, it doesn't matter where in our code the Arduino has got to, if the door's state CHANGES the "interrupt service routine" NOTICES!

The way I'll use them, interrupts are EDGE sensitive, not LEVEL sensitive. (I have a separate page for you about Level vs Edge sensing).

More specifically, "interrupts"...

For people who are new to interrupts, I want to say a bit more.

In a pointless, but I hope easy to follow, example, you could have something like this...

Superficially, on an Arduino with 6 LEDs attached, you could have a little program that used 4 of them to "count".

By that I mean the LEDs would light up to show 0, 1, 2, 3.... 13, 14, 15 as binary numbers, pausing for 1 second to display each of them. When the count got to 15, it would go back to zero, and repeat that over and over.

This is a relatively trivial bit of coding. If you don't agree with that statement, by all means dream of monitoring doors, using interrupts, doing all the cool things that you can to with Arduinos and such... but maybe you'll progress more quickly if you "walk before you try to run"?

When you want more detail on Using Interrupts, Interrupt Service Routines, ISRs (etc!) see https://wywtk.com/ardu/aht3inter_count.htm

Moving on...

If, in addition to what's needed for the above, you'll added just a few extra lines of code, you will have "turned on" the Arduino's ability to react to edges on one of the input pins. And set up an interrupt service routine (ISR).

In the way that is common to so much of what we do with computers, in the way that is Very Cool, the ISR can do ALMOST ANYTHING!... if you are a clever enough programmer.

For my silly "Here's an interrupt in action", I would have the ISR keep track of how many times a button has been pressed, and display 0, 1, 2, 3, 0, 1, 2, 3, 0, 1... etc on the OTHER two LEDs, to show that the interrupt had been processed.

Yes! You CAN do all of that without an ISR. But it is so much EASIER with an ISR!

Here's how it works...

Your basic count 0-15 over and over on the first four LEDs program would run and run and run, as if the rest of this wasn't happening.

But! When the button was pressed, that would "hi-jack" the process. It would INTERRUPT the process. (Hence the name!) It would do what was called for in the ISR... and then return control to the main program.

A bit like using a subroutine. But "triggered" by the external event, the "edge" on the input pin.

Does that make total sense?

I doubt it! It's one of those things you have to use a bit to get the hang of it. But I hope I've helped you know more than you did ten minutes ago.

How does this help us "watch" a door?

Okay. Fasten seat belt. For the rest of this page, I'm going to sketch something I'm doing in the real world. It isn't "an example for a tutorial. I'm building this, "to use in anger".

I'll sketch my approach to the problem here. And then do a separate page on the actual doing of it.

What it does

I'm working towards a program that creates a Arduserver... my name for an Arduino set up to serve html webpages to the internet. See my page about Arduservers if fascinated, but you don't need to for our purposes here.

In the usual way, there is a "setup()" block and a "loop()" block.

Once the program is running, it connects to the local WiFi, and via that, to the router that connects... through a firewall... my LAN to the wider internet.

If you give it the right URL, it serves up a page of HTML.

The "door monitor" code I am adding will be "behind" a new line in the HTML saying how long the door has been open. It will be a running total, like the "miles driven" indicator in your car.

I have programs which can automatically connect to my Arduservers, and by comparing how long it had been open an hour ago with how long it has been open now, I can see how long the door has been open in the past hour.

There are also temperature and humidity sensors in the mix. Quite a lot going on, as you can see!

So...

I will add some code that tells the Arduserver to respond to negative edges on an input. It will count them.

Even though the main loop of the Arduserver is only passed though once in a while, the ISR will always be "watching" for a negative edged on the input from the switch on the door.

But it gets better!

If it was JUST a switch on the door, there'd just be one edge when the door opened. After that, no more edges of the same sort until the door was closed and opened again. We could easily count how OFTEN the door opened. But not for HOW LONG.

So instead of the wire from the switch... CLOSED when the door is OPEN... simply going to one end of the "battery" (or whatever source of power is being used), the wire from the switch will go to an OSCILLATOR. If you were to attach it to a voltmeter, you'd see High/ Low/ High/ Low/ High....

Oscillators are not exotic or expensive. You can buy them for about $1 and set them up easily.

But why bother? Why not program one of the pins of the Arduino to go High/ Low/ High/ Low...?

That's the idea in all of this that gives me the biggest buzz! And that's what I've used in the code in the page I will write with the details...

Another time!

Related matter

I don't know if it is obvious, but if you want to use the above, you are probably going to have at least two things going on in your loop() subroutine.

There will be a "big loop" which you go around OFTEN, but when you go through it, you will USUALLY skip over a bunch of things. Those things might only be done once in 60 passes throught the loop.

Meanwhile, you might want an LED to blink, with the LED on for 180ms and off for 720ms.

It ways to do stuff like that sound interesting... see...my "aht3flash_LED_with_millis.htm"

Ending abruptly so I can get on with that more fun activity!



A few words from the sponsors...

Please get in touch if you discover flaws in this page. Please cite the page's URL. (http://wywtk.com/ardu/aht2door_watch.htm).

If you found this of interest, please mention in forums, give it a Facebook "like", Google "Plus", or whatever. If you want more of this stuff, help!? There's not much point in me writing these things, if no one feels they are of any use.



index sitemap
What's New at the Site Advanced search
Search tool (free) provided by FreeFind... whom I've used since 2002. Happy with it, obviously!

Unlike the clever Google search engine, this one merely looks for the words you type, so....
*    Spell them properly.
*    Don't bother with "How do I get rich?" That will merely return pages with "how", "do", "I"....

Please also note that I have three other sites, and that this search will not include them. They have their own search buttons.

My SheepdogSoftware.co.uk site, where you'll find my main homepage. It has links for other areas, such as education, programming, investing.

My SheepdogGuides.com site.

My site at Arunet.




How to email or write this page's editor, Tom Boyd. Please cite page's URL (http://wywtk.com/ardu/aht2door_watch.htm) if you write.


Valid HTML Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.

AND passes... Valid CSS!


Why does this page cause a script to run? Because of the Google panels, and the code for the search button. Also, I have my web-traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try one, check out their site. Why do I mention the script? Be sure you know all you need to about spyware.

....... P a g e . . . E n d s .....