Serial Monitor for instructions

Hello,

I am fairly new to Arduino, though I have done a bit of programming before. I am now taking an online class about interfacing with the Arduino and I am stuck on my latest assignment:

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.

The EEPROM portion is easy. I have already read any memory address and written to it hard coded byte values.

My issue is that I do not seem to understand how to read a command from the Serial Monitor and use it to trigger the EEPROM portion. I have been able to get my code to read the string entered and save it in a char variable, then compare it to Read or Write to know what command was entered:

int c = 0;
int i = 0;

void setup() {
 Serial.begin(9600);
 while (!Serial) {
   ;
 }
 Serial.println("Enter read or write option in the following format");
 Serial.println("Read: Read address. Example: Read 245");
 Serial.println("Write: Write address value. Example: Write 520 123");
}

void loop() {
 c = Serial.available();
 int c1 = 0;
 int c2 = 0;
 //  Serial.print("Unread Bytes: ");
 //  Serial.println(c,DEC);
 delay (500); //To allow the whole instruction be read at once
 char instr[c];
 if (c>0){
   for (i=0;i<=c-1;i++){
     instr[i] = Serial.read();
   }
   instr[c] = '\0';
   Serial.print("Instruction entered: ");
   for (i=0;i<=c-1;i++){
     Serial.print(instr[i]);
   }
   Serial.print("\n");
   char comp1[] = "Read ";
   char comp2[] = "Write ";
   for (i=0;i<=5;i++){
     if (instr[i] == comp1[i]) c1++;
     else if (instr[i] == comp2[i]) c2++;
   }
   if (c1 == 5){
     Serial.println("Read Command");
   }
   if (c2 == 6){
     Serial.println("Write Command");
   }
 } 
}

The problem I have is that the address is now in a char variable (as well as the data to be written).

I do not mean for anybody to do this for me, but suggestions will be greatly appreciated. Thanks!

Sebastian

The examples in Serial Input Basics are simple reliable ways to receive data. The 2nd example will probably meet your need.

There is also a parse example to illustrate how to split the data into variables. It uses a COMMA as the separator but you could easily change that to a SPACE

...R

You are not the only one that received this assignment. You could use the forum search capabilities, and see how others have solved it...

PaulS:
You are not the only one that received this assignment. You could use the forum search capabilities, and see how others have solved it...

That was my initial idea, but I could not find it. I did find it this time. Thanks!

PaulS:
You are not the only one that received this assignment. You could use the forum search capabilities, and see how others have solved it...

Isn't that like turning to the back page to see the solution rather than trying to do the crossword puzzle ?

...R

Robin2:
Isn't that like turning to the back page to see the solution rather than trying to do the crossword puzzle ?

...R

Yes, but so is asking us for the code.