Hi all,
I'm just trying to work with this sketch to control wind speed from an anemometer. Everything works, but when I want to knot the gust (or the highest wind speed of the measuring period) I received an error:'UINT_MAX' was not declared in this scope. The error comes from the last line of the code
Anybody could help me to understand and fix the problem?
Thank's
Eduard
#include <math.h>
//Variable to setup the machine ON
unsigned long vtime; //Declara un variable del mismo tipo que la que devuelve micros()
// Each time we loop through the main loop, we check to see if it's time to capture the sensor readings
unsigned int sensorCapturePeriod = 100;
unsigned int vtimeNextSensorReading;
void setup() {
initializeAnemometer();
// Schedule the next sensor reading and publish events
vtimeNextSensorReading = millis() + sensorCapturePeriod;
}
void loop() {
vtime = millis();
// Capture any sensors that need to be polled
// The wind speed sensors use interrupts, and so data is collected "in the background"
if (vtimeNextSensorReading <= millis()) {
// Schedule the next sensor reading
vtimeNextSensorReading = millis() + sensorCapturePeriod;
}
// Publish the data collected
float gustKPH;
float windKPH = (getAndResetAnemometerkph(&gustKPH));
float knots = windKPH * 0.5399;
delay(1000);
//print data
String SPKwind = String(windKPH);
String SPKgust = String(gustKPH);
String SPKknots = String(knots);
Serial.print("WindKPH");
Serial.println(SPKwind);
delay(10);
}
//===========================================================================
// Wind Speed (Anemometer)
//===========================================================================
int AnemometerPin = 5;
float AnemometerScalekph = 1.492; // Windspeed if we got a pulse every second (i.e. 1Hz)
volatile unsigned int AnemoneterPeriodTotal = 0;
volatile unsigned int AnemoneterPeriodReadingCount = 0;
volatile unsigned int GustPeriod = UINT_MAX;
unsigned int lastAnemoneterEvent = 0;
void initializeAnemometer() {
pinMode(AnemometerPin, INPUT_PULLUP);
AnemoneterPeriodTotal = 0;
AnemoneterPeriodReadingCount = 0;
GustPeriod = UINT_MAX; // The shortest period (and therefore fastest gust) observed
lastAnemoneterEvent = 0;
attachInterrupt(AnemometerPin, handleAnemometerEvent, FALLING);
return;
}
void handleAnemometerEvent() {
// Activated by the magnet in the anemometer (2 ticks per rotation), attached to input D3
unsigned int vtimeAnemometerEvent = millis(); // grab current time
//If there's never been an event before (first time through), then just capture it
if (lastAnemoneterEvent != 0) {
// Calculate time since last event
unsigned int period = vtimeAnemometerEvent - lastAnemoneterEvent;
// ignore switch-bounce glitches less than 10mS after initial edge (which implies a max windspeed of 149kph)
if (period < 10) {
return;
}
// if(period < GustPeriod) {
// // If the period is the shortest (and therefore fastest windspeed) seen, capture it
// GustPeriod = period;
// }
AnemoneterPeriodTotal += period;
AnemoneterPeriodReadingCount++;
}
lastAnemoneterEvent = vtimeAnemometerEvent; // set up for next event
}
float getAndResetAnemometerkph(float * gustKPH)
{
if (AnemoneterPeriodReadingCount == 0)
{
*gustKPH = 0.0;
return 0;
}
float result = AnemometerScalekph * 1000.0 * float(AnemoneterPeriodReadingCount) / float(AnemoneterPeriodTotal);
AnemoneterPeriodTotal = 0;
AnemoneterPeriodReadingCount = 0;
*gustKPH = AnemometerScalekph * 1000.0 / float(GustPeriod);
GustPeriod = UINT_MAX;
return result;
}