Thanks for all the replies. For those who wish to look at the complete code, here it is below:
The problem comes in when declaring the temperature value as int in this formula: temperature = (temperature*500)/1023;
indicated in the code below with // <<----- (comment) behind
/* Reading temperature using LM35 */
//Include LCD library
#include <LiquidCrystal.h>
#include <SD.h>
#include <SPI.h>
File data_file;
int LED = 13;
int lm35_pin = A0;
float temperature; // <<----- If set to int, the temp readings go 29, 30, -31, -30, -29…0 1 2… when exposed to high temperatures
int chip_select_pin = 10;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(9, 8, 5, 4, 3, 2);
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
pinMode(lm35_pin, INPUT);
pinMode(chip_select_pin, OUTPUT);
if (SD.begin())
{
Serial.println(“Initialization Successful. Ready to use”);
} else
{
Serial.println(“Initialization failed. Check your pin connections or change your SD card”);
return;
}
}
unsigned long StartTime = millis();
void loop() {
digitalWrite(LED, HIGH);
unsigned long CurrentTime = millis();
unsigned long ElapsedTime = CurrentTime - StartTime;
if(ElapsedTime > 500){ // read the value every __ milliseconds to mimmic a delay before refreshing the display
temperature = analogRead(lm35_pin);
temperature = (temperature*500)/1023; // <<----- This is the formula that does not work using integers.
data_file = SD.open(“Temp.txt”, FILE_WRITE);
if (data_file) {
Serial.print(CurrentTime);
data_file.print(CurrentTime);
Serial.print(",");
data_file.print(",");
Serial.println(temperature,1);
data_file.println(temperature,1);
data_file.close();
}
else {
Serial.println(“error opening your SD card file. Try again”);
}
//int v1 = analogRead(LDR1);
//int v2 = analogRead(LDR2);
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(“millis:”);
lcd.setCursor(8, 0);
lcd.print(CurrentTime);
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
lcd.print(“Temp:”);
//Print a message to second line of LCD
lcd.setCursor(8, 1);
lcd.print(temperature,0t);
lcd.setCursor(14, 1);
lcd.print(“C”);
StartTime = CurrentTime;
}
}