I'm new to saving data on eeprom and I'm obviously doing something wrong but I'm not sure what. I have one program which both writes to and reads the internal eeprom on a attiny85 and this seems to work. However when I try to use this program to write and then remove the power from the attiny, then reprogram it with another program to read the eeprom it gives values of -1 for time and NAN for temperature.
My program which works is as follows, the program to read the eeprom only is the same however I deleted all the sections which write. If you want me to add that program let me know.
I am using at attinyavr programmer from sparkfun and I have connected pins 3 & 4 on the attiny to an arduino uno which is connected to the serial monitor so that I can see the output (described here: http://www.instructables.com/id/Easy-ATTiny-Serial-Communication-with-Tiny-AVR-Pro/#CP78RMAJ76GI6JQ)
Feel free to direct me to another answer if someone else has had the same problem, at the moment I think I will figure this out eventually but I suspect it will take me a few days so thought I would ask hopefully it's obvious to someone else.
(I'm aware that the time won't be correct since it should be unsigned long not an int, was just changing it round to test if that was the problem which it seems not to be).
Any help is really appreciated!!! Thanks :) /*
ATTINY85 pin 3 -> Arduino Uno pin 0
ATTINY85 pin 4 -> Arduino Uno pin 1
Program choose 1MHz clock
*/
#include <EEPROM.h>
#include <SoftwareSerial.h>
// Definitions
#define rxPin 3 // Make pin 3 on the
#define txPin 4
SoftwareSerial mySerial(rxPin, txPin);
int currentTime;
int getTime;
int sensorPin = 7; // This is the 7th pin on the ATtiny85,
// it is an analog input when reading as digital
call it 7? analogpintochannel function
// (On the HLT tutorial it is labelled as Pin2: Analog input 1, SCK )
float temperature = -500.00f; // Create a new float called temperature and give it the value -500 to start with
float getTemperature = -500.00f;
int eepromAddress = 0; // initialize variable for position in eeprom to store data collected
int getEepromAddress = 0;
// the setup routine runs once when you press reset:
void setup() {
pinMode(sensorPin, INPUT); // make pin 7 on the ATTiny an input pin
mySerial.begin(9600); // begin software serial at rate of 9600
}
// the loop routine runs over and over forever:
void loop() {
static int countDataCollected = 0; // initialize a count variable to count the data collected, static int should set to 0 only first time, second time loop goes round it shouldn't reset to 0
if (countDataCollected==0) delay(90000); // put delay here so it gives me time to load the read eeprom program if required before it changes the eeprom in case that was the problem.
if((countDataCollected>=0) and (countDataCollected < 10)) {
currentTime = millis();
temperature = analogRead(sensorPin)*5.0/1024.0; // read value on pin 7, multiply it to find temperature
temperature = temperature - 0.5;
temperature = temperature / 0.01;
mySerial.print("\n\nData number: ");
mySerial.print(countDataCollected);
mySerial.print("\tTime: ");
mySerial.print(currentTime);
mySerial.print("\tTemperature: ");
mySerial.print(temperature);
mySerial.print("\tCurrent eeprom address: ");
mySerial.print(eepromAddress);
mySerial.print("\t");
mySerial.print(getEepromAddress);
EEPROM.put(eepromAddress, currentTime);
EEPROM.get(getEepromAddress, getTime);
mySerial.print("\tGot time: ");
mySerial.print(getTime);
eepromAddress = eepromAddress + sizeof(currentTime);
getEepromAddress = getEepromAddress + sizeof(getTime);
mySerial.print("\teeprom address: ");
mySerial.print(eepromAddress);
mySerial.print("\t");
mySerial.print(getEepromAddress);
EEPROM.put(eepromAddress, temperature);
EEPROM.get(getEepromAddress, getTemperature);
eepromAddress = eepromAddress + sizeof(temperature); //Move address to the next byte after float 'myfloat'.
getEepromAddress = getEepromAddress + sizeof(getTemperature);
mySerial.print("\tGot temperature: ");
mySerial.print(getTemperature);
mySerial.print("\teeprom address: ");
mySerial.print(eepromAddress);
mySerial.print("\t");
mySerial.print(getEepromAddress);
mySerial.println();
}
// wait two seconds so as not to send massive amounts of data
delay(2000);
countDataCollected=countDataCollected+1;
mySerial.print("\n\nNext data number =");
mySerial.print(countDataCollected);
}