[1] #include [2] #include [3] #include [4] // Data logger for the Arduino UNO computer [5] // This version logs data to the SD card after each set of data is taken [6] // the number of samples to be taken and averaged, at each z-location of the detector 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 */ [7] // how many milliseconds between sampling data and logging it. 1000 ms is once a second const int sampleInterval = 100; /* 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 */ [8] char ch; // Holds incoming character from the serial monitor [9] int z; // starting z-position of the detector [19] // The analog pin that is connected to the sensor const int photocellPin = 2; // analog pin 2 int photocellReading = 0; [11] // the Adafruit data logging shield uses digital pin 10 for the SD card CS ("chip select") pin const int chipSelect = 10; [12] // the logging file [13] File logfile; [14] // Error handling function [15] void error(char *str) [16] { [17] Serial.print("error: "); // prints the word "error" to the serial monitor [18] Serial.println(str); // prints the error message to the serial monitor along with a carriage return [19] while(1); // this condition would always be true, causing the program to stop here [20] } [21] // ============================================================ [22] void setup() [23] { [24] Serial.begin(9600); [25] Serial.println(); [26] pinMode(10,OUTPUT); [27] if (!SD.begin(chipSelect)) [28] { [29] error("Card failed, or not present"); [30] } [31] // set up the filename on the SC card to log data to [32] // it will be a CSV "comma-separated-value" format so Excel can easily read the data into separate columns char filename[] = "LOGGER00.CSV"; [33] for (uint8_t i = 0; i < 100; i++) [34] { [35] filename[6] = i/10 + '0'; [36] filename[7] = i%10 + '0'; [37] if (! SD.exists(filename)) [38] { [39] // only open a new file if it doesn't exist [40] logfile = SD.open(filename, FILE_WRITE); [41] break; // leave the loop! [42] } [43] } [44] if (! logfile) [45] { [46] error("couldn't create file"); [47] } [48] Serial.print("Logging to: "); [49] Serial.println(filename); [50] logfile.println("z,LIGHT"); // prints this text to the logging file [51] Serial.println("z,LIGHT"); // prints the same text to the serial monitor (if ECHO_TO_SERIAL is set to 1 TRUE) [52] #endif //ECHO_TO_SERIAL [53] Serial.println("Enter new z-value:"); [54] } // end of the SETUP function // ======================================================================== /* THE MAIN PROGRAM */ [55] void loop() [56] { [57] if(Serial.available()>0) [58] { [59] ch = Serial.peek(); [60] if((ch == 'e')||(ch == 'E')) [61] { [62] logfile.flush(); [63] Serial.print("Data Taking Completed"); [64] while(1); [65] } [66] z = Serial.parseInt(); [67] if(Serial.read() == '\n') [68] { [69] Serial.print("z = "); [70] Serial.print(z); [71] Serial.print(" "); [72] for (int i = 0; i < numSamples; i ++) [73] { [74] analogRead(photocellPin); [75] delay(10); // a 10ms delay [76] photocellReading = photocellReading + analogRead(photocellPin); [77] } [78] photocellReading = photocellReading/numSamples; [79] Serial.print("Det. Avg. = "); [80] Serial.print(photocellReading); [81] Serial.println(); [82] logfile.print(z); [83] logfile.print(", "); [84] logfile.print(photocellReading); [85] logfile.println(); // adds a carriage return ('new line') to the data stream [86] } [87] Serial.println("Enter new z-value:"); [88] } [89] }