Monday, June 21, 2010

Task 51 - 59: EEPROM

The Arduino contains a small amount of storage called EEPROM, this storage is available for the program to store and retrieve data.

below is my program to read tempeture and save it to the EEPROM then read it when needed.

#include <math.h>
#include <EEPROM.h>

#define DELAY (2 * 60) //(mins * seconds)

int value = 0;

double Thermister(int RawADC) {
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 * Temp) + (0.0000000876741 * Temp * Temp * Temp));
Temp = Temp - 273.15; // Convert Kelvin to Celcius
return Temp;
}

void delaySeconds(int delaytime = 0) {
for(int i = 0; i <= delaytime; i++){
delay(1000);
}
}

void setup() {
Serial.begin(9600);
Serial.println("Would you like to record temperatures? (y/N)");
do {;} while(Serial.available() <= 0); // Check for iput from serial
if((char)Serial.read() == (char)'y')
{
for(int i = 0; i < 512; i++) {
Serial.print("Reading temperatures, please wait : #");
Serial.println(i);
EEPROM.write(i, int(Thermister(analogRead(0))));
delaySeconds(DELAY);
}
}
for(int i = 0; i < 512; i++) {
value = EEPROM.read(i);

Serial.print(i);
Serial.print("\t");
Serial.print(value);
Serial.println();
}
}

void loop() {
}