Write and read with one function to several arrays (Simulate EEPROM in an Array)

Update:
You can find my solution here: Topic: Send array from Arduino to html page on AsyncWebServer (ESP8266)

Hi together,

like everybody who is posting something here, I also have a question and I hope that someone here knows the reason for the issue:

Background:
In the attached sketch example, I would like to use an array, to simulate the read and write functions of an EEPROM. My Project has several values, that I would to write to the history once each hour and keep the values for a period of 30 days. (So each EEPROM byte is written once each 30 days at the end, because the latest value will be replaceed be the newest one.)

Before I write the values to the EEPROM array, I would like to write the values to an array that just keeps the history for one value. Idea: The 1. (or n-th) value in each “value history array”, is connected to the same timestamp. That makes the handling easier. (The EEPROM can be read, to fill these arrays.) So I would like to use one function, to handly several “value history Arrays”.

My problem:

This is working fine (99 is written and 99 read):
-byte vByteval = HistTemperature[vAddress];

But when I try to define the name of the array as a part of the function:
-byte vByteval = vArray[vAddress];
99 is written but 72 is read!

Just check the attached code with you boards.

I used the following one:
ESP8266 ESP-12F

Best regards
Noyen

(This is my first post. I really tried, but maybe I missed something. Just let me know.)

EEPROM_Simulation.ino (985 Bytes)

This is my first post. I really tried, but maybe I missed something. Just let me know.

Welcome to the forum

If you looked at Read this before posting a programming question then you did not follow the instructions regarding posting code

Please post your code here in code tags

Your code, posted properly below (see "How to use the forum").

One of your several problems is that you are passing a String object, not a numeric array name or pointer, to a function. The function returned the value 72, because that is the ASCII representation of 'H', the first character in the String.

You need to read up on the basics of working with arrays, and passing array values versus addresses or pointers to and from functions.

byte HistTemperature[720];
byte HistHumidity[720];

void setup() {
  Serial.begin(115200);
}

void loop() {
  write_byte("HistTemperature", 0, 99);  //Write byte '99' to array HistTemperature @ address 0 (Byte = 0-255)
  read_byte("HistTemperature", 0);       //Read from array HistTemperature @ address
  delay(1000);
}

int read_byte(String vArray, int vAddress)
{
  //byte vByteval = HistTemperature[vAddress];
  byte vByteval = vArray[vAddress];
  Serial.print("Read from Array ");
  Serial.print(vArray);
  Serial.print(" at address: ");
  Serial.print(vAddress);
  Serial.print("):");
  Serial.println(vByteval);
  return vByteval;
}


void write_byte(String vArray, int vAddress, byte vByteval)
{
  Serial.print("Write to Array ");
  Serial.print(vArray);
  Serial.print(" at address: ");
  Serial.print(vAddress);
  Serial.print("):");
  Serial.println(vByteval);
  //HistTemperature[vAddress] = vByteval;
  vArray[vAddress] = vByteval;
}

Hi jremington,
Hi UKHeliBob,

thank you both for your feedback.

Jremington, you where totally right. I needed to improve my pointer knowledge. This Video really helped me: Character arrays and pointers - part 1 - YouTube (the interesting part is starting at minute 6).

Now the functions are working fine.

And thanks to you, UKHeliBob, too. I agee that it makes sense to add such a short code as a Quote:

//Define arrays
byte HistTemperature[720];
byte HistHumidity[720];

//Define pointer to the first adress of the array 
//No functinal need (just needed, to print the adress in this example: Serial.printf("%d\n",pHistTemperature + i);)
byte* pHistTemperature;

//Variables just for this example
byte i, k, z, vValue;
byte j = 55;

void setup() {
  Serial.begin(115200);
  //Small delay (wait until the monitor is ready)
  delay(200);

  //Define pointer to the first adress of the array 
  //No functinal need (just needed, to print the adress in this example: Serial.printf("%d\n",pHistTemperature + i);)
  pHistTemperature = HistTemperature;
}

void loop() {
  while(i < 10)
  {   
    Serial.print("#Define 10 values (0 till 9)#");
    Serial.println(i);

    //The steps between the adresses are depending on the data type of the array:
    //step size for byte arrays: 1 (byte)
    //step size for char arrays: 1 (byte)
    //step size for int arrays:  4 (byte)
    //...
    Serial.print("Adress of HistTemperature[");
    Serial.print(i);
    Serial.println("]:");
    //Address source: array
    Serial.printf("%d\n",&HistTemperature[i]);
    //Address source: pointer to array
    Serial.printf("%d\n",pHistTemperature + i);

    //Not working syntax:
    //Serial.printf("%d\n",*pHistTemperature[i]);
    //Compiled syntax, but with wrong result:
    //Serial.printf("%d\n",*(pHistTemperature + i));
    //Serial.printf("%d\n",pHistTemperature[i]);
    
    pHistTemperature[i] = j;
    //*p = j;

    Serial.print("HistTemperature ");
    Serial.print(i);
    Serial.print(":");
    Serial.println(HistTemperature[i]);

    i++;
    j--;
  }

  while(k < 10)
  {
    Serial.print("#Read Funktion Runde#");
    Serial.println(k);

    vValue = read_byte(HistTemperature, k);
    Serial.print("HistTemperature ");
    Serial.print(k);
    Serial.println(":");
    Serial.println(vValue);
//    Serial.println(HistTemperature[k]);

    k++;
  }

  while(z <= 0)
  { 
    Serial.println("#Write Funktion Test");
    write_byte(HistTemperature, 400, 4);  //Write byte '99' to array HistTemperature @ address 0 (Byte = 0-255)
    write_byte(HistTemperature, 401, 56);  //Write byte '99' to array HistTemperature @ address 0 (Byte = 0-255)
    write_byte(HistTemperature, 402, 234);  //Write byte '99' to array HistTemperature @ address 0 (Byte = 0-255)
    write_byte(HistHumidity, 700, 6);  //Write byte '99' to array HistTemperature @ address 0 (Byte = 0-255)
    write_byte(HistHumidity, 719, 76);  //Write byte '99' to array HistTemperature @ address 0 (Byte = 0-255)
  
    Serial.println("#Ausgabe#");
    Serial.println(HistTemperature[400]);
    Serial.println(HistTemperature[401]);
    Serial.println(HistTemperature[402]);
    Serial.println(HistHumidity[700]);
    Serial.println(HistHumidity[719]);
    Serial.println(HistTemperature[0]);
    Serial.println(HistTemperature[1]);
    Serial.println(HistTemperature[2]);
    Serial.println(HistTemperature[3]);
    Serial.println(HistTemperature[4]);
    Serial.println(HistTemperature[5]);
    Serial.println(HistTemperature[6]);
    Serial.println(HistTemperature[7]);
    Serial.println(HistTemperature[8]);
    Serial.println(HistTemperature[9]);

    z++;
  }
}

byte read_byte(byte *vAdressOfArray, int vPositionInArray)
{
  byte vByteval = *(vAdressOfArray + vPositionInArray);
  Serial.print("Read from AdressOfArray ");
  Serial.printf("%d",&*(vAdressOfArray + vPositionInArray));
  Serial.print(":");
  Serial.println(vByteval); 
  return vByteval;
}


void write_byte(byte *vAdressOfArray, int vPositionInArray, byte vByteval)
{
  Serial.print("Write to AdressOfArray ");
  Serial.printf("%d",&*(vAdressOfArray + vPositionInArray));
  Serial.print(":");
  Serial.println(vByteval);
  *(vAdressOfArray + vPositionInArray) = vByteval;
}

Please let me know, if there is still something unusual (in my sketch oder my post :slight_smile: )

Best regards
Noyen

EEPROM_Simulation.ino (3.85 KB)

Please let me know, if there is still something unusual

Like the fact that you posted it in [­quote] [­/quote] tags rather than in [­code] [­/code] tags

UKHeliBob: Like the fact that you posted it in [­quote] [­/quote] tags rather than in [­code] [­/code] tags

Done - Thanks for the hint.

Hint: the name of an array (without the square brackets) IS a pointer.

jremington: Hint: the name of an array (without the square brackets) IS a pointer.

Thanks, I already used it in the code:

//Define pointer to the first adress of the array 
//No functinal need (just needed, to print the adress in this example: Serial.printf("%d\n",pHistTemperature + i);)
pHistTemperature = HistTemperature;

And also in the function:

Function call:

write_byte(HistTemperature, 400, 4);

Function:

void write_byte(byte *vAdressOfArray, int vPositionInArray, byte vByteval)
{
  Serial.print("Write to AdressOfArray ");
  Serial.printf("%d",&*(vAdressOfArray + vPositionInArray));
  Serial.print(":");
  Serial.println(vByteval);
  *(vAdressOfArray + vPositionInArray) = vByteval;
}

Should I change something there?

If I may ask an additional question: Could you please name me a link, where I can find a description, how to send an array from the Arduino IDE to a html page (located on an ESPAsyncWebServer)?

Update: I found the solution on my own: Topic: Send array from Arduino to html page on AsyncWebServer (ESP8266)