I am using a Wind Vaned Anemometer to find the wind speed and direction. The system uses two sets of reed switches (one used for speed and one used for direction).

I am trying to print out the data to the serial monitor to confirm the data I am reading in, but am receiving nothing.

Can someone offer my some advice? I checked my syntax and I'm not sure if it is the way that it is being written or if it has to do with my timing.

When I hook up my system to the oscilloscope this is the reading that I get.

What I'm trying to do is calculate the time difference between the yellow pulses to figure out my RPM. And use the Time difference between the yellow and blue pulses to calculate phase shift to give me direction.

voidloop(){///////////////////////////////////////////////////////////////////////////////////////////// It takes about 100 microseconds to read an analog input //// Max reading rate is 10,000 times a second ///////////////////////////////////////////////////////////////////////////////////////////

I am trying to print out the data to the serial monitor to confirm the data I am reading in, but am receiving nothing.

You are only printing a value when count is greater than 0. The count variable only gets incremented in the interrupt function. That you get nothing in the serial monitor implies that the interrupt handler is not being called, which implies that the Arduino is not receiving any signal on digital pin 2. What is connected to that pin?

I took my printing out of the IF statement for now, but I may want to put it back there once I get the reading for the interrupt correct.

Quote

which implies that the Arduino is not receiving any signal on digital pin 2. What is connected to that pin?

You are right, I didn't connect anything to Digital Pin 2. I misread the how the interrupt function worked. I put my two inputs into Analog 0 and Analog 1.

I wanted to be able to tell when my analog signal rises so I can find the difference in time from when the signals rise relative to one another, and use that time to calculate speed and direction using RPM's and Phase Shift.

Since my signals are close to squares, could I send them through the digital pins (2,3). How would I handle the interrupt if now I have two different signals that I will need to compare?

Quote

As I see the pulses in the scope I would go for a digital read as it seems a nice square wave.

What is the voltage measured? You might need to boost it to get a decent level.

Input voltage is being regulated to 5 V, from a DC power Supply that is simulating a solar panel voltage for now.

I think you might want to look at some of the tachometer examples on this board, like Counting PulseIn.

I know a bit about anemometry and wind resource assessment. Are you sure your wind vane uses a reed switch? Don't think I've ever seen one of those, unless it uses several switches to indicate wind sectors.

Typical wind vanes are either:

linear potentiometer -- linear output from 2° to 358°. Really nice ones are linear from -90° to 450° to help with direction trending.

Gray code -- has several ttl outputs which generate a bitmap from which the wind sector can be deduced.

Reed switch anemometers are probably the easiest to read, but are quite low resolution. They work well with long cables. Other types are linear (voltage or current loop), low voltage AC (with frequency proportional to wind speed) and square wave (usually generated by a photochopper).

Since most wind data loggers (even the really expensive ones used for utility-grade work) are many times less powerful than an Arduino, I've been meaning to develop a decent logger sketch.

Since my signals are close to squares, could I send them through the digital pins (2,3). How would I handle the interrupt if now I have two different signals that I will need to compare?

The answer to the first question is yes. But, I wouldn't. I'd use 4 and 5. Or use 2 and 3, but attach interrupt handlers to the two pins (they are the external interrupt pins). Each ISR would fire on the RISING edge or the FALLING edge. In each ISR, record when the interrupt occured. In loop, look at the relative times.

Are you sure your wind vane uses a reed switch? Don't think I've ever seen one of those, unless it uses several switches to indicate wind sectors.

Yes, it does implement reed switches. Two in fact (one associated with the three cup system and one for the wind vein). You can find speed relative to one switch and seeing how fast the switch turns on and off. To find direction you have to measure the shift between the two pulses.

Quote

The answer to the first question is yes. But, I wouldn't. I'd use 4 and 5. Or use 2 and 3, but attach interrupt handlers to the two pins (they are the external interrupt pins). Each ISR would fire on the RISING edge or the FALLING edge. In each ISR, record when the interrupt occured. In loop, look at the relative times.

I guess I am just confused on how the data will be stored. If I attach my pins to 2 and 3 using and use 2 attathinterrupts instead of two

I would only want my SpeedDuration to trigger only when pin 2 rises. Does that mean I have to create a new subroutine outside of loop to account for this? Will this data be erased when pin 3 triggers with an interrupt? Can I compare the Time Difference in the loop if they are being handled in the interrupt subroutine?

You would not want to test for equality, anyway. You want to do something when count > 0, not any specific value greater than 0.

Are count and directionCount volatile?

In order to get good timing data, the time the interrupt occured needs to be recorded in the ISRs. Finally, while didling with the time and count values, you really should disable interrupts, so you are not interrupted during the computations. Re-enable them when done.

And last, but not necessarily least, GET RID OF THE delay()s. You probably want to only calculate and output speed and direction once every few seconds, but don't use delay to make that happen.

For some reason my direction is always 69 +/- 1 no matter the direction the wind vein points.

I'm calculating speed and direction value using

Quote

Speedvalue = (1492*3600000/SpeedDuration); ---where 1492 is some arbitrary distance and 3600000 is to convert to hours from milisecondsDirectionvalue = 360*TimeDifference/DirectionTimePeriod;---I decided to divide by the period of my direction signal rather than multiply by the frequency, because I would end up getting zero because of the scaling.

I'm checking the math using the tables and they match up to be 69-ish. I guess this leads to the idea that I'm not reading the rising pulses correctly. Any thoughts?

// Variables for timeunsigned long SpeedDuration;unsigned long DirectionDuration;unsigned long SpeedTimePrevious;unsigned long DirectionTimePrevious;unsigned long DirectionTimePrevious1;unsigned long DirectionTimePeriod;unsigned long TimeDifference;unsigned long Frequency;

void loop(){///////////////////////////////////////////////////////////////////////////////////////////// It takes about 100 microseconds to read an analog input //// Max reading rate is 10,000 times a second ///////////////////////////////////////////////e////////////////////////////////////////////

Speedvalue = (1492*3600000/SpeedDuration); 1492 and 3600000 are treated as ints. 1492 is an int, but 3600000 is not. You should have a L or UL at the end, to tell the compiler to treat the value as a long or unsigned long.

Are you using the digital pins, so digitalRead is appropriate, or the analog pins?If you are using digital pins, 0 and 1 are not good choices while doing serial I/O.If you are using analog pins, you should be using the analogRead function.

1492 and 3600000 are treated as ints. 1492 is an int, but 3600000 is not. You should have a L or UL at the end, to tell the compiler to treat the value as a long or unsigned long.

Are you implying that I store the 1492*3600000 as 5371200000 in another UL variable and hash out my math in explicit UL variables?

Quote

Are you using the digital pins, so digitalRead is appropriate, or the analog pins?If you are using digital pins, 0 and 1 are not good choices while doing serial I/O.If you are using analog pins, you should be using the analogRead function.

Sorry those were poor name choices for variables. Since the input were square waves, I decided to have them be digital inputs. Originally they were supposed to be analog inputs at pins 0 and 1, but I changed it to digital pin 2 and 3 and forgot to change my comments/names.

On another note, I am trying out a debounce circuit on my input to help clean out my input. That could be a source of error when reading in my data.

I cleaned up the code a little bit to get rid of some redundancies in the old code.

I am noticing the following:

1. My Time Difference is always the same no matter what angle I move my anemometer.

It makes me wonder if I am even making it to that part of the code to update my software or if the program is reading my square waves correctly.

2. I have my sensor set up to a fan that gives my cups 154 rotations per minute, which give my code 308 interrupts per minute.

I read that the millis() function acts as an interrupt as well. I referr to millis() at lease 4 x in my loop function.

That mixed in with the number of interrupts I am getting, will I be able to make it though my loop function to update my data correctly?

Another question: How much voltage does it take to trigger a rising interrupt? I have very minimal noise on my signal when it is on/off. I'm not sure if this is a problem as my timing to measure my distance between rising edges on 1 signal is off from the oscilloscope readings.

I am wondering whether your use of Serial.print is impacting your times. I assume that the UART will buffer the text to some extent, but since you're sending around 30 characters each iteration of the loop, you may be getting delayed by that.

Also, you might get a bit more accuracy on the speed if you collect a few more interrupts (10?) before calculating the time difference