Skip to content

Commit

Permalink
lab3-SD update
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamenm03 committed Jan 30, 2024
1 parent 4f273ca commit 2245e0d
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
97 changes: 97 additions & 0 deletions examples/Lab3-SDtester/Lab3-SDtester.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// 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 string that goes at the top of your csv file. They are the column headers for your spreadsheet.
const String header = "Time (ms),Voltage (V),TMP36_1 (V),TMP36_2 (V),Pressure (V),Humidity (V),Accel_x (V),Accel_y (V),Accel_z (V)";

#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() {

double vDivVal = 0;
double tmp1Val = 1;
double tmp2Val = 2;
double pressVal = 3;
double humidVal = 4;
double accelxVal = 5;
double accelyVal = 6;
double accelzVal = 7;

// 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 adjusted voltage divider value
dataString += String(vDivAdj);
dataString += ",";
// add the first TMP36 value
dataString += String(tmp1Val);
dataString += ",";
// add the second TMP36 value
dataString += String(tmp2Val);
dataString += ",";
// add the pressure value
dataString += String(pressVal);
dataString += ",";
// add the humidity value
dataString += String(humidVal);
dataString += ",";
// add the x-accel value
dataString += String(accelxVal);
dataString += ",";
// add the y-accel value
dataString += String(accelyVal);
dataString += ",";
// add the z-accel value
dataString += String(accelzVal);

// 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();
}
// if the file isn't open, pop up an error:
else {
Serial.println("error opening datalog.txt");
}

// This line means we will be printing data to the SD card about once every 500 milliseconds, or half a second
delay(500);
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ENGR100-950",
"version": "1.0.9",
"version": "1.1.0",
"keywords": "Arduino,library",
"description": "UMich ENGR 100-950 Library",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ENGR100-950
version=1.0.9
version=1.1.0
author=Benjamen Miller <[email protected]>
maintainer=Benjamen Miller <[email protected]>
sentence=UMich ENGR 100-950 Library
Expand Down

0 comments on commit 2245e0d

Please sign in to comment.