#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 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 */ // 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 */ #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 char ch; // Holds incoming character from the serial monitor int z; // starting z-position of the detector // The analog pin that is connected to the sensor const int photocellPin = 2; // analog pin 2 int photocellReading = 0; // 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. */ pinMode(10,OUTPUT); if (!SD.begin(chipSelect)) { error("Card failed, or not present"); } // 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); logfile.println("z,LIGHT"); // prints this text to the logging file #if ECHO_TO_SERIAL Serial.println("z,LIGHT"); // prints the same text to the serial monitor (if ECHO_TO_SERIAL is set to 1 TRUE) #endif //ECHO_TO_SERIAL Serial.println("Enter new z-value:"); } // end of the SETUP function // ======================================================================== /* THE MAIN PROGRAM */ void loop() { if(Serial.available()>0) { ch = Serial.peek(); if((ch == 'e')||(ch == 'E')) { logfile.flush(); Serial.print("Data Taking Completed"); while(1); } z = Serial.parseInt(); if(Serial.read() == '\n') { Serial.print("z = "); Serial.print(z); Serial.print(" "); for (int i = 0; i < numSamples; i ++) { analogRead(photocellPin); delay(10); // a 10ms delay photocellReading = photocellReading + analogRead(photocellPin); } photocellReading = photocellReading/numSamples; Serial.print("Det. Avg. = "); Serial.print(photocellReading); Serial.println(); logfile.print(z); logfile.print(", "); logfile.print(photocellReading); logfile.println(); // adds a carriage return ('new line') to the data stream } Serial.println("Enter new z-value:"); } }