-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindsensor.h
70 lines (61 loc) · 1.53 KB
/
windsensor.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* read Windsensor input and return the wind speed in km/h
PCE Instruments PCE-WS P
Author: Christian Langrock
Date: 2022-August-06
*/
char* strMQTTTopic_wind {mqtt_topic_wind};
volatile unsigned long CountReed = 0;
float wind {0.0};
float last_wind {0.0};
float wind_out {0.0};
//char charBuffer[32];
volatile unsigned long last_micros;
long debouncing_time = 0.1; //in millis
unsigned long cycleTime;
unsigned long cycleTime_old;
// define the interrupt for the wind sensor
void IRAM_ATTR Interrupt()
{
if ((long)(micros() - last_micros) >= debouncing_time * 1000) {
CountReed++;
last_micros = micros();
}
}
void initWindsensor() {
pinMode(input_pin_wind, INPUT_PULLUP);
attachInterrupt(input_pin_wind, Interrupt, RISING);
cycleTime_old = millis();
}
float windsensor() {
detachInterrupt(input_pin_wind);
cycleTime = millis();
unsigned long runtime = cycleTime - cycleTime_old;
if (runtime >= 900 && runtime <= 1100) {
if (CountReed == 0) {
wind = 0.0;
wind_out = 0.0;
}
else {
wind = (CountReed * 0.8) + 3; // PCE Instruments PCE-WS P
}
wind_out = wind;
last_wind = wind;
}
else {
wind_out = last_wind;
}
CountReed = 0;
cycleTime_old = cycleTime;
if (debugOutput) {
Serial.print("Task Wind running time: ");
Serial.print(runtime);
Serial.println ("ms");
}
if (debugMeasure) {
Serial.print("Wind: ");
Serial.print(wind_out);
Serial.println(" km/h");
}
attachInterrupt(input_pin_wind, Interrupt, RISING);
return wind_out;
}