5

Im building a arduino based bicycle speedometer. When the code starts the user picks his bike size (which I need to calculate the radius). Then I have a state finite machine where the user taps the button between speed, time and temperature. I need to put this circuit on a battery and it needs to be mounted on a bike. But every time you plug and unplug the battery, the code runs from the beginning again, which I don't want. So every time the user turns the speedometer off then on, he needs to pick his bike size again. Is it possible to save the size he has picked so that the next time he turns it on, it doesn't come across.

So - user has his speedometer, turns it on, picks the bike size, turns it off, then sometime later turns it back on and the part for picking the bike size doesn't show up, it simply "knows" remembers what the user picked last time.

I don't know how could that work, would the Arduino run the certain code only once or is there a function for "the next time Arduino has electricity behave like this".

1
  • 3
    use the EEPROM library
    – Juraj
    Commented Sep 10, 2020 at 9:03

1 Answer 1

6

As one of the comments said, you will need to use the EEPROM library. When you save a variable in your code (e.g. int test = 10;) the variable along with its value gets stored in the RAM. The EEPROM is a piece of harddisk on your arduino that will remember its value when you power off your arduino.

If you implement a power-off button, simply save the state (or in your case, the wheel size) in EEPROM whenever the user inputs it. The next time you power up, you can check if there is a previous value in EEPROM and continue with that value if it exists. Otherwise ask the user for their wheel size.

You might want to implement a reset-button as well, to reset whenever you change bike or wheels. Look up a code example of EEPROM, as there are a lot online.

4
  • Alright, thanks. I will look it up, this part "the next time you power up, you can check if there is a previsus value" - that is what I need. And yeah I will implement a reset button, good idea. Cheers man :) Commented Sep 10, 2020 at 15:09
  • @MarinFilipovic don’t forget to read eeprom, it will not be in the variable you stored previously
    – Ananas_hoi
    Commented Sep 10, 2020 at 15:11
  • ahaa okay, I get it, thanks Commented Sep 10, 2020 at 15:31
  • On boot/reset, read the configs from EEPROM. You can keep them in RAM except for when they change. It may help to store distance, calories, and everything else in there too. Don't forget to add some sort of magic number and version so that if it is fresh and not formatted (or you change the data structure) that you can format or convert it. Another possible thing you may want is a variation of wear-leveling, since EEPROM writes aren't free and it gradually wears out. It's good for a guaranteed 100,000 cycles, and usually much further, but if that won't be enough, wear-leveling is a good idea.
    – user47164
    Commented Sep 16, 2020 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.