//Name: Albert Madakson
//AE598 PROJECT Simulation with SD Card PROJECT
//Date: 12/07/2022
#include <SD.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(6, 5, 4, 9, 8, 7);
#define CS_PIN 10
const float BETA = 3950; // should match the Beta Coefficient of the thermistor
File root;
void setup() {
Serial.begin(115200);
lcd.begin(16, 2);
Serial.print("Initializing SD card... ");
if (!SD.begin(CS_PIN)) {
Serial.println("Card initialization failed!");
lcd.print("Card initialization failed!");
while (true);
}
Serial.println("Initialization Done.");
Serial.println("");
Serial.println("Reading Temperature from Sensor...");
int analogValue = analogRead(A0);
float celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" ℃");
//Print on LCD first row
lcd.print("Temperature: ");
//Print on LCD second row
lcd.setCursor(0, 1);
lcd.print(celsius);
lcd.print(" deg. C");
delay(1000);
// Creating New File in Card and Writing Temperature
File textFile = SD.open("PROJECT.txt", FILE_WRITE);
if (textFile){
textFile.print(celsius);
textFile.println(" ℃");
Serial.println("");
Serial.println("File Creation & Writing Complete");
textFile.close();
} else {
Serial.println("error opening file to write!");
}
Serial.println("Files Now Available in the Card:");
root = SD.open("/");
printDirectory(root, 0);
Serial.println("");
// Reading Data on New File in Card
textFile = SD.open("PROJECT.txt");
if (textFile){
Serial.println("Opening PROJECT.txt... ");
Serial.print("PROJECT.txt: Temperature Saved in Card is ");
while (textFile.available()) {
Serial.write(textFile.read());
}
textFile.close();
} else {
Serial.println("error opening file for reading!");
}
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}