Notifications
Clear all

[Solved] simple serial monitor calculator won't work

7 Posts
4 Users
3 Likes
1,084 Views
(@tperry724)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

Folks,

It seems the operator isn't getting picked up.  Can someone spot the problem?

Best,

Tony

 

float myNumber1;
float myNumber2;
float result;
String msg="Enter first number: ";
String msg2="Enter second number: ";
String msg3="Enter your operator (+,-,*,/)";
String calc_operator;
String msg4="Your answer is: ";

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:

Serial.println(msg);
while (Serial.available()==0); {
}
myNumber1=Serial.parseFloat();

Serial.println(msg2);
while (Serial.available()==0) {
}
myNumber2=Serial.parseFloat();

Serial.println(msg3);
while (Serial.readString()==0); {
}
calc_operator=Serial.readString();

Serial.println(calc_operator);

if (calc_operator == "+") {
result = myNumber1 + myNumber2;
}

if (calc_operator == "-") {
result = myNumber1 - myNumber2;
}

if (calc_operator == "*") {
result = myNumber1 * myNumber2;
}

if (calc_operator == "/") {
result = myNumber1 / myNumber2;
}


Serial.print(msg4);
Serial.println(result);
Serial.println();

}

 


   
Quote
ZoolanderMicro
(@zoolandermicro)
Member
Joined: 4 years ago
Posts: 144
 

Does this actually compile without complaint? I see three while() loops that you close without any body. What are they for? Adding comments to your code may help to make your intent clear. Do not place a semi-colon after the closing paren of the while loop function: 

while (Serial.available()==0);

Also, you could have entered the dialog into the println() functions just as easily as creating String objects, and it would save a lot of global memory and program space. It would also make the code easier to follow. Practice getting user input and outputting it to the serial monitor. There are many good books as reference. I recommend Beginning C for Arduino, Second Edition: Learn C Programming for the Arduino by Jack Purdum. It has lots of examples of getting user input. It may be easier to select the arithmetic operation by printing numbered choices. The user then enters the number of the operation. That number (assigned to a variable) can be passed to a switch case to perform the math. 

Serial.println("Enter the number for the arithmetic operation do you want to perform");

Serial.println("1. Addition");

Serial.println("2. Subtraction");

Serial.println("3. Multiplication");

Serial.print("4. Division"); 

Switch statements are easy to follow, they compile economically, and you don't have to test for a character in a string. Each case in the switch performs the operation on the myNumber1 and myNumber2 values entered by the user. 

ZoolanderMicro, where small ideas are a big deal


   
ReplyQuote
(@frank)
Member
Joined: 4 years ago
Posts: 4
 
Posted by: @tperry724

Folks,

It seems the operator isn't getting picked up.  Can someone spot the problem?

Best,

Tony

Serial.println(msg3);
while (Serial.readString()==0); {
}


I believe in the loop waiting for the operator to be typed in you want to use Serial.available()  not Serial.readString(), no?

regards

Frank


   
tperry724 reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@tperry724

Firstly, I would remove the empty bodies (the curly braces{}) you have after the semi colons - Though a valid language construct, they are not required in this instance.

Secondly, the problem you're experiencing is due to the serial buffer not being properly read by the calls you're making.  What I mean by that is that there is still some characters left in the stream buffer (such as a new line character), that are being automatically read by the next operation before you get to enter anything, hence your user input is being skipped and thus ignored.

To fix that, you'll need to remove any remaining characters in the serial stream after each read, with the following serial stream member function:

  Serial.readStringUntil('\n');

This should fix it, so have a go and post back if you get stuck.

Cheers.


   
tperry724 reacted
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@zoolandermicro

Posted by: @zoolandermicro

Does this actually compile without complaint? I see three while() loops that you close without any body. What are they for? Adding comments to your code may help to make your intent clear. Do not place a semi-colon after the closing paren of the while loop function: 

while (Serial.available()==0);

Actually, placing a semi colon after a while loop like that is quite valid code under the language, and regularly used.  Likewise, the curly braces are valid code under the language too, even following the semi colon.  All they do is introduce a new scope, but there is no need for them to have been used in this scenario.

Cheers.

 


   
tperry724 reacted
ReplyQuote
(@tperry724)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

@frank  Thanks, Frank.  It worked.  Have a great day.


   
ReplyQuote
(@tperry724)
Member
Joined: 4 years ago
Posts: 33
Topic starter  

@frogandtoad   Thank you.  I've cleaned up my code like you suggested and I will try the serial stream member function.    Have a great day.  


   
ReplyQuote