-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnemometer.cpp
More file actions
78 lines (67 loc) · 4.17 KB
/
Anemometer.cpp
File metadata and controls
78 lines (67 loc) · 4.17 KB
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
/************************************************************************************************************
* Anemometer.cpp - Arduino library for making an extractor into an anenometer * *
* Copyright 2014 Joel Cormack (joel.greta@gmail.com) *
************************************************************************************************************
************************************************************************************************************
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
***********************************************************************************************************/
#define _USE_MATH_DEFINES
#include <math.h>
#include "Arduino.h"
#include "Anemometer.h"
/*this library gives back the miles per hour speed of a wind extractor. the magnet is fixed to the circumference of the extractor and a
*hall effect sensor is wired into an arduino. the class takes the pin number of the hall effect sensor, the circumference of the extractor
*and the time interval between measurements or frequency of measurements.
*
*pin = microprocessor pin intput
*radius = radius of center of extractor to sensor in centimeters
*interval = time interval in milliseconds */
Anemometer::Anemometer(int pin, int radius, int interval) {
_pin=pin;
_radius=radius;
_interval=interval;
analogReference(DEFAULT);
}
//the following function uses the formulae from http://www.dfrobot.com/wiki/index.php/SHARP_GP2Y0A41SK0F_IR_ranger_sensor_%284-30cm%29_SKU:SEN0143
//converts the signal from the analog pin to cm
double Anemometer::mph() {
_rotationsPerInterval = countrotations(); //call countrotations
_rotationsPerSecond = (_rotationsPerInterval/_interval)*1000; //convert roations back to milliseconds and multiply by 1000 to get rotations per second
_metersPerSecond = _rotationsPerSecond * circuference; //times the distance in meters with roations per hour to get meters per hour
_mph = mps2mph(_metersPerSecond);
return _mph;
}
float Anemometer::mps2mph(float _mps){
return _mps * 2.2369362920544;
}
int Anemometer::circumference(){
_radiusMeters = _radius/100;
return (2 * _radiusMeters) * M_PI;
}
/*this function calculates the rotations of the extractor every interval
*and returns rotations per interval
*/
int Anemometer::countRotations(){
_rotations=0;
//while time passed is less than interval, count rotations
while((unsigned long)(millis() - _previousMillis) <= _interval){
if(!digitalRead (_pin)){ //if magnet is near sensor
_rotations++; //add one to rotations
while(!digitalRead (_pin)){ //while the sensor is open
}//end while //do nothing special..
}//end if
}//end while
return _rotations;
}