I'm currently taking the UCI class on programming IOT devices through Coursera, and working on an assignment where you read and write values to the EEPROM through the serial monitor. I'm trying to write values to variables using Serial.read() and having it print the input back out before I actually write the code to have it read and write to the EEPROM. The issue I'm having is that the serial monitor seems to be skipping over lines of code before receiving input. I did some googling on this issue and copy and pasted the code at this page into the IDE:
(as a side note, the code is missing an additional closed curly bracket at the end of the loop)
Even here, my serial monitor is skipping right from:
Enter your name
to
Enter your Mobile No.
without receiving any input on my end. I'm pretty stumped at this point and any assistance would be much appreciated.
Welcome to the Forum. Usually, questions like this will garner better answers if you can post the code using code tags. (Read the first three posts on this Forum for details on how to do this.) It's also helpful if you tell us which Arduino you are using and your host operating system.
String name = "";
String Mobile = "";
String Address = "";
String Email = "";
void setup()
{
Serial.begin(9600);
}
void loop()
{
Serial.println("Enter your name.");
while (Serial.available() == 0)
{ //Wait for user input }
name = Serial.readString(); //Reading the Input string from Serial port.
Serial.println("Enter your Moblie No.");
while (Serial.available() == 0) {}
Mobile = Serial.readString();
Serial.println("Enter your Address.");
while (Serial.available() == 0) {}
Address = Serial.readString();
Serial.println("Enter your Email.");
while (Serial.available() == 0) {}
Email = Serial.readString();
Serial.println("-------------------------"); //Showing the details
Serial.println("YOUR NAME:" + name);
Serial.println("YOUR MOBILE NO:" + Mobile);
Serial.println("YOUR ADDRESS:" + Address);
Serial.println("YOUR EMAIL:" + Email);
Serial.println("Thanks You...");
Serial.println("");
while (Serial.available() == 0) {}
}
If you want help here, post your code here so that members do not have to download it. More members will see your code if posted properly. Read the how get the most out of this forum sticky to see how to properly post code. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code in code tags.
Thanks for the help everyone. I found the missing curly bracket in the example code and got that to work. Now I'm working through the assignment and I'm getting really stuck with variables and an if loop. Here's the code I used to verify the right values are getting into the inp variable
int inp;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Press 1 to write or 2 to read");
while ( Serial.available()==0){
}
if (Serial.available() != 0){
inp = Serial.read(); //Serial.println(inp);
}
if (inp == 1){
Serial.println("OK");
inp = 0;
}
}
and i get the output shown in the img1 attachment
When I un-comment out the Serial.println(inp); line of code and type 1 into the serial monitor, I get the output in the img2 attachment.
I thought that it spitting out 49 and 10 might've had something to do with ASCII values for 1, but I wasn't able to make heads or tails of that. Any thoughts on what I'm missing?
Thank you, I updated the code and added some more stuff to it and here's where it's at now
int inp;
int addr = -1;
int val = -1;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("Press 1 to write to EEPROM or 2 to read from EEPROM");
while ( Serial.available()==0){
}
if (Serial.available() != 0){
inp = Serial.read();
}
if (inp == 49){
Serial.print("Which address to write to?");
while (Serial.available() == 0) {}
if (Serial.available() !=0){
addr=Serial.read();
}
Serial.println(addr);
addr = -1;
inp = -1;
}
}
and it is giving me the output shown in the attachment. It is not letting me enter any input for the addr variable. I tried using quotation marks to get around the issue with the ASCII characters but that didn't seem to work. I also looked through the documentation on this site regarding the different variable types and couldn't find anything that helped me with this. Could someone please point me in the right direction? Thank you for your patience, this is my first exposure to the Arduino and C, and I just have some basic Python background to fall back on.
I agree it looks more natural, I updated the code with that and it behaved similarly. I'm trying to figure out how to get the code to ignore the '10' value but I don't know if I want to get too hung up on that since I don't think it should ultimately effect the read and writes to the EEPROM once I program for that. I'm wondering if the issue is with this part of the code:
if (inp == '1'){
Serial.print("Which address to write to?");
while (Serial.available() == 0) {}
if (Serial.available() !=0){
addr=Serial.read();
}
It seems to be skipping over this once I set the inp to 1. Is this due to the 10 that's being produced from the first Serial.available() read? Or does the second "Serial.available() == 0" line of code not execute because it thinks Serial.available() != 0 for some reason? I tried adding delays and changing the Serial.println to Serial.print to no avail.
Are you referring to changing the value in the circled drop down? Because that seems to have gotten around that hurdle. The problem I'm now having is that I entered 55 for addr and its printing 53...