Skip to content

Instantly share code, notes, and snippets.

@dwblair
Created May 25, 2014 04:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dwblair/669a991062f7f53dadbf to your computer and use it in GitHub Desktop.
/*
* Append Example
*
* This sketch shows how to use open for append.
* The sketch will append 100 line each time it opens the file.
* The sketch will open and close the file 100 times.
*/
#include <SdFat.h>
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures
// SD chip select pin
const uint8_t chipSelect = SS;
// file system object
SdFat sd;
// create Serial stream
ArduinoOutStream cout(Serial);
// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
char name[] = "APPEND1.TXT";
//------------------------------------------------------------------------------
void setup() {
// filename for this example
Serial.begin(9600);
while (!Serial) {} // wait for Leonardo
// pstr() stores strings in flash to save RAM
cout << endl << pstr("Type any character to start\n");
while (Serial.read() <= 0) {}
delay(400); // Catch Due reset problem
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
}
//------------------------------------------------------------------------------
void loop() {
int reading = analogRead(sensorPin);
// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = reading * 5.0;
voltage /= 1024.0;
// print out the voltage
Serial.print(voltage); Serial.println(" volts");
// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((voltage - 500mV) times 100)
Serial.print(temperatureC); Serial.println(" degrees C");
//char name[] = "APPEND.TXT";
ofstream sdout(name, ios::out | ios::app);
if (!sdout) error("open failed");
sdout << millis() << "," << temperatureC << endl;
sdout.close();
if (!sdout) error("append data failed");
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment