Hello everyone!I've got a problem, and i would appreciate if you could help me for one more time.
My problem is that i've got a wind sensor, and i want to count pulses per second in order to calculate with a formula the speed and all the other details witch has to do with wind.
First of all.As i discuss with my friend, we don't care when the interrups is triggering. How can i read pulses per second from ISR ?Is there an example whitch someone has done it before??
But what i think it is also important to think about the measurement frequency... Depends of your sensor (you do not say what type it is) may be 1Hz is so high... in my case, to have a mean value, i have a frequency of 1 data per minute... (although i recognize that it could be so low).
I do not have here my code. I will post here later. May be it could help you in some way.
Cheers,
Ok, here is the code what i wrote for read data from my home-made anemometer based on an IR-sensor. It count the number of pulses in a period of 10 seconds, and calculate the wind velocity taking into account the anemometer characteristics... It use interrupts in the digital pin number 3 (INT1). May be it could be useful to you. Enjoy it!
/* QRD1114 IR Sensor on an cups anemometer
* Authors: M.A. de Pablo & C. de Pablo S., 2010
* version: 1.1 20110714
* Wind speed information: http://en.wikipedia.org/wiki/Beaufort_scale
*/
// Pin definitions
//# define IR 3 // Receive the data from sensor
# define led 13 // Light a led
// Constants definitions
const float pi = 3.14159265; // pi number
int period = 10000; // Measurement period (miliseconds)
int delaytime = 10000; // Time between samples (miliseconds)
int radio = 65; // Radio from vertical anemometer axis to a cup center (mm)
char* winds[] = {"Calm", "Light air", "Light breeze", "Gentle breeze", "Moderate breeze", "Fresh breeze", "Strong breeze", "Moderate gale", "Fresh gale", "Strong gale", "Storm", "Violent storm", "Hurricane"};
// Variable definitions
unsigned int Sample = 0; // Sample number
unsigned int counter = 0; // B/W counter for sensor
unsigned int RPM = 0; // Revolutions per minute
float speedwind = 0; // Wind speed (m/s)
unsigned short windforce = 0; // Beaufort Wind Force Scale
void setup()
{
// Set the pins
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
// sets the serial port to 115200
Serial.begin(115200);
// Splash screen
Serial.println("ANEMOMETER");
Serial.println("**********");
Serial.println("Based on QRD1114 IR sensor");
Serial.print("Sampling period: ");
Serial.print(period/1000);
Serial.print(" seconds every ");
Serial.print(delaytime/1000);
Serial.println(" seconds.");
Serial.println("** You could modify those values on code **");
Serial.println();
}
void loop()
{
Sample++;
Serial.print(Sample);
Serial.print(": Start measurement...");
windvelocity();
Serial.println(" finished.");
Serial.print("Counter: ");
Serial.print(counter);
Serial.print("; RPM: ");
RPMcalc();
Serial.print(RPM);
Serial.print("; Wind speed: ");
WindSpeed();
Serial.print(speedwind);
Serial.print(" [m/s] (+/- 0.07 m/s); Wind force (Beaufort Scale): ");
Serial.print(windforce);
Serial.print(" - ");
Serial.println(winds[windforce]);
Serial.println();
delay(10000);
}
// Measure wind speed
void windvelocity(){
speedwind = 0;
counter = 0;
digitalWrite(led, HIGH);
attachInterrupt(1, addcount, CHANGE);
unsigned long millis();
long startTime = millis();
while(millis() < startTime + period) {
}
digitalWrite(led, LOW);
detachInterrupt(1);
}
void RPMcalc(){
RPM=((counter/2)*60)/(period/1000); // Calculate revolutions per minute (RPM)
}
void WindSpeed(){
speedwind = ((2 * pi * radio * RPM)/60) / 1000; // Calculate wind speed on m/s
if (speedwind <= 0.3){ // Calculate Wind force depending of wind velocity
windforce = 0; // Calm
}
else if (speedwind <= 1.5){
windforce = 1; // Light air
}
else if (speedwind <= 3.4){
windforce = 2; // Light breeze
}
else if (speedwind <= 5.4){
windforce = 3; // Gentle breeze
}
else if (speedwind <= 7.9){
windforce = 4; // Moderate breeze
}
else if (speedwind <= 10.7){
windforce = 5; // Fresh breeze
}
else if (speedwind <= 13.8){
windforce = 6; // Strong breeze
}
else if (speedwind <= 17.1){
windforce = 7; // High wind, Moderate gale, Near gale
}
else if (speedwind <= 20.7){
windforce = 8; // Gale, Fresh gale
}
else if (speedwind <= 24.4){
windforce = 9; // Strong gale
}
else if (speedwind <= 28.4){
windforce = 10; // Storm, Whole gale
}
else if (speedwind <= 32.6){
windforce = 11; // Violent storm
}
else {
windforce = 12; // Hurricane (from this point, apply the Fujita Scale)
}
}
void addcount(){
counter++;
}