Skip to content

Commit

Permalink
Create Lab3-SD.ino
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamenm03 authored Jan 15, 2024
1 parent e28dc79 commit 703b4f9
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions examples/Lab3-SD/Lab3-SD.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// These are the pins your MicroSD Card Adapter will be connected to.
// These pins are specific and should not change. You do not need to worry
// about why these are the pins, just connect them as listed.
// MOSI - pin 11
// MISO - pin 12
// CLK - pin 13
// CS - pin 10
const int chipSelect = 10;

// This is the pin your TMP36 is connected to.
// Change this as needed.
const int tmpPin = A1;

// This is the pin your voltage divider is connected to.
// Change this as needed.
const int vDivPin = A0;

// For future labs, you may find it helpful to copy the above settings for additional sensors.
// **HINT HINT WINK WINK**

// This is the string that goes at the top of your csv file. It is the column headers for your spreadsheet.
// You can change this as needed.
const String header = "Time (ms),TMP36 (Raw),Voltage (Raw)";

#include <SPI.h>
#include <SD.h>

void setup() {
Serial.begin(9600);

delay(100); // arbitrary delay to let the serial monitor start up
Serial.print("Initializing SD card...");

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
while (1);
}
Serial.println("card initialized.");

// now, before we actually start reading data, we need to write the header to the file.
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.csv", FILE_WRITE);
if (dataFile) {
dataFile.println(header);
dataFile.close();
} else {
Serial.println("error opening datalog.csv");
}
}

void loop() {

// Note: in future labs, you may need to change this to add additional sensors.

int tmpVal = analogRead(tmpPin);
int vDivVal = analogRead(vDivPin);

// Now let's make a nice string to write to the file.
// This is a comma-separated value (csv) file, so we need to separate each value with a comma.
String dataString = "";
// add the time (since boot) in milliseconds
dataString += String(millis());
dataString += ",";
// add the raw TMP36 value
dataString += String(tmpVal);
dataString += ",";
// add the raw voltage divider value
dataString += String(vDivVal);

// now let's open the file again
File dataFile = SD.open("datalog.csv", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();

// print to the serial port too:
// Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}

delay(500);
}

0 comments on commit 703b4f9

Please sign in to comment.