Code to store data in EEPROM with Xbee

Hey guys!

I have an Uno (with an Xbee on it) reading analog values and am wanting them to be stored in EEPROM until I plug in my Xbee USB on the other end to get the data unloaded from EEPROM and printed in my serial port. The code that I think should do the trick is not uploading properly…? Anyone see issues with it?

/*
Program reads voltage from pin A0 and stores it in EEPROM until asked to "unload" it in serial
*/

#include <EEPROM.h>

int addr = 0;

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() {
  int sensorValue = analogRead(A0)/4; //divide by 4 cause analog inputs rage from 0-1023 and EEPROM can hold only 0-255
  EEPROM.write(addr, sensorValue);
  addr = addr + 1;
  
  if(addr == 512) {          //incase the data isn't unloaded before EEPROM is full turn on LED and wait 24hrs
    digitalWrite(13, HIGH);
    delay(86400000);
  } 
  //If serial available read EEPROM values then clear EEPROM
  if (Serial.available()){
    while(addr =! 0) {
      EEPROM.read(addr);
      Serial.print("Getting values from EEPROM...");
      Serial.print(addr);
      Serial.print("/t");
      Serial.print(sensorValue, DEC);  
      Serial.println();  
      addr = addr + 1;
    }
    for(addr = 0; addr <= 512; addr++) {
      EEPROM.write(addr, 0);
    }
  }
}

Thank you!
McK

Anyone see issues with it?

We could play guessing games, or you could tell us what problem(s) you had uploading it.

You dont set addr back to 0 before you start sending it back over serial.

So, you could just use a for loop instead of of the while.

Incidentally in 'while' you use '=!' rather than '!='.

You could save at higher resolution if you wanted by packing the int into two bytes. You might also be able to just store the deltas, or compress the range if you are measuring something that changes slowly.

There are examples of this kind of thing in this forum and in my new book 'Programming Arduino: Getting started with sketches'. http://www.arduinobook.com

You could save at higher resolution if you wanted by packing the int into two bytes.

But, you could only save half as often. Not that that really matters, since EEPROM is going to fill up for you in a matter of microseconds anyway.

I think Paul is hinting that you need to put a delay in there to slow down your sampling - what are you trying to do exactly?

So when it it reads values back over EEPROM it is suppose to unload starting at addr = 0. no? Then it should read input values from zero to 512. Is that why when I said while addr != zero it doesnt make sense because it has to start at zero?

/*
Program reads voltage from pin A0 and stores it in EEPROM until asked to "unload" it in serial
*/

#include <EEPROM.h>

int addr = 0;

void setup() {
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() {
  int sensorValue = analogRead(A0)/4; //divide by 4 cause analog inputs rage from 0-1023 and EEPROM can hold only 0-255
  EEPROM.write(addr, sensorValue);
  addr = addr + 1;
  
  if(addr == 512) {          //incase the data isn't unloaded before EEPROM is full turn on LED and wait 24hrs
    digitalWrite(13, HIGH);
    delay(86400000);
  } 
  //If serial available read EEPROM values then clear EEPROM
  if (Serial.available()){
    Serial.print("Getting values from EEPROM...");
    for(addr = 0; addr <= 512; addr++) {
      EEPROM.read(addr);
      Serial.print(addr);
      Serial.print("/t");
      Serial.print(sensorValue, DEC);  
      Serial.println();  
      addr = addr + 1;
      delay(10000); //read values once every 10seconds
    }
    for(addr = 0; addr <= 512; addr++) {
      EEPROM.write(addr, 0);
    }
  }
}

for(addr = 0; addr <= 512; addr++) {
EEPROM.read(addr);
Serial.print(addr);
Serial.print("/t");
Serial.print(sensorValue, DEC);
Serial.println();
addr = addr + 1;
delay(10000); //read values once every 10seconds
}

Why are you incrementing addr inside the loop, too?

The 512 places in EEPROM are numbered 0 to 511. You should lose the = after the <.