#include #include #include #include // Data logger for the Arduino UNO computer // This version logs data to the SD card after each set of data is taken // the total number of samples to be taken const int numSamples = 20; /* total number of samples to be taken; defining the integer variable 'numSamples' by declaring it to be a constant ensures that no other part of the program will change it from this value */ // how many milliseconds between sampling data and logging it. 1000 ms is once a second const int sampleInterval = 1000; /* msec between data samples; defining the integer variable 'sampleInterval' by declaring it to be a constant ensures that no other part of the program will change it from this value */ #define ECHO_TO_SERIAL 1 // echo data to serial port; set to 0 if you don't want to echo data to the serial monitor #define WAIT_TO_START 1 // Wait for serial input in setup(); set to 0 if you don't want to use a keyboard entry to begin data taking float aref_voltage = 5; // set the UNO analog reference voltage to 5 volts // The analog pin that is connected to the sensor const int photocellPin = 0; // analog pin 0 RTC_DS1307 RTC; // define the Real Time Clock object // the Adafruit data logging shield uses digital pin 10 for the SD card CS ("chip select") pin const int chipSelect = 10; // the logging file File logfile; // Error handling function void error(char *str) { Serial.print("error: "); // prints the word "error" to the serial monitor Serial.println(str); // prints the error message to the serial monitor along with a carriage return while(1); // this condition would always be true, causing the program to stop here } void setup() { Serial.begin(9600); Serial.println(); analogReference(EXTERNAL); /* sets the reference (maximum) voltage for the built-in 10-bit analog-to-digital converter to be the voltage applied to the AREF pin. As previously defined by the variable 'aref_voltage' this will be set to +5V. In turn, this "maps" the 10-bit range of the ADC to 0-5V: digital '0' corresponds to 0V, and digital '1023' corresponds to 5V. */ // set up the filename on the SC card to log data to // it will be a CSV "comma-separated-value" format so Excel can easily read the data into separate columns char filename[] = "LOGGER00.CSV"; for (uint8_t i = 0; i < 100; i++) { filename[6] = i/10 + '0'; filename[7] = i%10 + '0'; if (! SD.exists(filename)) { // only open a new file if it doesn't exist logfile = SD.open(filename, FILE_WRITE); break; // leave the loop! } } if (! logfile) { error("couldn't create file"); } Serial.print("Logging to: "); Serial.println(filename); // connect to RTC Wire.begin(); if (!RTC.begin()) /* If the RTC is functioning properly, this IF loop does NOT need to execute: RTC.begin() will return TRUE (a '1'), the NOT operator ! will cause FALSE to be returned to the IF statement, and the IF statement will NOT be executed. If the RTC is NOT functioning properly, this IF loop DOES need to execute; RTC.begin() will return FALSE (a '0'), the NOT operator ! will cause TRUE to be returned to the IF statement, and the IF statement WILL be executed. */ { logfile.println("RTC failed"); #if ECHO_TO_SERIAL Serial.println("RTC failed"); #endif } logfile.println("TIME,LIGHT"); // prints this text to the logging file #if ECHO_TO_SERIAL Serial.println("TIME,LIGHT"); // prints the same text to the serial monitor (if ECHO_TO_SERIAL is set to 1 TRUE) #endif //ECHO_TO_SERIAL } // end of the SETUP function /* THE MAIN PROGRAM */ void loop() { DateTime now; for (int i = 0; i < numSamples; i ++) { // fetch the time now = RTC.now(); // log time logfile.print(now.hour(), DEC); logfile.print(":"); logfile.print(now.minute(), DEC); logfile.print(":"); logfile.print(now.second(), DEC); #if ECHO_TO_SERIAL Serial.print(now.hour(), DEC); Serial.print(":"); Serial.print(now.minute(), DEC); Serial.print(":"); Serial.print(now.second(), DEC); #endif analogRead(photocellPin); delay(10); // a 10ms delay int photocellReading = analogRead(photocellPin); logfile.print(", "); logfile.print(photocellReading); logfile.println(); // adds a carriage return ('new line') to the data stream #if ECHO_TO_SERIAL Serial.print(", "); Serial.print(photocellReading); Serial.println(); #endif //ECHO_TO_SERIAL delay(sampleInterval); } // finally, actually write all of the data to the SD card! logfile.flush(); while(1); }