Go Down

Topic: Question for EEPROM_readAnything/EEPROM_writeAnything. (Read 1 time) previous topic - next topic

I have the following simple program.

The problem is with var1++. It doesn't increase the value of var1. It always stays 0.00. Actually I can't perform any operation with var1 as rValue. I I put something like var1=3.14 the value changes and gets stored.

What am i missing?

Code: [Select]
#include <EEPROM.h>
#include "ee.h"

#define sw_0 A5

float var1;
float a = 0.0;

void read_c(){
  EEPROM_readAnything(4, var1);
}

void write_c(){
  EEPROM_writeAnything(4, var1); 
}

void setup() {
  read_c();
  Serial.begin(9600);
  pinMode(sw_0, INPUT);
}

void loop(){
 
  if (digitalRead(sw_0) == LOW) {
    var1++;
    delay(100);
    write_c();
  }
 
  Serial.print(millis()/1000);
  Serial.print(" ");
  Serial.println(var1);
  delay(1000);
}


ee.h:
Code: [Select]
#include <WProgram.h>

template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
  EEPROM.write(ee++, *p++);
    return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    int i;
    for (i = 0; i < sizeof(value); i++)
  *p++ = EEPROM.read(ee++);
    return i;
}

PaulS

Quote
What am i missing?

A description of how the switch is connected.

With no other pins in use, why are you using analog pin 5 for the switch? You can. There is nothing wrong with doing so, as long as you are actually connecting the switch to analog pin 5, and using an external pull-up resistor.


What evidence do you have that the switch is being read correctly?

If I change float var1 to int var1 everything works as expected.

I forgot to mention that I am using an ATMEGA8 with an external programmer and avrdude.

PaulS

With the variable as a float, what does
var1 += 1.492;
do?

No, nothing. It doesn't work. Regardless of the operation (var1=var1 +-* ...) the eeprom is ff ff c0 ff.

If I reinitialize var1 after read_c() e.g.

Code: [Select]
read_c();
var1=0.0;


It works. I can see with eeprom dump the bytes. As obvious this is not usable. I need the saved value.

I also tried Arduino version 0021, 0019, 0017. Same behavior. It only works for int and long.

I am getting somewhere...

I have noticed that if you pass a variable by reference to a function and assign 0xFF to its bytes hapens exactly the same. After programing eeprom is 0xFF.

If I zero out the bytes I use for storing everithing looks fine.

Code: [Select]

void setup() {
  int i;
//using the first byte as an indicator for the first run after programming.
  if (EEPROM.read(0) == 0xff) {
  EEPROM.write(0,0x00);
  for (i=0; i<20; i++) {  //i could be up to 512
    EEPROM.write(i,0x00);
  }
  }
 
  read_c();
  Serial.begin(9600);
  pinMode(sw_0, INPUT);
}
  }

Go Up