// Code developed by Dr Kris Chan & Dr Bruce Main, King's College London 20.10.2018 // ////////////////////////////////////////////////////////////////////////////////////// /* LIBRARIES */ #include //SPI communication library (for clock & SD card) #include //SD library. Need this library? https://github.com/greiman/SdFat /* CONSTANTS */ int count = 0; unsigned int wADC; //Arduino internal temperature call int CS=10; //SD HOUSEKEEPING SdFat sd; SdFile file; char newfile[] = "MyFileName.csv"; //name of file /* SETUP */ void setup() { Serial.begin(9600); Serial.println("setup start"); while (!sd.begin(CS, SPI_HALF_SPEED)) {} //SD card housekeeping file.open(newfile, O_WRITE | O_CREAT | O_APPEND); file.close(); Serial.println("setup end"); delay(100); } /* LOOP */ void loop() { Serial.println("loop"); delay(1000); count++; GetTemp(); //data written to SD: String dataString = ""; dataString += count; dataString += ","; dataString += wADC; Serial.println(dataString); Serial.println(""); delay(50); while (!sd.begin(CS,SPI_HALF_SPEED)) { } // initialises SD card again - for when sd card is removed for data file.open(newfile, O_WRITE | O_APPEND); //Opens the file delay(5); file.println(dataString); //prints data string to the file delay(5); file.close(); //closes the file delay(20); } /* GET PROCESSOR TEMPERATURE SUBROUTINE */ double GetTemp(void) { ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3)); ADCSRA |= _BV(ADEN); delay(20); ADCSRA |= _BV(ADSC); while (bit_is_set(ADCSRA,ADSC)); wADC = ADCW; }