// Melissa Wells
// AE598
// Arduino Simulation
// 30NOV22
// This project takes a temperature sensor, reads the temperature and writes to
//the SD card the analog reading. It also will output the temperature reading in
//the output display for as long as the simulation is left running.
//Temperature was set for 25 deg C for what the sensor is monitoring environmentally
const float BETA = 3950;
#include <SD.h>
#define CS_PIN 10
File stuff;
int tempPin = A0;
int temp;
void setup() {
Serial.begin(9600);
Serial.print("SD Card Initializing...");
if (!SD.begin(CS_PIN)) {
Serial.println("Card Failed");
while(true);
}
Serial.println("Initialization Complete");
Serial.println("Files on card");
stuff = SD.open("/");
printDirectory(stuff,0);
Serial.println("");
// Writing Data to the Card for Output
temp = analogRead(A0);
float celsius1 = 1 / (log(1/ (1023. / temp - 1)) / BETA +1.0 / 298.15)-273.15;
File textFile = SD.open("/temp.txt", FILE_WRITE);
if (textFile){
textFile.println("Testing whether things write");
Serial.println("Writing Data Test Complete - Temperature Reading in Degrees C: ");
Serial.println(celsius1);
Serial.println();
textFile.close();
}
else {
Serial.println("Error Writing File");
}
}
// Running Loop for Constant Display of Temperature
void loop() {
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(" deg C");
delay(1000);
//Base coding for temperature sensor modified from reference [1], [2]
//Base coding for SDcard modified from references [3] and [4] and class lecture notes.
}
// Reading Data Off Card
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
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 {
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
// References
// [1] "Wokwi-NTC-Temperature-Sensor Reference," Wokwi, 2021. <https://docs.wokwi.com/parts/wokwi-ntc-temperature-sensor>
// [2] "Adafruit-ntc.ino," Wokwi, 2021. <https://wokwi.com/projects/299330254810382858>
// [3] "Wokwi-MicroSD-Card Reference," Wokwi, 2021. <https://docs.wokwi.com/parts/wokwi-microsd-card>
// [4] "Microsd-card-prototype-test.ino," Wokwi, 2021. <https://wokwi.com/projects/310542489623724609>