A-Star 328PB Saving on EEPROM

Hey, I am trying to save values on the eeprom of my a-star 328pb microcontroller, so if the power dies then it will recall the position value of my motor. I am following the same format as I would usually do it with an arduino. Below is the code I am using (I have completely broken it down just to get this section working, but it doesn’t seme to work). I am not sure why it isn’t working.

When I run the below code I get back in the serial
“Value: nan”

#include <EEPROM.h>

void setup() {
  // put your setup code here, to run once:
  EEPROM.put(1, 10);

  Serial.begin(9600);

  delay(1000);

  Serial.print("Value: ");
  float val = 0;
  EEPROM.get(1, val);
  Serial.println(val);
}

void loop() {
  // put your main code here, to run repeatedly:

}

Hello.

I suspect the problem is just a type mismatch. If you want to read the value into a float, you might try changing EEPROM.put(1, 10); to EEPROM.put(1, 10.0); or EEPROM.put(1, (float)10);.

Alternatively, defining val as an int instead of a float should work, too.

Brandon

That did the job, thanks so much Brandon. I’m glad it was something simple!

1 Like