-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSDL_Weather_80422.cpp
390 lines (251 loc) · 7.92 KB
/
SDL_Weather_80422.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
SDL_Weather_80422.cpp - Library for SwitchDoc Labs WeatherRack.
SparkFun Weather Station Meters
Argent Data Systems
Created by SwitchDoc Labs July 27, 2014.
Released into the public domain.
Version 1.1 - updated constants to suppport 3.3V
Version 1.6 - Support for ADS1015 in WeatherPiArduino Board February 7, 2015
*/
#include "Arduino.h"
#include <Time.h>
#include "SDL_Weather_80422.h"
#include "Adafruit_ADS1015.h"
Adafruit_ADS1015 ads1015;
//#define NO_PORTC_PINCHANGES // to indicate that port c will not be used for pin change interrupts
//#define NO_PORTD_PINCHANGES // to indicate that port d will not be used for pin change interrupts
//
//#include "PinChangeInt.h"
// private functions
boolean fuzzyCompare(float compareValue, float value)
{
#define VARYVALUE 0.05
if ( (value > (compareValue * (1.0-VARYVALUE))) && (value < (compareValue *(1.0+VARYVALUE))) )
{
return true;
}
return false;
}
float voltageToDegrees(float value, float defaultWindDirection)
{
// Note: The original documentation for the wind vane says 16 positions. Typically only recieve 8 positions. And 315 degrees was wrong.
// For 5V, use 1.0. For 3.3V use 0.66
#define ADJUST3OR5 0.66
#define PowerVoltage 3.3
if (fuzzyCompare(3.84 * ADJUST3OR5 , value))
return 0.0;
if (fuzzyCompare(1.98 * ADJUST3OR5, value))
return 22.5;
if (fuzzyCompare(2.25 * ADJUST3OR5, value))
return 45;
if (fuzzyCompare(0.41 * ADJUST3OR5, value))
return 67.5;
if (fuzzyCompare(0.45 * ADJUST3OR5, value))
return 90.0;
if (fuzzyCompare(0.32 * ADJUST3OR5, value))
return 112.5;
if (fuzzyCompare(0.90 * ADJUST3OR5, value))
return 135.0;
if (fuzzyCompare(0.62 * ADJUST3OR5, value))
return 157.5;
if (fuzzyCompare(1.40 * ADJUST3OR5, value))
return 180;
if (fuzzyCompare(1.19 * ADJUST3OR5, value))
return 202.5;
if (fuzzyCompare(3.08 * ADJUST3OR5, value))
return 225;
if (fuzzyCompare(2.93 * ADJUST3OR5, value))
return 247.5;
if (fuzzyCompare(4.62 * ADJUST3OR5, value))
return 270.0;
if (fuzzyCompare(4.04 * ADJUST3OR5, value))
return 292.5;
if (fuzzyCompare(4.34 * ADJUST3OR5, value)) // chart in documentation wrong
return 315.0;
if (fuzzyCompare(3.43 * ADJUST3OR5, value))
return 337.5;
//Serial.print(" FAIL WIND DIRECTION");
return defaultWindDirection; // return previous value if not found
}
// Interrupt routines
// for mega, have to use Port B - only Port B works.
/*
Interrupt 4 - Pin 19
Interrupt 5 - Pin 18
*/
unsigned long lastWindTime;
void serviceInterruptAnem()
{
unsigned long currentTime= (unsigned long)(micros()-lastWindTime);
lastWindTime=micros();
if(currentTime>1000) // debounce
{
SDL_Weather_80422::_currentWindCount++;
if(currentTime<SDL_Weather_80422::_shortestWindTime)
{
SDL_Weather_80422::_shortestWindTime=currentTime;
}
}
}
unsigned long currentRainMin;
unsigned long lastRainTime;
void serviceInterruptRain()
{
unsigned long currentTime=(unsigned long) (micros()-lastRainTime);
lastRainTime=micros();
if(currentTime>500) // debounce
{
SDL_Weather_80422::_currentRainCount++;
// interrupt_count[19]++;
if(currentTime<currentRainMin)
{
currentRainMin=currentTime;
}
}
// interrupt_count[18]++;
}
long SDL_Weather_80422::_currentWindCount =0;
long SDL_Weather_80422::_currentRainCount =0;
unsigned long SDL_Weather_80422::_shortestWindTime =0;
SDL_Weather_80422::SDL_Weather_80422(int pinAnem, int pinRain, int intAnem, int intRain, int ADChannel, int ADMode)
{
//pinMode(pinAnem, INPUT);
//pinMode(pinRain, INPUT);
_pinAnem = pinAnem;
_pinRain = pinRain;
_intAnem = intAnem;
_intRain = intRain;
_ADChannel = ADChannel;
_ADMode = ADMode;
_currentRainCount = 0;
_currentWindCount = 0;
_currentWindSpeed = 0.0;
_currentWindDirection = 0.0;
lastWindTime = 0;
_shortestWindTime = 0xffffffff;
_sampleTime = 5.0;
_selectedMode = SDL_MODE_SAMPLE;
_startSampleTime = micros();
// set up interrupts
pinMode(pinAnem, INPUT); // pinAnem is input to which a switch is connected
digitalWrite(pinAnem, HIGH); // Configure internal pull-up resistor
pinMode(pinRain, INPUT); // pinRain is input to which a switch is connected
digitalWrite(pinRain, HIGH); // Configure internal pull-up resistor
attachInterrupt(_intAnem, serviceInterruptAnem, RISING);
attachInterrupt(_intRain, serviceInterruptRain, RISING);
if (_ADMode == SDL_MODE_I2C_ADS1015)
ads1015.begin();
}
float SDL_Weather_80422::get_current_rain_total()
{
float rain_amount = 0.2794 * _currentRainCount/2; // mm of rain - we get two interrupts per bucket
_currentRainCount = 0;
return rain_amount;
}
#define WIND_FACTOR 2.400
float SDL_Weather_80422::current_wind_speed() // in milliseconds
{
if (_selectedMode == SDL_MODE_SAMPLE)
{
_currentWindSpeed = get_current_wind_speed_when_sampling();
}
else
{
// km/h * 1000 msec
_currentWindCount = 0;
delay(_sampleTime*1000);
_currentWindSpeed = ((float)_currentWindCount/_sampleTime) * WIND_FACTOR;
}
return _currentWindSpeed;
}
float SDL_Weather_80422::get_wind_gust()
{
unsigned long latestTime;
latestTime =_shortestWindTime;
_shortestWindTime=0xffffffff;
double time=latestTime/1000000.0; // in microseconds
return (1/(time))*WIND_FACTOR/2;
}
float SDL_Weather_80422::current_wind_direction()
{
float voltageValue;
float Vcc = PowerVoltage;
if (_ADMode == SDL_MODE_I2C_ADS1015)
{
int value = ads1015.readADC_SingleEnded(1); // AIN1 wired to wind vane on WeatherPiArduino
voltageValue = value*0.003f;
}
else
{
// use internal A/D converter
voltageValue = (analogRead(_ADChannel)/1023.0)*Vcc;
}
float direction = voltageToDegrees(voltageValue, _currentWindDirection);
return direction;
}
float SDL_Weather_80422::current_wind_direction_voltage()
{
float voltageValue;
float Vcc = PowerVoltage;
if (_ADMode == SDL_MODE_I2C_ADS1015)
{
int value = ads1015.readADC_SingleEnded(1); // AIN1 wired to wind vane on WeatherPiArduino
voltageValue = value*0.003f;
}
else
{
// use internal A/D converter
voltageValue = (analogRead(_ADChannel)/1023.0)*Vcc;
}
return voltageValue;
}
void SDL_Weather_80422::reset_rain_total()
{
_currentRainCount = 0;
}
float SDL_Weather_80422::accessInternalCurrentWindDirection()
{
return _currentWindDirection;
}
void SDL_Weather_80422::reset_wind_gust()
{
_shortestWindTime = 0xffffffff;
}
// ongoing samples in wind
void SDL_Weather_80422::startWindSample(float sampleTime)
{
_startSampleTime = micros();
_sampleTime = sampleTime;
}
float SDL_Weather_80422::get_current_wind_speed_when_sampling()
{
unsigned long compareValue;
compareValue = _sampleTime*1000000;
if (micros() - _startSampleTime >= compareValue)
{
// sample time exceeded, calculate currentWindSpeed
float _timeSpan;
// _timeSpan = (unsigned long)(micros() - _startSampleTime);
_timeSpan = (micros() - _startSampleTime);
_currentWindSpeed = ((float)_currentWindCount/(_timeSpan)) * WIND_FACTOR*1000000;
_currentWindCount = 0;
_startSampleTime = micros();
}
else
{
//Serial.println("NOT Triggered");
//Serial.print("time = ");
//Serial.println(micros() - _startSampleTime);
// if not, then return last wind speed
}
return _currentWindSpeed;
}
void SDL_Weather_80422::setWindMode(int selectedMode, float sampleTime) // time in seconds
{
_sampleTime = sampleTime;
_selectedMode = selectedMode;
if (_selectedMode == SDL_MODE_SAMPLE)
{
startWindSample(_sampleTime);
}
}