User update new value into eeprom

Hi, pardon me if my question sounded dumb or anything. Im pretty new in arduino and i really hope anyone would help me on this one.

Im trying to let user insert new value into eeprom. This is a simple coding i think but i keep getting error messages.

Can someone please help me. Thank you :smiley:

#include <EEPROM.h>
float newval;
int address =0;
void setup(){
  Serial.begin(9600);
  Serial.print("Enter new value:");
  newval=Serial.read();
  EEPROM.update(address,newval);
  float a=EEPROM.get(address);
  Serial.print(a);
}
 void loop(){}

Think about the data types involved.


After it compiles, you will notice that Serial.read() won't wait for the user (luckily), but return a (int) -1, meaning "nothing to read".

  newval=Serial.read();

Serial.read() returns 1 byte of useful data, in the low order byte of an int. Storing that one byte/character in a float makes no sense.

michael_x:
Think about the data types involved.


After it compiles, you will notice that Serial.read() won't wait for the user (luckily), but return a (int) -1, me

PaulS:

  newval=Serial.read();

Serial.read() returns 1 byte of useful data, in the low order byte of an int. Storing that one byte/character in a float makes no sense.

Pardon me as i am new with arduino. I am trying to enter the new value in floating point..can you help me fix the code?

I am trying to enter the new value in floating point..can you help me fix the code?

There are two ways to fix the code.

One is to collect the data in an array, as each character arrives. When some end-of-packet marker (the carriage return or line feed that the Serial Monitor can send are good examples) arrives, instead of storing it, pass the array to atof() to convert to a float. Be sure to NULL terminate the array after each addition.

The other is to use the lazy way, and call Serial.parseFloat() instead of Serial.read().