I have built a simple temperature sensor with a TMP36 connected to Analog 0. and a Ready LED (Red) on Pin 2 and a Start LED (Green) On pin 8. At the beginning of the program, it writes a simple text string to the SD in a file called Test.txt. In the loop, it is supposed to write the sample number and the temperature to the SD in the same file (for now) but it wont do that, it stops after it writes the test to the SD and then it stays there. It is getting really frustrating becuase i know the SD card is formatted correctly and its all hooked up right. Any suggestions? Thanks.
Code:
#include <toneAC.h>
#include <SPI.h>
#include <SD.h>
int readyLED = 4;
int startLED = 8;
int buzzer = 7;
int tempSensor = A0;
int readyButton = 5;
int sampleNo = 0;
int button_mode = 1;
File myFile;
void setup() {
// put your setup code here, to run once:
pinMode(readyLED, OUTPUT);
pinMode(startLED, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(10, OUTPUT);
pinMode(tempSensor, INPUT);
pinMode(readyButton, INPUT);
digitalWrite(readyLED, HIGH);
digitalWrite(startLED, LOW);
Serial.begin(9600);
while (!Serial){}
Serial.println("Initializing SD card...");
if(!SD.begin(4)){
Serial.println("Failed!");
return;
}
Serial.println("Success!");
myFile = SD.open("test.txt", FILE_WRITE);
if (myFile) {
Serial.println("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
delay(500);
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
delay(1000);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(startLED, LOW);
digitalWrite(readyLED, LOW);
delay(650);
digitalWrite(startLED, HIGH);
digitalWrite(readyLED, LOW);
delay(650);
int reading = analogRead(tempSensor);
float voltage = reading * 5.0;
voltage /= 1024.0;
float temperatureC = (voltage - 0.5) * 100;
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print("Sample No. ");
sampleNo = sampleNo + 1;
Serial.print(sampleNo);
Serial.print(" Temperature: ");
Serial.print(temperatureF);
Serial.println(" F");
myFile = SD.open("test.txt");
if(myFile){
Serial.println("Test.txt");
}
while(myFile.available()){
myFile.print("Sample No. ");
myFile.print(sampleNo);
myFile.print(" Temperature: ");
myFile.print(temperatureF);
myFile.println(" F");
}
myFile.close();
}
the Serial output at the time of freezing
Initializing SD card...
Success!
Writing to test.txt...
done.
Sample No. 1 Temperature: 72.08 F
Test.txt
The Files on the SD after running:
TEST.txt
And on that file is:
testing 1, 2, 3.