Pages: [1]   Go Down
Author Topic: How to Save Configuration  (Read 2952 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley

Code:
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  :'(
Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 81
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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()?
Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 81
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 http://avr-libc.nongnu.org/user-manual/group__avr__eeprom.html


Logged

Central Europe
Offline Offline
Edison Member
*
Karma: 7
Posts: 1220
Use the Source, Luke.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

how to use it with Float ?
Logged

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 81
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
#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(){
}
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :
Code:
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?
« Last Edit: November 14, 2010, 10:34:24 am by the_omicron » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

EEPROM.write(0,byte(ShiftStore));
« Last Edit: November 14, 2010, 10:46:45 am by spatry » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yay, I did it with

Code:
#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
« Last Edit: November 14, 2010, 09:15:11 pm by the_omicron » Logged

Seattle
Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
Haxorz? In MY Computerz? Its liklyr than you think
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

[code]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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 303
Posts: 26354
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: