Arduino multivalue problem

Hi guys,

I have a problem - I don't know how to save certain values. The best example would be the radio. You're searching for stations and when you find one - for ex. 95.5 you save the value 95.5 in EEPROM under the name RADIO1, then 98.2 under RADIO2 and so forth - in the EEPROM.

For this I need it with a potentiometer for start, and then I'll modify it.

Any suggestions? Thanks a lot!

Start here and here

Thanks for the links - I did read them and came across this code.

#include <avr/eeprom.h>

struct settings_t
{
  long alarm;
  int mode;
} settings;

void setup()
{
  eeprom_read_block((void*)&settings, (void*)0, sizeof(settings));
    // ...
}
void loop()
{
    // let the user adjust their alarm settings
    // let the user adjust their mode settings
    // ...

    // if they push the "Save" button, save their configuration
    if (digitalRead(13) == HIGH)
      eeprom_write_block((const void*)&settings, (void*)0, sizeof(settings));
}

But I really don’t understand what’s going on in here?

How can I manipulate for ex. the int mode variable to show me the value of the potentiometer?

mode = analogRead(A0)?

Thanks for the help.

settings.mode = analogRead(A0);

And then how do I read the saved values? I ran the program and the board rx button wasn’t blinking so I’m thinking maybe it didn’t save anything.

Here’s the code again :

#include <avr/eeprom.h>

struct settings_t
{
  long alarm;
  int mode;
} settings;

void setup()
{
  eeprom_read_block((void*)&settings, (void*)0, sizeof(settings));
    // ...
}
void loop()
{
    // let the user adjust their alarm settings
    // let the user adjust their mode settings
    // ...
    settings.mode = analogRead(A0);  // potentiometer on A0
    // if they push the "Save" button, save their configuration
    if (digitalRead(2) == HIGH)  // button attached on digital pin 2
      eeprom_write_block((const void*)&settings, (void*)0, sizeof(settings));
}

EDIT : I thought maybe because I didn’t declare the button it may have been causing problems but no, still doesn’t do anything.

#include <avr/eeprom.h>

const int buttonPin = 2;

int buttonState = 0;

struct settings_t
{
  long alarm;
  int mode;
} settings;

void setup()
{
  eeprom_read_block((void*)&settings, (void*)0, sizeof(settings));
    // ...
}
void loop()
{
    
    buttonState = digitalRead(buttonPin);  
  
    // let the user adjust their alarm settings
    // let the user adjust their mode settings
    // ...
    settings.mode = analogRead(A0);  // potentiometer on A0
    // if they push the "Save" button, save their configuration
    if (buttonState == HIGH)  // button attached on digital pin 2
      eeprom_write_block((const void*)&settings, (void*)0, sizeof(settings));
}
#include <EEPROM.h>

void setup()
{
  Serial.begin (115200);
  char* fox = "The quick brown fox";
  byte len = (byte) strlen (fox);
  EEPROM.write(0, len);
  for (int i = 0; i < len; i++)
  {
    EEPROM.write(1 + i, fox [i]);
  }
}

void loop()
{
  byte len = EEPROM.read (0);
  for (int i = 0; i < len; i++)
  {
    Serial.print ( (char)EEPROM.read(1 + i));
  }
  Serial.println ();
  delay (500);  
}

This involves a char variable, while I need only int variable, I see you take the lenght of the string, but how can I modify this for my purpose, with potentiometer?

More specifically

int mappedValue[] = { 1, 2, 3, 4, 5 }; int sensorVal[] = { A, B, C, D, E};

when A will be a value from the potentiometer when button 1 is pressed. Then value B also reads value from potentiometer when button 2 is pressed and so on.

This is saved in eeprom memory and later read.

I’m still stuck with this.

In case I’m not clear, as you can see I have a potentiometer on pin A0, button on pin 2, and when I press the button I need to save the potentiometer value in EEPROM memory under name VALUE1 for ex, then when I press another button again save the potentiometer value in eeprom memory under name VALUE2 and so on, and then read those saved values in serial window.

So far, with this code that I have nothing is showing up.

So this is what I have so far, anyone able to help me out ?

#include <avr/eeprom.h>
 
const int buttonPin = 2;
 
int buttonState = 0;
 
struct settings_t
{
  long alarm;
  int mode;
} settings;
 
void setup()
{
  pinMode(buttonPin, INPUT);
  eeprom_read_block((void*)&settings, (void*)0, sizeof(settings));
    // ...
}
void loop()
{
   
    buttonState = digitalRead(buttonPin);  
 
    // let the user adjust their alarm settings
    // let the user adjust their mode settings
    // ...
    settings.mode = analogRead(A0);  // potentiometer on A0
    // if they push the "Save" button, save their configuration
    if (buttonState == HIGH)  // button attached on digital pin 2
    {
      eeprom_write_block((const void*)&settings, (void*)0, sizeof(settings));
      eeprom_read_block((void*)&settings, (void*)0, sizeof(settings));
      Serial.println(settings.mode);
    }
}

Maybe you don't see anything because you don't have a Serial.begin.

Well that's without a doubt one of the worst mistakes I could make, really slipped out.

It works now.

But to store five values in five different variables, it's mapped values actually, for example the first variable i store 137, second 267, third 600 and so on, but only write the value once, how can i do this.

Can this be made using one button and using a for loop, or do I need to have 5 buttons?

Thanks.

I don't know - whatever works best, but it's best to write when the switch becomes pressed (or unpressed), not all the time it is pressed. Also, best to do a read of the EEPROM first then compare with what you're about to write so you don't write the same value, and limit the life of your EEPROM.

I don't know - whatever works best, but it's best to write when the switch becomes pressed (or unpressed), not all the time it is pressed.

You mean to have both buttonState - HIGH and LOW statements?

Also, best to do a read of the EEPROM first then compare with what you're about to write so you don't write the same value, and limit the life of your EEPROM.

In the buttonState function I tried putting the read instead of write block and it just outputs the first number I entered. Does this solve my issue with only one value to be written in the eeprom?

In the buttonState function I tried putting the read instead of write block and it just outputs the first number I entered.

You haven't posted a button state function, however, if you're reading back the value you wrote last, then that's a good sign, and if it is the same as the value you're about to write, then you don't need to bother - it's already there, which also means you don't have to worry so much about the switch transition problem I mentioned.

I meant to say statement - not function.

About the switch transition problem - I'll try to fix it and let you know.

Thank you very much for your help.

If you’re going to use the functions in <avr/eeprom.h>, which are better than the EEPROM.h stuff anyway, use the update functions, not the write function. The update functions will automatically read the value first, and only save it if it’s different, thus saving your eeprom from being written too many times.

http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html

Changing the write_block with update_block results in this error :

_1.ino: In function 'void loop()': _1:36: error: 'eeprom_update_block' was not declared in this scope

"eeprom_update_block" isn't in the "eeprom.h" that ships with the IDE (hardware/tools/avr/avr/include/avr/eeprom.h), but it's easy enough to cobble-together by looking at "eeprom_read_block" and "eeprom_write_block"

I have managed to save multiple values to EEPROM and read them. I added three buttons and saved the values to specific adresses and it works great.

But I do have another problem now. It is mapping the values and making an automatic scale.

So, I have three values - int zero, int ten, and int forty : which hold the saved values in the eeprom and are read.

So now I have this

int mappedValue[] = { 0, 10, 40 }; int sensorVal[] = { zero, ten, forty};

I have the zero, ten and forty values by reading the values from the eeprom. The problem is that the mapping isn't working. I want to make a scale using those values. I get some random numbers, and sometimes just stays 0, and the last time it counted to 10.

I'm outputing this on 7 segment display if it makes any difference.

What might be causing this?

What might be causing this?

Who can tell?

Post your code, post your observations - we're not psychic.

Never mind, I got it working. Thank you very much for your help.