Interrupt Help

Hi all,

Looking for some assistance on a sketch to record wind speed from an annemometer. The annemometer switches to ground each revolution of the cups.

I am trying to use an interrupt function because the initial code I had seems to be missing pulses (the switch only goes to ground momentarilty on each revolution)

This is what I have so far.

/*
Wind speed read from switch to ground on Pin 2
 */

//Wind Speed Constants

int LastState = 0;
int SensorTime = 0;                   // Time at wind speed falling edge
int PrevSensorTime = 0;               // Previous time at wind speed falling edge
long period = 0;                      // Period of windspeed square wave in milliseconds
float AppWindSpeed = 0;               // Windspeed in knots

#define SpeedSensor 2      //Switch to ground

void setup()
{
  pinMode(SpeedSensor, INPUT_PULLUP);
  Serial.begin(9600);
  attachInterrupt(SpeedSensor, WindSpeed, FALLING);
}

void loop()
{
  //OUTPUT DATA TO SCREEN

  Serial.print("       Apparent Wind Speed = ");
  Serial.print(AppWindSpeed);
  Serial.println(" Knots");

  delay (1);          // Delay 1 millisecond for stability
}

void WindSpeed()
{
  SensorTime = millis();                               //Read time of change
  period = SensorTime - PrevSensorTime;                //Calculate time since last change
  PrevSensorTime = SensorTime;                         //Set current sensor time as previous time for next reading
  AppWindSpeed = 1955.20 / period;        //convert from period to frequency and then to knots
}

It compiles OK but i get silly windspeed readings like around 100knots when i would expect 5 or 10.

attachInterrupt(interrupt, function, mode)

from the reference.

You are setting the interrupt to 2 (speedSensor). If your speed sensor is connected to pin 2 the interrupt would be 0. There are two external interrupts pin 2 is interrupt 0 and pin 3 is interrupt 1 on a Uno.

What is the nature of the switch in the anemometer? If it's anything mechanical, e.g. a reed switch, there will be switch bounce to deal with. That may be what is causing it to look like it's closing more often than reasonable. I'd estimate the minimum expected period based on the maximum wind speed to be measured, then use that for some debouncing code.

Thanks for the feedback.

I am using an Arduino Micro, as I read it the interrupt pins are 0, 1, 2 and 3 but the Arduino site does not refer to what the interrupt numbers are for these pins so I have assumed the interrupt no is the pin no is that correct???

The sensor has a magnet rotating against a sensor so i assume it is a Hall effect sensor, would these be susceptible to bounce like a reed switch?

You didn't say which and I only know the Uno.

Is there anyone else who has experience with a micro? or interrupts in general who can see what i’m missing?

I don't have a Micro but looking at the 32U4 datasheet and the Micro schematic, there are five external interrupts, 0-3 and 6, assigned as follows:

INT0 - D3/SCL
INT1 - D2/SDA
INT2 - D0/RX
INT3 - D1/TX
INT6 - D7

Regarding the Hall effect sensor, could be, but I would prefer not to assume. Is there a datasheet or other reference material for the anemometer?

Your main loop is flooding the serial port set at low speed. I'm not sure if this effects your timing, but it should be changed.

Use a higher baud rate and longer delay.
Keep a running average of results and report every nth cycle.
Maintain a running count of the interrupt pulses and report pulses/sec as a debugging tool. If the pulse count is too high than you may have a bounce problem. It is always better to measure than to guess.