1 | #include <SFE_BMP180.h>
|
2 |
|
3 | /*
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | * analog sensors on analog ins 0, 1,
|
9 | * SD card attached to SPI bus as follows:
|
10 | ** MOSI - pin 11
|
11 | ** MISO - pin 12
|
12 | ** CLK - pin 13
|
13 | ** CS - pin 4 (digital assigned via setup)
|
14 | ** VCC & Ground
|
15 | -------------------------------------------------------------------
|
16 | V2-
|
17 | Adding RTC support
|
18 | RTC Connected to VCC and Ground
|
19 | I2C Connections:
|
20 | RTC SDA connected to pin Analog 4
|
21 | RTC SCL connected to pin Analog 5
|
22 |
|
23 | Used I2CScanner.ino to find I2C addresses- 0x50 (EEPROM?) and 0x68 found
|
24 | Used SetRTC.ino to manually set proper date/date on RTC module- This wil set the date/time as soon as serial
|
25 | is initiated so set ahead by a minute and wait till time matches then open serial window to set.
|
26 | -----------------------------------------------------------------------
|
27 | V3-
|
28 | Added Adafruit 992- MPL115A2 Baro/Temp Sensor 5volt 12C device
|
29 | SDA connected to pin Analog 4
|
30 | SCL connected to pin Analog 5
|
31 | Code below should be optimized to avoid multiple I2C calls
|
32 |
|
33 | Added Battery Voltage Monitoring:
|
34 | Changed A0 to be used for Bus Voltage monitoring (on board LiPo)
|
35 | Will use voltage divider with 2 10Kohm resistores to monitor the 7.4V supply from battery
|
36 | http://forum.arduino.cc/index.php/topic,13728.0.html
|
37 | Testing- Ran solid 15 hours no issues, 47000 lines recorded, Filesize 2.1MB
|
38 | -----------------------------------------------------------------------
|
39 | V4
|
40 | Added output for altitude in meters (Thanks Jon)
|
41 | http://forum.arduino.cc/index.php?topic=63726.0
|
42 | Used static value of 15 for Temp as using actual temp causes unreliable values indoors
|
43 |
|
44 | ------------------------------------------------------------------------
|
45 | V5
|
46 | Added code for LM35 Temp sensor (Will be internal monitoring) on A1 data2 and temp variables
|
47 | Had to do 2 analog reads to debounce the values with a small delay.
|
48 | Added Bluetooth- shortened serial write text to fit phone screen
|
49 | W
|
50 | ----------------------------------------------------------------------
|
51 | */
|
52 | #include <SFE_BMP180>// Support for Sensor BMP180
|
53 | Adafruit_SFE BMP180//Support for BMP 180#include "Wire.h"
|
54 | #define DS1307_ADDRESS 0x68//RTC Support
|
55 | //Eric Note- another address showed up in scan at 0X50 EEPROM?
|
56 | byte zero = 0x00; //workaround for issue #527 RTC Support
|
57 |
|
58 | const float referenceVolts = 5.0; // the default reference on a 5 volt board
|
59 | const int batteryPin = 0; // +V from battery is connected to analog pin 0
|
60 | const float sea_press = 1013.25;//added for altitude calc
|
61 | float alt=0;//added for altitude calc
|
62 |
|
63 | #include <SD.h>
|
64 | File myFile; //SD
|
65 | const int chipSelect=4; // set chipselect pin to 4 for SD
|
66 | //int dat1=A0;//set data pin
|
67 | int dat2=1;//define pin0 connect with LM35
|
68 | float temp=0;
|
69 | //int data1=0;
|
70 | int data2=0;
|
71 | int i=0;
|
72 | boolean present=0;// SD Card available & usable
|
73 | void setup()
|
74 | {
|
75 |
|
76 | Wire.begin();
|
77 | Serial.begin(9600);
|
78 | mpl115a2.begin();// Baro Sensor Adafruit 992- MPL115A2
|
79 | //pinMode(dat1,INPUT);
|
80 | pinMode(dat2,INPUT);
|
81 | checkSD();
|
82 | // Write column headers for Excel CSV
|
83 | myFile = SD.open("datalog.csv", FILE_WRITE);
|
84 | if(myFile)
|
85 | {
|
86 | //Prints headers to file
|
87 | myFile.print("Time"); // Prints Date and Time header
|
88 | myFile.print(",");
|
89 | myFile.print("line#");// prints line number Header for troubleshooting etc
|
90 | myFile.print(",");
|
91 | myFile.print("BusVoltage");
|
92 | myFile.print(",");
|
93 | myFile.print("IntTemp");
|
94 | myFile.print(",");
|
95 | myFile.print("Pressure");
|
96 | myFile.print(",");
|
97 | myFile.print("ExtTemp");
|
98 | myFile.print(",");
|
99 | myFile.println("Alt(m)");
|
100 | myFile.close();
|
101 |
|
102 | }
|
103 | }
|
104 | void loop()
|
105 | {
|
106 | int val = analogRead(batteryPin); // read the value from the A0 battery monitoring pin with voltage divider
|
107 | float volts = (val / 511.0) * referenceVolts ; // divide val by (1023/2)because the resistors divide the voltage in half
|
108 |
|
109 | float pressureKPA = 0, temperatureC = 0; //Adafruit 992- MPL115A2
|
110 | if(present==1)
|
111 | {
|
112 | pressureKPA = mpl115a2.getPressure();//get pressure Adafruit 992- MPL115A2
|
113 | temperatureC = mpl115a2.getTemperature(); //Adafruit 992- MPL115A2
|
114 | printDate(); //Runs the print date subroutine below
|
115 | i++;
|
116 | //data1=analogRead(dat1);
|
117 | analogRead(1);// added to take initial read to stabilize the ADC
|
118 | delay(10);// Delay to stabilize the ADC
|
119 | temp = analogRead(1) * 5000L / 1024L / 10;// Temperature calculation formula
|
120 | //data2=analogRead(dat2); //used for Ext Temp sensor TBD
|
121 | //temp= data2 * 4.9 / 10 ; // Temperature calculation formula
|
122 | Serial.print(" BusV:");
|
123 | Serial.print(volts);
|
124 | Serial.print(" ITemp:");
|
125 | Serial.print(temp);
|
126 | Serial.print(" Press:");
|
127 | Serial.print(pressureKPA, 4);
|
128 | Serial.print(" ExTemp:");
|
129 | Serial.print(temperatureC, 1);
|
130 | alt= ((pow((sea_press / (pressureKPA *10)), 1/5.257) - 1.0) * (15 + 273.15)) / 0.0065;//rough altitude equation in meters can use real temp instead of 15
|
131 | Serial.print(" Alt:");
|
132 | Serial.println(alt);
|
133 | writeSD();
|
134 | }
|
135 | delay(1000);
|
136 | }
|
137 |
|
138 | byte decToBcd(byte val){ //Eric- needed for RTC variable stuff
|
139 | // Convert normal decimal numbers to binary coded decimal
|
140 | return ( (val/10*16) + (val%10) );
|
141 | }
|
142 |
|
143 | byte bcdToDec(byte val) { //Eric- needed for RTC variable stuff
|
144 | // Convert binary coded decimal to normal decimal numbers
|
145 | return ( (val/16*10) + (val%16) );
|
146 | }
|
147 |
|
148 | void checkSD()
|
149 | {
|
150 | Serial.print("check SD card");
|
151 | if (!SD.begin(chipSelect)) {
|
152 | Serial.println("Card failed");
|
153 | // don't do anything more:
|
154 | return;
|
155 | }
|
156 | Serial.println("SD card OK");
|
157 | present=1;
|
158 | delay(2000);
|
159 | }
|
160 |
|
161 | void writeSD()
|
162 | {
|
163 | myFile = SD.open("datalog.csv", FILE_WRITE);
|
164 | if(myFile)
|
165 | {
|
166 | int val = analogRead(batteryPin); // read the value from the A0 battery monitoring pin with voltage divider
|
167 | float volts = (val / 511.0) * referenceVolts ; // divide val by (1023/2)because the resistors divide the voltage in half
|
168 | // Reset the register pointer
|
169 | Wire.beginTransmission(DS1307_ADDRESS);
|
170 | Wire.write(zero);
|
171 | Wire.endTransmission();
|
172 |
|
173 | Wire.requestFrom(DS1307_ADDRESS, 7);
|
174 |
|
175 | int second = bcdToDec(Wire.read());
|
176 | int minute = bcdToDec(Wire.read());
|
177 | int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
|
178 | int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
|
179 | int monthDay = bcdToDec(Wire.read());
|
180 | int month = bcdToDec(Wire.read());
|
181 | int year = bcdToDec(Wire.read());
|
182 | float pressureKPA = mpl115a2.getPressure();//get pressure Adafruit 992- MPL115A2- these are redundant calls to I2c but I dont want to fix
|
183 | float temperatureC = mpl115a2.getTemperature();//get temp Adafruit 992- MPL115A2- these are redundant calls to I2c but I dont want to fix
|
184 |
|
185 | //Print RTC values to SD
|
186 | myFile.print(month);
|
187 | myFile.print("/");
|
188 | myFile.print(monthDay);
|
189 | myFile.print("/");
|
190 | myFile.print(year);
|
191 | myFile.print(" ");
|
192 | myFile.print(hour);
|
193 | myFile.print(":");
|
194 | myFile.print(minute);
|
195 | myFile.print(":");
|
196 | myFile.print(second);
|
197 | //myFile.print(":");
|
198 | myFile.print(",");
|
199 | //End Print RTC values
|
200 | myFile.print(i);
|
201 | myFile.print(",");
|
202 | myFile.print(volts);
|
203 | myFile.print(",");
|
204 | myFile.print(temp);
|
205 | myFile.print(",");
|
206 | myFile.print(pressureKPA, 4);
|
207 | myFile.print(",");
|
208 | myFile.print(temperatureC, 1);
|
209 | myFile.print(",");
|
210 | myFile.println(alt);
|
211 | myFile.close();
|
212 |
|
213 | }
|
214 | }
|
215 |
|
216 | void printDate()// Only used to print to serial and not needed for logging purposes
|
217 | {
|
218 | Wire.beginTransmission(DS1307_ADDRESS);
|
219 | Wire.write(zero);
|
220 | Wire.endTransmission();
|
221 |
|
222 | Wire.requestFrom(DS1307_ADDRESS, 7);
|
223 |
|
224 | int second = bcdToDec(Wire.read());
|
225 | int minute = bcdToDec(Wire.read());
|
226 | int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
|
227 | int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
|
228 | int monthDay = bcdToDec(Wire.read());
|
229 | int month = bcdToDec(Wire.read());
|
230 | int year = bcdToDec(Wire.read());
|
231 |
|
232 | //print the date EG 3/1/11 23:59:59
|
233 | Serial.print(month);
|
234 | Serial.print("/");
|
235 | Serial.print(monthDay);
|
236 | Serial.print("/");
|
237 | Serial.print(year);
|
238 | Serial.print(" ");
|
239 | Serial.print(hour);
|
240 | Serial.print(":");
|
241 | Serial.print(minute);
|
242 | Serial.print(":");
|
243 | Serial.print(second);
|
244 |
|
245 | }
|