forked from dttaylo2/Sensor_Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArduino_Implementation
More file actions
194 lines (147 loc) · 4.75 KB
/
Arduino_Implementation
File metadata and controls
194 lines (147 loc) · 4.75 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
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
#include <SoftwareSerial.h>
#include <Wire.h> // I2C library, required for MLX90614
#include <SparkFunMLX90614.h> // Temp Sensor : SparkFunMLX90614 Arduino library
SoftwareSerial serialComm(0, 1); // RX, TX
IRTherm therm; // Create an IRTherm object to interact with throughout
int inPinI = 1; //
int sampleI;
double filteredI;
double offsetI = 512;
double sqI;
double sumI;
double ICAL = 62.1;
double Irms;
//value of port used by sensor
const int sensor2 = 2;
const int sensor3 = 3;
//raw data buffers
int rawBuffer0[10];
int rawBuffer1[10];
int rawBuffer2[10];
int rawBuffer3[10];
//real valued buffers
float realDataBuffer0[10];
float realDataBuffer1[10];
float realDataBuffer2[10];
float realDataBuffer3[10];
float avgVal0;
float avgVal1;
float avgVal2;
float avgVal3;
String timeStamp;
void setup() {
//Clear the average values variables
avgVal0 = 0;
avgVal1 = 0;
avgVal2 = 0;
avgVal3 = 0;
therm.begin(); // Initialize thermal IR sensor
therm.setUnit(TEMP_C); // Set the library's units to Celcius with TEMP_C
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() {
//Collect raw data readings
for (int i=0 ; i<10 ; i++){
//Collect raw data readings
//Call therm.read() to read object and ambient temperatures from the sensor.
therm.read();
//rawBuffer2[i] = analogRead(sensor2);
//rawBuffer3[i] = analogRead(sensor3);
//Converted raw data
realDataBuffer0[i] = calcIrms(1480); // Calculate Irms
realData1Buffer[i] = therm.object();
// realData2Buffer[i] = rawBuffer2[i] * (5.0 / 1023.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V);
// realData3Buffer[i] = rawBuffer3[i] * (5.0 / 1023.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V);
}
//Get average values from buffers
for (int j=0 ; j<10 ; j++){
avgVal0 = avgVal0 + realDataBuffer0[j];
}
for (int j=0 ; j<10 ; j++){
avgVal1 = avgVal1 + realDataBuffer1[j];
}
for (int j=0 ; j<10 ; j++){
avgVal2 = avgVal2 + realDataBuffer2[j];
}
for (int j=0 ; j<10 ; j++){
avgVal3 = avgVal3 + realDataBuffer3[j];
}
//Print data to serial monitor for testing purposes
/*
Serial.print(realData0);
serialComm.print("V, ");
Serial.println("");
delay(100);
Serial.print(realData2);
Serial.print("V, ");
Serial.println("");
Serial.println("");
delay(100);
Serial.print(realData2);
Serial.print("V, ");
Serial.println("");
Serial.println("");
delay(100);
Serial.print(realData2);
Serial.print("V, ");
Serial.println("");
Serial.println("");
delay(100);
*/
//transmit data to RasPi
/*
serialComm.print(avgVal0); //current
serialComm.write(avgVal1); //temp
serialComm.write(avgVal2); //vibration
serialComm.write(avgVal3); //speed
*/
}
//-----------------------------------------------------------------
double calcIrms(unsigned int Number_of_Samples)
{
int SupplyVoltage = readVcc();
for (unsigned int n = 0; n < Number_of_Samples; n++)
{
sampleI = analogRead(inPinI);
// Digital low pass filter extracts the 2.5 V or 1.65 V dc offset,
// then subtract this - signal is now centered on 0 counts.
offsetI = (offsetI + (sampleI-offsetI)/1024);
filteredI = sampleI - offsetI;
// Root-mean-square method current
// 1) square current values
sqI = filteredI * filteredI;
// 2) sum
sumI += sqI;
}
double I_RATIO = ICAL *((SupplyVoltage/1000.0) / (1024));
Irms = I_RATIO * sqrt(sumI / Number_of_Samples);
//Reset accumulators
sumI = 0;
return Irms;
}
//------------------------------------------------------------------
long readVcc() {
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA,ADSC)); // measuring
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
long result = (high<<8) | low;
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}
//-----------------------------------------------------------------