Serial monitor and EEPROM

Hello, I am taking an online class about Interfacing with the Arduino on Coursea course. I am now hard stuck on the assignment from the class. The assignment is "Write a sketch that allows a user to access data in EEPROM using the serial monitor. In the serial monitor the user should be able to type one of two commands: “read” and “write. "Read" takes one argument, an EEPROM address. "Write" takes two arguments, an EEPROM address and a value.

For example, if the user types “read 3” then the contents of EEPROM address 3 should be printed to the serial monitor. If the user types “write 3 10” then the value 10 should be written into address 3 of the EEPROM."
I do not know what is wrong with my code. It seems like the program does not read the data at all when writing data to the EEPROM address. The program seems fine when the user chooses to read, though.
Thank you for your help.

#include <EEPROM.h>
String input = "";
int data = 0;
int readAddress = 0;
int writeAddress = 0;
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  Serial.println("Type 'read' if you want to read from the EEPROM or type 'write' if you want to type to the EEPROM");
  while (Serial.available() == 0){}
  input = Serial.readString();
  
  if (input == "read")
  {
    Serial.println("Please type the address");
    while (Serial.available() == 0){}
    readAddress = Serial.read();
    Serial.println (EEPROM.read (readAddress));
  }
  else if (input == "write")
  {
    Serial.println("Please type the address");
    while (Serial.available() == 0){}
    writeAddress = Serial.read();
    Serial.println ("Please type the data you want to write");
    while (Serial.available() == 0) {}
    data = Serial.read();
    if (data < 0 || data > 255)
    {
      Serial.println("The data is invalid,it should be between 0 and 255");
    }
    else
    {
      EEPROM.write (writeAddress, data);
    }
  }
}

Please describe what serial monitor shows you. When you type read or write, does it tell you to enter the address? Or does it not show you that?


After i type 'write' and then the address, the program does not let me type the data in

What are the settings for the line ending in the serial monitor?

I am sorry I don't understand what is line ending in the serial monitor mean( I am quite new to Ardunio). I use a tinkercad simulation for the assignment. https://www.tinkercad.com/dashboard

I have no idea about TinkerCad. In the IDE you would have the following options

  1. Carriage return
  2. Line feed
  3. Both NL and CR
  4. None

Anything similar to that anywhere in TinkerCad?

I tried to look for it, but i can not find it anywhere. I tried on a different simulator (Wokwi) and it didn't even let me input the address argument. I did not have the physical Arduino microcontroller so I have to do this assignment on the simulator.
Is there any error in my code? i think the problem is lying in my code but i cannot find where it gone wrong.

I've tested the code using a real Arduino Uno R3, with the serial monitor set to 'No line ending'.

Here are the results from the serial monitor:

21:27:27.085 -> Type 'read' if you want to read from the EEPROM or type 'write' if you want to type to the EEPROM
21:27:43.401 -> Please type the address
21:27:49.003 -> 48
21:27:49.003 -> Type 'read' if you want to read from the EEPROM or type 'write' if you want to type to the EEPROM
21:27:50.054 -> Type 'read' if you want to read from the EEPROM or type 'write' if you want to type to the EEPROM
21:28:13.460 -> Please type the address
21:28:24.217 -> Please type the data you want to write
21:28:24.257 -> Type 'read' if you want to read from the EEPROM or type 'write' if you want to type to the EEPROM
21:28:25.264 -> Type 'read' if you want to read from the EEPROM or type 'write' if you want to type to the EEPROM

  • The first time round, I typed 'read'.
  • when asked for the address, I typed '100'.
  • The result 48 was then printed.
  • The next time round I typed 'write'.
  • When asked for the address, I typed '100'.
  • The message 'Please type the data you want to write' appears.
  • Before you have time to answer it has moved on and printed:
    'Type 'read' if you want to read from the EEPROM or type 'write' if you want to type to the EEPROM, again.

If you look at the timestamps there is only 40ms between
21:28:24.217 -> Please type the data you want to write
and
21:28:24.257 -> Type 'read' if you want to read from the EEPROM or type 'write' if you want to type to the EEPROM

That 40ms is clearly not enough time to reply .

To know what serial monitor in Tinkercad sends to the Arduino, you can try the follwoing sketch

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

void loop()
{
  if (Serial.available())
  {
    Serial.print("0x");
    byte b = Serial.read();
    if (b < 0x10)
    {
      Serial.print("0");
    }
    Serial.print(b, HEX);
    Serial.print(" ");
  }
}

This will print every byte that is received in hexadecimal format. If you see 0x0A (line feed / NL) and/or 0x0D (carriage return / CR) you can let us know what you see.

Code typed on the forum so not tested.

Note: you can check the values that you get on e.g. https://www.asciitable.com/.

I tried to put delay () function right after the line Serial.println ("Please type the data you want to write"); but it seems like the program does not receive the data at all.

Hello, I have copied the code and put it in tinkercad. And here what is printed on the Serial monitor screen after i type 1;2;3;a;b and c on the monitor


I did not know whether it is line feed/NL or carriage return / CR.

Newline is 0x0A
Carriage Return is 0x0D
You are not seeing either of those so it must be 'No Line Ending'.

This is what it would look like if both NL and CR were being sent:

I suspect that your confusion comes from the difference between text and numbers.

What you type in serial monitor is text. I did modify your code slightly (see the end) and the output for a read from address 0 is

10:29:21.741 -> Type 'read' if you want to read from the EEPROM or type 'write' if you want to type to the EEPROM
10:29:26.134 -> Please type the address
10:29:27.332 -> Read address = 48
10:29:27.332 -> Data from EEPROM = 50

48 is the decimal ascii value for the character 0, 50 is the decimal ascii value for the character 2.

If you want to read address 0, you will need to convert. If you limit yourself to the addresses 0 .. 9, you can subtract 48 from the address and use that; the same for the data that you want to write.

Modified code (I did initially forgot to add it)

#include <EEPROM.h>
String input = "";
int data = 0;
int readAddress = 0;
int writeAddress = 0;
void setup()
{
  Serial.begin(115200);
}

void loop()
{
  Serial.println("Type 'read' if you want to read from the EEPROM or type 'write' if you want to type to the EEPROM");
  while (Serial.available() == 0) {}
  input = Serial.readString();

  if (input == "read")
  {
    Serial.println("Please type the address");
    while (Serial.available() == 0) {}
    readAddress = Serial.read();
    Serial.print("Read address = ");
    Serial.println(readAddress);
    Serial.print("Data from EEPROM = ");
    Serial.println (EEPROM.read (readAddress));
  }
  else if (input == "write")
  {
    Serial.println("Please type the address");
    while (Serial.available() == 0) {}
    writeAddress = Serial.read();
    Serial.println ("Please type the data you want to write");
    while (Serial.available() == 0) {}
    data = Serial.read();
    if (data < 0 || data > 255)
    {
      Serial.println("The data is invalid,it should be between 0 and 255");
    }
    else
    {
      Serial.print("Write address = ");
      Serial.println(writeAddress);
      Serial.print("Data = ");
      Serial.println(data);
      EEPROM.write (writeAddress, data);
    }
  }
}

You might benefit and get ideas from Robin's updated serial input basics tutorial.

Oh i understand now !
Thank you so much for your help :grin:

If the read/write address is larger than one digit 0-9 (or the ascii equivalent)
you may want to consider using serial.parseInt() instead of serial.read().

However, the methods of the previously mentioned tutorial on serial input basics are superior as they are non blocking.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.