|
| 1 | +#include <SD.h> |
| 2 | +#include <Wire.h> |
| 3 | +#include "RTClib.h" |
| 4 | + |
| 5 | +// how many milliseconds between grabbing data and logging it. 1000 ms is once a second |
| 6 | +#define LOG_INTERVAL 100 // mills between entries (reduce to take more/faster data) |
| 7 | + |
| 8 | +// how many milliseconds before writing the logged data permanently to disk |
| 9 | +// set it to the LOG_INTERVAL to write each time (safest) |
| 10 | +// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to |
| 11 | +// the last 10 reads if power is lost but it uses less power and is much faster! |
| 12 | +#define SYNC_INTERVAL 1*LOG_INTERVAL // mills between calls to flush() - to write data to the card |
| 13 | +uint32_t syncTime = 0; // time of last sync() |
| 14 | + |
| 15 | +#define ECHO_TO_SERIAL 1 // echo data to serial port |
| 16 | +#define WAIT_TO_START 0 // Wait for serial input in setup() |
| 17 | + |
| 18 | +// the digital pins that connect to the LEDs |
| 19 | +#define redLEDpin 2 |
| 20 | +#define greenLEDpin 3 |
| 21 | + |
| 22 | + |
| 23 | +#define aref_voltage 3.3 // we tie 3.3V to ARef and measure it with a multimeter! |
| 24 | +#define bandgap_voltage 1.1 // this is not super guaranteed but its not -too- off |
| 25 | + |
| 26 | +RTC_DS1307 RTC; // define the Real Time Clock object |
| 27 | + |
| 28 | +// for the data logging shield, we use digital pin 10 for the SD cs line |
| 29 | +const int chipSelect = 10; |
| 30 | + |
| 31 | +const int z = A0; |
| 32 | +const int y = A1; |
| 33 | +const int x = A2; |
| 34 | + |
| 35 | +const int sens = 300; // mv/g from spec sheet |
| 36 | + |
| 37 | +const int x_off = 510; |
| 38 | +const int y_off = 506; |
| 39 | +const int z_off = 520; |
| 40 | + |
| 41 | +const int button = 5; //input pin for push button |
| 42 | +int val = 0; |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +// the logging file |
| 47 | +File logfile; |
| 48 | + |
| 49 | +void error(char *str) |
| 50 | +{ |
| 51 | + Serial.print("error: "); |
| 52 | + Serial.println(str); |
| 53 | + |
| 54 | + // red LED indicates error |
| 55 | + digitalWrite(redLEDpin, HIGH); |
| 56 | + |
| 57 | + while(1); |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +void setup() |
| 63 | +{ |
| 64 | + analogReference(EXTERNAL); |
| 65 | + Serial.begin(9600); // sets the serial port to 9600 |
| 66 | + pinMode(button, INPUT); |
| 67 | + |
| 68 | + // use debugging LEDs |
| 69 | + pinMode(redLEDpin, OUTPUT); |
| 70 | + pinMode(greenLEDpin, OUTPUT); |
| 71 | + |
| 72 | + #if WAIT_TO_START |
| 73 | + Serial.println("Type any character to start"); |
| 74 | + while (!Serial.available()); |
| 75 | + #endif //WAIT_TO_START |
| 76 | + |
| 77 | + // initialize the SD card |
| 78 | + Serial.print("Initializing SD card..."); |
| 79 | + // make sure that the default chip select pin is set to |
| 80 | + // output, even if you don't use it: |
| 81 | + pinMode(10, OUTPUT); |
| 82 | + |
| 83 | + // see if the card is present and can be initialized: |
| 84 | + if (!SD.begin(chipSelect)) { |
| 85 | + error("Card failed, or not present"); |
| 86 | + } |
| 87 | + Serial.println("card initialized."); |
| 88 | + |
| 89 | + // create a new file |
| 90 | + char filename[] = "LOGGER00.CSV"; |
| 91 | + for (uint8_t i = 0; i < 100; i++) { |
| 92 | + filename[6] = i/10 + '0'; |
| 93 | + filename[7] = i%10 + '0'; |
| 94 | + if (! SD.exists(filename)) { |
| 95 | + // only open a new file if it doesn't exist |
| 96 | + logfile = SD.open(filename, FILE_WRITE); |
| 97 | + break; // leave the loop! |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + if (! logfile) { |
| 102 | + error("couldnt create file"); |
| 103 | + } |
| 104 | + |
| 105 | + Serial.print("Logging to: "); |
| 106 | + Serial.println(filename); |
| 107 | + |
| 108 | + // connect to RTC |
| 109 | + Wire.begin(); |
| 110 | + if (!RTC.begin()) { |
| 111 | + logfile.println("RTC failed"); |
| 112 | + #if ECHO_TO_SERIAL |
| 113 | + Serial.println("RTC failed"); |
| 114 | + #endif //ECHO_TO_SERIAL |
| 115 | + } |
| 116 | + |
| 117 | + |
| 118 | + logfile.println("millis \t stamp \t datetime \t x (G) \t y (G) \t z (G) \n "); |
| 119 | +#if ECHO_TO_SERIAL |
| 120 | + Serial.println("millis \t stamp \t datetime \n"); |
| 121 | +#endif //ECHO_TO_SERIAL |
| 122 | + |
| 123 | + // If you want to set the aref to something other than 5v |
| 124 | + analogReference(EXTERNAL); |
| 125 | + |
| 126 | +} |
| 127 | + |
| 128 | +void loop() |
| 129 | +{ |
| 130 | + |
| 131 | + DateTime now; |
| 132 | + |
| 133 | + // delay for the amount of time we want between readings |
| 134 | + delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL)); |
| 135 | + |
| 136 | + digitalWrite(greenLEDpin, HIGH); |
| 137 | + |
| 138 | + // log milliseconds since starting |
| 139 | + uint32_t m = millis(); |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + // fetch the time |
| 144 | + now = RTC.now(); |
| 145 | + |
| 146 | + #if ECHO_TO_SERIAL |
| 147 | + Serial.print(m); // milliseconds since start |
| 148 | + Serial.print("\t "); |
| 149 | + Serial.print(now.unixtime()); // seconds since 1/1/1970 |
| 150 | + Serial.print("\t "); |
| 151 | + Serial.print('"'); |
| 152 | + Serial.print(now.year(), DEC); |
| 153 | + Serial.print("/"); |
| 154 | + Serial.print(now.month(), DEC); |
| 155 | + Serial.print("/"); |
| 156 | + Serial.print(now.day(), DEC); |
| 157 | + Serial.print(" "); |
| 158 | + Serial.print(now.hour(), DEC); |
| 159 | + Serial.print(":"); |
| 160 | + Serial.print(now.minute(), DEC); |
| 161 | + Serial.print(":"); |
| 162 | + Serial.print(now.second(), DEC); |
| 163 | + Serial.print('"'); |
| 164 | + #endif //ECHO_TO_SERIAL |
| 165 | + |
| 166 | + int xRaw = analogRead(x); // read analog input pin 0 |
| 167 | + int yRaw = analogRead(y); // read analog input pin 1 |
| 168 | + int zRaw = analogRead(z); // read analog input pin 1 |
| 169 | + |
| 170 | + float x_g = (xRaw - x_off) * (3300 / 1023.0) / sens; |
| 171 | + float y_g = (yRaw - y_off) * (3300 / 1023.0) / sens; |
| 172 | + float z_g = (zRaw - z_off) * (3300 / 1023.0) / sens; |
| 173 | + //float x_g = map(xRaw, 0, 1023, -3.0, 3.0); |
| 174 | + val = digitalRead(button); |
| 175 | + //Serial.print(val); |
| 176 | + //Serial.print("\t"); |
| 177 | + if (val == HIGH){ |
| 178 | + // log time |
| 179 | + logfile.print(m); // milliseconds since start |
| 180 | + logfile.print("\t "); |
| 181 | + logfile.print(now.unixtime()); // seconds since 1/1/1970 |
| 182 | + logfile.print("\t "); |
| 183 | + logfile.print('"'); |
| 184 | + logfile.print(now.year(), DEC); |
| 185 | + logfile.print("/"); |
| 186 | + logfile.print(now.month(), DEC); |
| 187 | + logfile.print("/"); |
| 188 | + logfile.print(now.day(), DEC); |
| 189 | + logfile.print(" "); |
| 190 | + logfile.print(now.hour(), DEC); |
| 191 | + logfile.print(":"); |
| 192 | + logfile.print(now.minute(), DEC); |
| 193 | + logfile.print(":"); |
| 194 | + logfile.print(now.second(), DEC); |
| 195 | + logfile.print('"'); |
| 196 | + // logfile.print("\t x (G), y (G), z (G): "); |
| 197 | + logfile.print("\t"); |
| 198 | + logfile.print(x_g); // print the acceleration in the X axis |
| 199 | + logfile.print("\t"); // prints a space between the numbers |
| 200 | + logfile.print(y_g); // print the acceleration in the Y axis |
| 201 | + logfile.print("\t"); // prints a space between the numbers |
| 202 | + logfile.println(z_g); // print the acceleration in the Z axis |
| 203 | + #if ECHO_TO_SERIAL |
| 204 | + // Serial.print(" x (G), y (G), z (G): "); |
| 205 | + Serial.print("\t"); |
| 206 | + Serial.print(x_g); // print the acceleration in the X axis |
| 207 | + Serial.print("\t"); // prints a space between the numbers |
| 208 | + Serial.print(y_g); // print the acceleration in the Y axis |
| 209 | + Serial.print("\t"); // prints a space between the numbers |
| 210 | + Serial.println(z_g); // print the acceleration in the Z axis |
| 211 | + #endif // ECHO_TO_SERIAL |
| 212 | + //delay(100); // wait 100ms for next reading |
| 213 | + } else { |
| 214 | + #if ECHO_TO_SERIAL |
| 215 | + Serial.println("\t Button not pushed \n"); |
| 216 | + delay(10); |
| 217 | + #endif |
| 218 | + } |
| 219 | + |
| 220 | + // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card |
| 221 | + // which uses a bunch of power and takes time |
| 222 | + if ((millis() - syncTime) < SYNC_INTERVAL) return; |
| 223 | + syncTime = millis(); |
| 224 | + |
| 225 | + // blink LED to show we are syncing data to the card & updating FAT! |
| 226 | + digitalWrite(redLEDpin, HIGH); |
| 227 | + logfile.flush(); |
| 228 | + digitalWrite(redLEDpin, LOW); |
| 229 | + |
| 230 | +} |
0 commit comments