How to Save Configuration

Hello, I am a newbie at Arduino, and this is my first project.

I am trying to make an adjustable shiftlight for my bike, but everytime I turn it off the setting's gone.

What should I do to be able to save the shift RPM setting?

this is my setting code, any advice would be appreciated :)

while(k!=0)
{
if(bouncer.read()==HIGH)
                  {
                        Shift = Shift+100;
                        lcd.print(Shift);
                        lcd.setCursor(12,0);
                        lcd.print("RPM");
                        lcd.setCursor(4,1);
                        lcd.print("Set Shift");
                        delay(1500);
                        digitalWrite(ShiftUpPin, LOW);
                        digitalWrite(ChangeModePin, LOW);
                        
                  }
else if(bouncer2.read()==HIGH)
                  {
                        Shift = Shift-100;
                        lcd.print(Shift);
                        lcd.setCursor(12,0);
                        lcd.print("RPM");
                        lcd.setCursor(4,1);
                        lcd.print("Set Shift");
                        delay(1500);
                        digitalWrite(ShiftDownPin, LOW);
                        digitalWrite(ChangeModePin, LOW);
                  
                  }
else if(bouncer3.read()==HIGH)
                  {
                        lcd.print("Saved");
                        delay(1000);
                        digitalWrite(ChangeModePin, LOW);
                        k=0;
                  }
}

NB: Sorry for my bad English, I'm not fluent in English :'(

Use the internal EEPROM of Arduino to store the your settings. Example in the EEPROM library helps.

Use the internal EEPROM of Arduino to store the your settings. Example in the EEPROM library helps.


Thank you for your response, but I still didn't understand how it works.

Lets say, I want to save the Shift variable value as 7000, how do I save it with EEPROM.write()?

You are right, Arduino lib only allows to read&write single byte.
Use directly the avr/eeprom.h

#include <avr/eeprom.h>

eeprom_write_word ( addres, value );
value = rom_read_word ( address );

And the avr eeprom lib details avr-libc: <avr/eeprom.h>: EEPROM handling

Just remember a few things before you go off an go all EEPROM-crazy.

First, your EEPROM is rated for 100000 write cycles. So don't write to it every time loop is called or it will go bust in a few minutes. The proper way to do is to write to the EEPROM only when the relevant value change or every hour or so.

Second, when you read the EEPROM at start-up, expect it to have the wrong value from your previous experiments with EEPROMs. That memory isn't initialised. Always check at the first reading if the values read are safe and inside expected parameters, if not ignore them and use default values. After the next writing, that problem will be solved.

Korman

is avr/eeprom.h library included in the Arduino IDE 0021 or should I download it?

how to use it with Float ?

Yes avr/eeprom library included in Arduino environment. Use the eeprom block read&write.

#include <avr/eeprom.h>

typedef struct
{
  float value1;
  float value2;
  float value3;
  float value4;
}  settingsType;

settingsType settings = {700.3,123.56, 650.5, 100};


void setup(){
  Serial.begin(9600);
  
  eeprom_write_block((const void*)&settings, (void*)0, sizeof(settingsType));
  
  eeprom_read_block((void*)&settings, (void*)0, sizeof(settingsType));
  
  Serial.print(settings.value1);
}

void loop(){
}

wow… this is getting harder for my “noobility” (hey, a new word) to follow… :-[

If you don’t mind, could you give me an easier explanation by… giving example on how to save float Shift = 7000.0 ?

EDITED:

I have an idea, how about :

ShiftStore = Shift/100;

//then I store the value to the EEPROM which is 0-255

EEPROM.write(0,ShiftStore);

//and then to read it

ShiftStore=EEPROM.read(0);
Shift=ShiftStore*100;

will it work?

You'll need to cast as a byte for that to work, but you're on the right track.

EEPROM.write(0,byte(ShiftStore));

yay, I did it with

#include <EEPROM.h>

float ShiftStore;
float Shift;

void setup()
{
  ShiftStore=float(EEPROM.read(0)); //read stored byte as float
  Shift=ShiftStore*100;
}

void loop()
{
//to save
if(bouncer3.read()==HIGH)
                  {
                     ShiftStore=Shift/100; //to divide so it could be saved as value between 0-255
                        EEPROM.write(0,byte(ShiftStore)); //to save the float as byte
                        lcd.print("Saved");
                        delay(1000);
                        digitalWrite(ChangeModePin, LOW);
                        k=0;
                  }
}

I will reporting back as soon as I’ve uploaded it to my board

First, your EEPROM is rated for 100000 write cycles. So don't write to it every time loop is called or it will go bust in a few minutes./code]
Just putting this out there:
I always use I2C EEPROM for this kind of thing. You dont have to worry about burning out your onboard eeprom, and is an I2C bus so you can put multiple of them on a single bus.

I always use I2C EEPROM for this kind of thing. You dont have to worry about burning out your onboard eeprom

The only advantages here are:

  1. I2C EEPROMs have 10 times longer life (it’ll take a bit longer to wear them out) and
  2. you can (sometimes) socket them, so they’ll be easier to swap out when they do reach end of life.

The correct approach, as Korman pointed out, is to only write to EEPROM if the value has changed.