Can someone help me find a code for an anemometer mine it one that only has two wires and all i can find are ones with more.
Can you post a link to the anemometer ?
I guess if it has only two wires, then you get one pulse per revolution, possibly from a reed switch. In that case, the code is simple. Use millis() to measure the elapsed time between pulses, and calculate the rotational speed.
Two wires may mean you have a motor generator type anemometer or a reed switch type. Some reed switch type give one pulse per revolution and some give 2 pulses. My machine has a bar magnet rotating over a reed switch, so it gives two pulses per rev. If yours is a motor/generator, you will need to program to measure the voltage output and relate it to wind speed.
Paul
It is a reed switch type with one pulse per rotation. I can't give a link to it because it is from my dad's old weather station. I can give a pdf to the weather station though. what you say with the counting millis() then calculating the rotation speed. How would you do that.
read millis() every time the front ( or back ) edge of the pulse is detected
subtract previously-read value of millis() to determine rotation time.
wind speed will be inversely proportional to both rotation time (which will vary) and vane length (which is fixed but relevant )
Can you give me a link to a tutorial to help.
Can you give me a link to a tutorial to help.
To using millis()? The blink without delay example shows how.
To doing basic math? No. You are on your own for that.
This comes quite close to what you want to do:
http://playground.arduino.cc/Main/ReadingRPM
You are essentially building a sort of tachometer. Once you have the Revolutions per Minute, you have to calculate the linear velocity at the tips of the arms to get the wind speed.
I found a code that i am useing from
http://cactus.io/hookups/weather/anemometer/davis/hookup-arduino-to-davis-anemometer-wind-speed
#include <math.h>
#define WindSensorPin (2) // The pin location of the anemometer sensor
volatile unsigned long Rotations; // cup rotation counter used in interrupt routine
volatile unsigned long ContactBounceTime; // Timer to avoid contact bounce in interrupt routine
float WindSpeed; // speed miles per hour
void setup() {
Serial.begin(9600);
pinMode(WindSensorPin, INPUT);
attachInterrupt(digitalPinToInterrupt(WindSensorPin), rotation, FALLING);
Serial.println("Davis Wind Speed Test");
Serial.println("Rotations\tMPH");
}
void loop() {
Rotations = 0; // Set Rotations count to 0 ready for calculations
sei(); // Enables interrupts
delay (3000); // Wait 3 seconds to average
cli(); // Disable interrupts
// convert to mp/h using the formula V=P(2.25/T)
// V = P(2.25/3) = P * 0.75
WindSpeed = Rotations * 0.75;
Serial.print(Rotations); Serial.print("\t\t");
Serial.println(WindSpeed);
}
// This is the function that the interrupt calls to increment the rotation count
void rotation () {
if ((millis() - ContactBounceTime) > 15 ) { // debounce the switch contact.
Rotations++;
ContactBounceTime = millis();
}
}
but i am getting an error and i don't know why.
sketch_dec19a.ino: In function 'void setup()':
sketch_dec19a:14: error: 'NOT_AN_INTERRUPT' was not declared in this scope
You're using pin 2 ?
I'd change this:
#define WindSensorPin (2) // The pin location of the anemometer sensor
to:
#define WindSensorPin 2 // The pin location of the anemometer sensor
You are using a Uno ?
That didn't work and I am using a nano.
I can't see the problem. It must be having difficulty interpreting digitalPinToInterrupt().
A Uno and Nano are both ATMEGA328 based so pin 2 is an interrupt pin.
Does this work:
attachInterrupt( 0 , rotation, FALLING); // 0 = pin2
best to not try to use interrupts if you're inexperienced
try the below for your main():
the first reading will be somewhat bogus because prev_ts will initially be zero
static unsigned long prev_ts;
static bool prev_pin_state = true;
bool curr_pin_state = digitalRead(2);
if ((curr_pin_state == false) && (prev_pin_state == true))
{
Serial.println("high-to-low edge detected")
Serial.print(millis() - prev_ts);
Serial.println(" ms since previous edge");
}
prev_pin_state = curr_pin_state;
delay (10);
best to not try to use interrupts if you're inexperienced
Everyone has to learn at some time. While interrupts introduce a whole host of issues, and the code that OP posted was an example of the worst possible way to use interrupts, your alternative has problems, too. Your code must loop often enough to guarantee never missing a pulse on the anemometer pin. While that is possible, printing novels to the serial port is not going to let that happen.
And, of course, burying your head in the sand for 10 milliseconds on EVERY iteration through loop() is just guaranteeing that you WILL miss pulses.
OP: Ditch that crappy code and start over. The interrupt handler is fine, except for the crappy indenting. But, enabling interrupts, then delaying for 3 seconds, then disabling ALL interrupts and calling functions, like Serial.print(), that require interrupts being enabled, is just NOT the way to use interrupts.
You need to keep interrupts enabled at all times. Periodically, you need to see how many interrupts have happened, reset the counter, compute the RPM and wind speed, and print the values. The blink without delay example shows how to periodically do things WITHOUT using the dreaded delay() function.
PaulS:
. . . The interrupt handler is fine, except for the crappy indenting . . .
OK. But can you see the reason for the compiler error he reported ?
OK. But can you see the reason for the compiler error he reported ?
Well, the version of the IDE is important. The digitalPinToInterrupt() macro is a relatively recent addition.
OP really needs to enable verbose mode when compiling, and show us ALL of the output, which will include the version of the IDE, the board being compiled for, etc.
When I compile the code OP posted, for a Nano, using 1.6.9, I get:
Sketch uses 4,666 bytes (15%) of program storage space. Maximum is 30,720 bytes.
Global variables use 254 bytes (12%) of dynamic memory, leaving 1,794 bytes for local variables. Maximum is 2,048 bytes.
wg0z:
best to not try to use interrupts if you're inexperienced
try the below for your main():
the first reading will be somewhat bogus because prev_ts will initially be zero
where is main():
thanks guy's i just had to update my IDE