Please help-Issue with Arduino program to read and write to EEPROM

Hello Everyone,

I am new to programming and the Arduino environment. I am taking a class and an assignment is to create a sketch that will allow a person to read and write to EEPROM by typing read (EEPROM address) or write (EEPROM address, value).

I think i am on the right track and i am trying to code the right way (I think), but when I try to compile my code, I get an error on line 50 that says:

"exit status 1
invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]"

I have tried to find code that would help covert, but everything I read doesn’t help me. I think most of it is over my head. If anyone has any suggestions on how i could do this, or even to do it better I would appreciate any constructive feedback you can give me. I have spent over 7 hours on this little bit of code, and I still can’t make it work.

Here is my sketch

/* This is a Sketch that will allow a peron to read and write values

  • to the Arduino EEPROM. It takes two commands read and write.
  • The read command takes 1 argument the EEPROM location and will
  • return the value stored there. The write command takes 2 arguments
  • (EEPROM loc, value) and will write the value to EEPROM location.
    */

#include <EEPROM.h>
#include <string.h>
#include <stdlib.h>

// start reading from the first byte (address 0) of the EEPROM

String instructions = “Type read(loc) or write(loc,value)in lowercase”;
String readlocout = "In EEPROM location ";
String readvalout = "This value is stored: ";
String writevalout = "You have put the value ";
String writelocout = " into EEPROM register ";
String getcommand;
String getaddr;
String getval;
int addr;
int value;
int readval;

void setup() {
// initialize serial and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available() > 0) {
}
getcommand = Serial.readStringUntil(’ ');
Serial.read();
addr = Serial.parseInt();
Serial.read();
value = Serial.parseInt();
if(getcommand == ‘read’) readEEPROM(addr);

if(getcommand == ‘write’) writeEEPROM(addr, value);

else if (getcommand != ‘read’ && getcommand != ‘write’)
{
Serial.println(“That is not a valid command, use read or write”)
}
}

void readEEPROM(int addr) //Reads the value in the EEPROM location that is passed
{
Serial.println(“Reading from EEPROM”);
readval = EEPROM.read(addr);
Serial.print(readlocout);
Serial.print(addr);
Serial.print("\t");
Serial.print(readvalout);
Serial.println(readval);
delay(100);
}

void writeEEPROM(int addr, int value) //Writes the value to the EEPROM memory location
{
Serial.println(“Reading Serial In”);
EEPROM.write(addr, value);
delay(100);
Serial.print(writevalout);
Serial.print(value);
Serial.print("\t");
Serial.print(writelocout);
Serial.println(addr);

}

Use " for strings and ' for chars. ("text", 'x')

  if (getcommand == "read") readEEPROM(addr);

  if (getcommand == "write") writeEEPROM(addr, value);

  else if (getcommand != "read" && getcommand != "write")
  {
    Serial.println("That is not a valid command, use read or write"); // had a missing ';' 
  }

Your code doesn’t quite work for a couple of other reasons as well as that pointed out by Whandall. I made a couple of minor changes - first only printing this if there actually was serial data received:- “That is not a valid command, use read or write”

I moved this and wrapped it around the serial reads:-

while (Serial.available() > 0)
{
.
.
}

I removed the 100ms delays. They’re not needed.

You don’t need to explicitly include “stdlib.h”

I included the print of the instructions.

It now looks like this:-
(It might not be exactly what you want, but it’s closer. Have a good look so you understand the changes fully.)

/* This is a Sketch that will allow a peron to read and write values
 *  to the Arduino EEPROM. It takes two commands read and write.
 *  The read command takes 1 argument the EEPROM location and will
 *  return the value stored there. The write command takes 2 arguments
 * (EEPROM loc, value) and will write the value to EEPROM location.
 */


#include <EEPROM.h>
#include <string.h>
//#include <stdlib.h>

// start reading from the first byte (address 0) of the EEPROM

String instructions = "Type read(loc) or write(loc value)in lowercase";
String readlocout = "In EEPROM location ";
String readvalout = "This value is stored: ";
String writevalout = "You have put the value ";
String writelocout = " into EEPROM register ";
String getcommand;
String getaddr;
String getval;
int addr;
int value;
int readval;


void setup()
{
    // initialize serial and wait for port to open:
    Serial.begin(9600);
    while (!Serial)
    {
        ; // wait for serial port to connect. Needed for native USB port only
    }
    Serial.println(instructions);
    Serial.println();
}
void loop()
{
    // put your main code here, to run repeatedly:
    if (Serial.available() > 0)
    {
        while (Serial.available() > 0)
        {
            getcommand = Serial.readStringUntil(' ');
            Serial.read();
            addr = Serial.parseInt();
            Serial.read();
            value = Serial.parseInt();
        }
        if (getcommand == "read")
            readEEPROM(addr);

        if (getcommand == "write")
            writeEEPROM(addr, value);

        else if (getcommand != "read" && getcommand != "write")
            Serial.println("That is not a valid command, use read or write");
    }
}

void readEEPROM(int addr)  //Reads the value in the EEPROM location that is passed
{
    Serial.println("Reading from EEPROM");
    readval = EEPROM.read(addr);
    Serial.print(readlocout);
    Serial.print(addr);
    Serial.print("\t");
    Serial.print(readvalout);
    Serial.println(readval);
    Serial.println();
    //    delay(100);
}

void writeEEPROM(int addr, int value) //Writes the value to the EEPROM memory location
{
    Serial.println("Writing to EEPROM");
    EEPROM.write(addr, value);
    //    delay(100);
    Serial.print(writevalout);
    Serial.print(value);
    Serial.print("\t");
    Serial.print(writelocout);
    Serial.println(addr);
    Serial.println();
}

For future reference, when posting code you should always place it in a code block by using [code]code tags[/code], as I’ve done above.
That makes it easier to copy and paste into an IDE for verification, and also stops the forum software from potentially corrupting the text with smilies, italics etc.

Also, it’s a good idea to put each statement on a separate line when posting on the forums.
Like this:-

if(getcommand == 'read')
    readEEPROM(addr);

Instead of like this:-

if(getcommand == 'read') readEEPROM(addr);

Thank you all so much. It really does help me to learn how real programmers code. I will be sure to use all the information in the future. Thank you again, and have a great day.

Thales1975:
Thank you all so much. It really does help me to learn how real programmers code. I will be sure to use all the information in the future. Thank you again, and have a great day.

Glad to help. That's my good deed done for the day. Now I can be mean and rude again. :smiley: