-
Notifications
You must be signed in to change notification settings - Fork 0
/
EDFTest_AnemometerIncluded.ino
211 lines (163 loc) · 8.52 KB
/
EDFTest_AnemometerIncluded.ino
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
int voltage = 0;
int Time = 0;
int prevoltage = 0;
const byte interruptPin = 3; //anemomter input to digital pin
volatile unsigned long sTime = 0; //stores start time for wind speed calculation
unsigned long dataTimer = 0; //used to track how often to communicate data
volatile float pulseTime = 0; //stores time between one anemomter relay closing and the next
volatile float culPulseTime = 0; //stores cumulative pulsetimes for averaging
volatile bool start = true; //tracks when a new anemometer measurement starts
volatile unsigned int avgWindCount = 0; //stores anemometer relay counts for doing average wind speed
float aSetting = 60.0; //wind speed setting to signal alarm
// Arduino with load cell
// Put two known loads on the sensor and take readings. Put those values
// here!!!!!!!!!!!!!!!!
float aReading = 192.0;
float aLoad = 15.0; // lbs.
float bReading = 344.0;
float bLoad = 24.3; // lbs.
long time = 0;
int interval = 200; // Take a reading every 200 ms
// Below 2 lines are from Johnathan. A0 pin from original changed to A3. Should not have any conflicts.*
const int sensorPin = A3;
float sensorValue;
unsigned long rTime = millis();
if((rTime - sTime) > 2500) pulseTime = 0; //if the wind speed has dropped below 1MPH than set it to zero
if((rTime - dataTimer) > 1800){ //See if it is time to transmit
detachInterrupt(interruptPin); //shut off wind speed measurement interrupt until done communication
float aWSpeed = getAvgWindSpeed(culPulseTime,avgWindCount); //calculate average wind speed
if(aWSpeed >= aSetting) digitalWrite(13, HIGH); // high speed wind detected so turn the LED on
else digitalWrite(13, LOW); //no alarm so ensure LED is off
culPulseTime = 0; //reset cumulative pulse counter
avgWindCount = 0; //reset average wind count
float aFreq = 0; //set to zero initially
if(pulseTime > 0.0) aFreq = getAnemometerFreq(pulseTime); //calculate frequency in Hz of anemometer, only if pulsetime is non-zero
float wSpeedMPH = getWindMPH(aFreq); //calculate wind speed in MPH, note that the 2.5 comes from anemometer data sheet
//*****************Arduino anemometer sketch******************************
const byte interruptPin = 3; //anemomter input to digital pin
volatile unsigned long sTime = 0; //stores start time for wind speed calculation
unsigned long dataTimer = 0; //used to track how often to communicate data
volatile float pulseTime = 0; //stores time between one anemomter relay closing and the next
volatile float culPulseTime = 0; //stores cumulative pulsetimes for averaging
volatile bool start = true; //tracks when a new anemometer measurement starts
volatile unsigned int avgWindCount = 0; //stores anemometer relay counts for doing average wind speed
float aSetting = 60.0; //wind speed setting to signal alarm
void setup() {
pinMode(13, OUTPUT); //setup LED pin to signal high wind alarm condition
pinMode(interruptPin, INPUT_PULLUP); //set interrupt pin to input pullup
attachInterrupt(interruptPin, anemometerISR, RISING); //setup interrupt on anemometer input pin, interrupt will occur whenever falling edge is detected
dataTimer = millis(); //reset loop timer
Serial.begin(9600);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(13, OUTPUT); //setup LED pin to signal high wind alarm condition. Won't be used!!!!!
pinMode(interruptPin, INPUT_PULLUP); //set interrupt pin to input pullup
attachInterrupt(interruptPin, anemometerISR, RISING); //setup interrupt on anemometer input pin, interrupt will occur whenever falling edge is detected
dataTimer = millis(); //reset loop timer
}
void loop() {
unsigned long rTime = millis();
if((rTime - sTime) > 2500) pulseTime = 0; //if the wind speed has dropped below 1MPH than set it to zero
if((rTime - dataTimer) > 1800){ //See if it is time to transmit
// Baseline Temperature in Celsius
if (Time < 300) {
Serial.print("Baseline Temperature in Celsius = ");
Serial.println(-40 + 0.488155 * (analogRead(A0) - 20));
detachInterrupt(interruptPin); //shut off wind speed measurement interrupt until done communication
float aWSpeed = getAvgWindSpeed(culPulseTime,avgWindCount); //calculate average wind speed
if(aWSpeed >= aSetting) digitalWrite(13, HIGH); // high speed wind detected so turn the LED on
else digitalWrite(13, LOW); //no alarm so ensure LED is off
culPulseTime = 0; //reset cumulative pulse counter
avgWindCount = 0; //reset average wind count
float aFreq = 0; //set to zero initially
if(pulseTime > 0.0) aFreq = getAnemometerFreq(pulseTime); //calculate frequency in Hz of anemometer, only if pulsetime is non-zero
float wSpeedMPH = getWindMPH(aFreq); //calculate wind speed in MPH, note that the 2.5 comes from anemometer data sheet
Serial.begin(57600); //start serial monitor to communicate wind data
Serial.println();
Serial.println("...................................");
Serial.print("Anemometer speed in Hz ");
Serial.println(aFreq);
Serial.print("Current wind speed is ");
Serial.println(wSpeedMPH);
Serial.print("Current average wind speed is ");
Serial.println(aWSpeed);
Serial.end(); //serial uses interrupts so we want to turn it off before we turn the wind measurement interrupts back on
start = true; //reset start variable in case we missed wind data while communicating current data out
attachInterrupt(digitalPinToInterrupt(interruptPin), anemometerISR, RISING); //turn interrupt back on
dataTimer = millis(); //reset loop timer
}
}
//using time between anemometer pulses calculate frequency of anemometer
float getAnemometerFreq(float pTime) { return (1/pTime); }
//Use anemometer frequency to calculate wind speed in MPH, note 2.5 comes from anemometer data sheet
float getWindMPH(float freq) { return (freq*2.5); }
//uses wind MPH value to calculate KPH
float getWindKPH(float wMPH) { return (wMPH*1.61); }
//Calculates average wind speed over given time period
float getAvgWindSpeed(float cPulse,int per) {
if(per) return getWindMPH(getAnemometerFreq((float)(cPulse/per)));
else return 0; //average wind speed is zero and we can't divide by zero
}
//This is the interrupt service routine (ISR) for the anemometer input pin
//it is called whenever a falling edge is detected
void anemometerISR() {
unsigned long cTime = millis(); //get current time
if(!start) { //This is not the first pulse and we are not at 0 MPH so calculate time between pulses
// test = cTime - sTime;
pulseTime = (float)(cTime - sTime)/1000;
culPulseTime += pulseTime; //add up pulse time measurements for averaging
avgWindCount++; //anemomter went around so record for calculating average wind speed
}
sTime = cTime; //store current time for next pulse time calculation
start = false; //we have our starting point for a wind speed measurement
}
// Time Counter. Every 200 milliseconds it adds 200
// to time. 1 millisecond=1 time.
delay(200); // Wait for 200 millisecond(s)
Time += 200;
Serial.print("Time_");
Serial.println(Time);
float newReading = analogRead(5);
// Calculate load based on A and B readings above
float load = ((bLoad - aLoad)/(bReading - aReading)) * (newReading - aReading) + aLoad;
// millis returns the number of milliseconds since the board started the current program
if(millis() > time + interval) {
Serial.print("Reading: ");
Serial.print(newReading,1); // 1 decimal place
Serial.print(" Load: ");
Serial.println(load,1); // 1 decimal place, println adds a carriage return
time = millis();
// Voltage Math. Calculates from prevoltage to
// voltage by 34.093. ONLY ONE DIGIT FOR SOME
// REASON!
delay(100); // Wait for 100 millisecond(s)
prevoltage = analogRead(A1);
delay(100); // Wait for 100 millisecond(s)
voltage = (prevoltage / 34.093);
// Voltage. (Prints voltage)
delay(200); // Wait for 200 millisecond(s)
Serial.print("MAINVoltage_");
Serial.println(voltage);
// Prevoltage. Displays true analog voltage. Must
// be divided by 34.093 for a more accurate read.
delay(200); // Wait for 200 millisecond(s)
Serial.print("PREVoltage_");
Serial.println(prevoltage);
Serial.println("-----------------");
// Johnathan's sensor data
sensorValue = (analogRead(sensorPin)/1024.0)*5.0;
Serial.print("The data-line voltage is ");
Serial.print(sensorValue);
Serial.println(" Volts.");
Serial.print("\n");
sensorValue = abs(sensorValue-2.5)/0.0133;
Serial.print("The measured current is ");
Serial.print(sensorValue);
Serial.println(" Amps.");
Serial.print("\n");
delay(500.0);
}
setup() {
}
loop() {
}