I have a small anemometer with a reed switch from an old weather station.
I would like to operate a small relay when the reed switch from the anemometer has pulse 15 to or 36 pulses per second and when the desire number has reached the arduino will activate an output (i.e small relay). I am completely new to this and would like to see an example (of a code) of this.
Interrupts are commonly used in this scenario, though at this speed you might well get away with polling, depending on what else your arduino has to do. Take a look at attachInterrupt in the reference section.
wildbill:
Interrupts are commonly used in this scenario, though at this speed you might well get away with polling
Reed switches bounce!
... like a bad check!!
Good point. I don't like pulsin for this though if there is other work going on that can't wait; hardware debounce? Or come to think of it, just debounce it in software in the interrupt routine - store millis when it fires and ignore it for a while after.
It's all a matter of priorities - and some other things.
Interrupts are appropriate if the code dives into some complex processing from which it is difficult to separate and which will take a comparable time to the interval between events (which events include both make and break of the reed switch contacts here).
In this case, 36 pulses per second (sounds an awful lot for an anemometer) implies events about fourteen milliseconds long. If we were to use a one millisecond heartbeat (interrupt) and a debounce count of four, this should handle the events quite nicely by polling. As long as no other process (such as software serial communications code) will delay as long as half a millisecond.
Putting debounce code into interrupts is somewhat more tricky and in particular, wastes time and introduces random delays when contact bounce results in barrages of interrupts. It could easily throw out the software serial timing for example.