I am logging temperature data on an SD Card. I want to have the arduino write the temperature plus a number that represent the iteration number of the loop so:
0,74
1,74
2,74
3,74
4,74
5,74
6,74
7,74
8,74
9,74
10,74
11,74
0,74
1,74
2,74
3,74
4,74
5,74
6,74
7,74
8,74
9,74
10,74
11,74
You can see though that it iterates through the loop 12 times and then resets and starts over. It will do this indefinately. I thought perhaps I was exceeding RAM so I commented out a few serial.print lines but it still does it. If I remove the count variable and just write the temperature to the SD card it doesn't reset, or if I just run it without an SD Card it doesn't reset so it is definately something to do with count variable. Any ideas? Here is the total code:
/*
* BBQControl.c
*
* Created: 8/7/2011 12:34:32 PM
* Author: Saleem and Leslie
*/
#define LCD_Command_A 0x7C
#define LCD_Command_B 0xFE
#define LCD_Clear_Screen 0x01
#define LCD_Cursor_Position(a) {Serial.print(LCD_Command_B,BYTE); Serial.print(a+128,BYTE);}
#define chipSelect 10
#include <math.h>
#include <stdint.h>
#include <SD.h>
boolean _sd_available = false;
void setup()
{
pinMode(A0,INPUT);
Serial.begin(9600);
initLCD();
//check for brownout
if(MCUSR & (1<<BORF)) {
LCD_Cursor_Position(84);
Serial.print("Brownout Reset ");
delay(2000);
}
//initialize SD card
LCD_Cursor_Position(84);
Serial.print("Initializing SD");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect))
{
LCD_Cursor_Position(84);
Serial.println("SD Failed ");
_sd_available = false;
}
else
{
LCD_Cursor_Position(84);
Serial.println("SD Active ");
_sd_available = true;
}
}
uint32_t count;
void loop()
{
uint16_t ADCVal;
uint16_t temperature;
String dataString = "";
ADCVal = analogRead(A0);
temperature = (get_temperature(ADCVal));
LCD_Cursor_Position(8);
Serial.print(temperature);
Serial.print(" ");
if (_sd_available)
{
dataString += String(count++);
dataString += ",";
dataString += String(temperature);
// 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("logger.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
// print to the serial port too:
//Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else
{
LCD_Cursor_Position(84);
Serial.println("error opening ");
}
}
delay(500);
}
void get_parameters(uint16_t ADCvalue, uint16_t *beta, float *r_infinity)
{
if (ADCvalue < 15) {*beta = 5191; *r_infinity=0.07028;}
else if (ADCvalue <= 39) {*beta = 5085; *r_infinity=0.08597;}
else if (ADCvalue <= 158) {*beta = 4942; *r_infinity=0.11637;}
else if (ADCvalue <= 530) {*beta = 4767; *r_infinity=0.17766;}
else if (ADCvalue <= 577) {*beta = 4671; *r_infinity=0.23249;}
else if (ADCvalue <= 670) {*beta = 4641; *r_infinity=0.25289;}
else if (ADCvalue <= 757) {*beta = 4604; *r_infinity=0.28168;}
else if (ADCvalue <= 832) {*beta = 4566; *r_infinity=0.31618;}
else if (ADCvalue <= 892) {*beta = 4526; *r_infinity=0.35779;}
else if (ADCvalue <= 938) {*beta = 4485; *r_infinity=0.40755;}
else if (ADCvalue <= 1023){*beta = 4453; *r_infinity=0.45243;}
return;
}
//*************Hardware SetUp***************
//
// ADC Input
// |
// Rpad | Rtherm
// Vcc--------/\/\/\------/\/\/\------ground
//
//******************************************
uint16_t get_temperature(uint16_t ADCVal){
//temperature in kelvin = beta/ln(R/Rinfinity)
uint16_t beta = 0;
float Ri = 0;
float Rpad = 100000;
float Rtherm = Rpad/(1-((float)ADCVal/1024.0)) - 100000;
float Temperature;
float t;
get_parameters(ADCVal, &beta, &Ri);
t=beta/log(Rtherm/Ri);
t = ((9.0/5.0)*(t-273))+32;
Temperature = t;
//Out to LCD
//LCD_Cursor_Position(72);
//Serial.print(ADCVal);
//Serial.print(" ");
//LCD_Cursor_Position(28);
//Serial.print(beta);
//Serial.print(" ");
//LCD_Cursor_Position(8);
return Temperature;
}
void initLCD(){
delay(1000); //wait for LCD to wake up
Serial.print(LCD_Command_B,BYTE);
Serial.print(LCD_Clear_Screen,BYTE); //clear screen
delay(20);
LCD_Cursor_Position(0);
Serial.print("Temp = ");
//LCD_Cursor_Position(64); //cursor move to second line
//Serial.print("ADC = ");
//LCD_Cursor_Position(20); //cursor move to third line
//Serial.print("Beta = ");
}