Hallo! Thank You for bothering to look at this message and for trying to help someone.
I Was making a simple calculator that wouldn't require any hardware and which I thought I would make in 15 minutes. so I wanted my calculator to work using the serial monitor so I would first type in 25 and hit enter then I would type in a + or a - or x or * or a / and then hit enter and then type in another number and then it would tell me the answer.
int a;
int b;
int x=1;
char calSignal;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Enter the First number");
}
int solve(int a,int b) {
int result;
switch (calSignal) {
case '+' :
result = a + b;
return result;
break;
case '-' :
result = a - b;
return result;
break;
case 'x' :
result = a * b;
return result;
break;
case '/' :
result = a / b;
return result;
break;
case '*' :
result = a * b;
return result;
break;
case '.' :
result = a * b;
return result;
break;
default :
Serial.println("What was that? Invalid Operator");
}
}
void loop() {
x=1;
while (Serial.available()==0){}
a = Serial.parseInt();
Serial.println(" ");
Serial.print("you have entered : ");
Serial.print(a);
Serial.println(" Please enter an operator");
Serial.println("(x, * or . is allowed for multiplication, / for division, + for addition and - for subtraction)") ;
x=2;
while(Serial.available()==0){}
calSignal = Serial.read();
Serial.println("you have entered : ");
Serial.print(calSignal);
Serial.println("Enter the second number");
x=3;
while(Serial.available()==0){}
b = Serial.parseInt();
Serial.println(" ");
Serial.print("Solution = ");
Serial.print(solve(a,b));
}
and this is the code I wrote for that
compiles and I enter the first number(after it asks me to enter it)
but then it just skips over the operator part
and says that the operator I entered was in fact nothing!
right after i enter the first number
it doesnt even give me a chance to enter in the operator!
int a;
int b;
int x=1;
char calSignal;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Enter the First number");
}
int solve(int a,int b) {
int result;
switch (calSignal) {
case '+' :
result = a + b;
return result;
break;
case '-' :
result = a - b;
return result;
break;
case 'x' :
result = a * b;
return result;
break;
case '/' :
result = a / b;
return result;
break;
case '*' :
result = a * b;
return result;
break;
case '.' :
result = a * b;
return result;
break;
default :
Serial.println("What was that? Invalid Operator");
}
}
void loop() {
x=1;
while (Serial.available()==0){}
a = Serial.parseInt();
Serial.println(" ");
Serial.print("you have entered : ");
Serial.print(a);
Serial.println(" Please enter an operator");
Serial.println("(x, * or . is allowed for multiplication, / for division, + for addition and - for subtraction)") ;
x=2;
while(Serial.available()==0){Serial.read();}
calSignal = Serial.read();
Serial.println("you have entered : ");
Serial.print(calSignal);
Serial.println("Enter the second number");
x=3;
while(Serial.available()==0){}
b = Serial.parseInt();
Serial.println(" ");
Serial.print("Solution = ");
Serial.print(solve(a,b));
}
int a;
int b;
char calSignal;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Enter the First number");
}
int solve(int a,int b) {
int result;
switch (calSignal) {
case '+' :
result = a + b;
return result;
break;
case '-' :
result = a - b;
return result;
break;
case 'x' :
result = a * b;
return result;
break;
case '/' :
result = a / b;
return result;
break;
case '*' :
result = a * b;
return result;
break;
case '.' :
result = a * b;
return result;
break;
default :
Serial.println("What was that? Invalid Operator");
}
}
void loop() {
while (Serial.available()==0){}
a = Serial.parseInt();
Serial.println(" ");
Serial.print("you have entered : ");
Serial.print(a);
Serial.println(" Please enter an operator");
Serial.println("(x, * or . is allowed for multiplication, / for division, + for addition and - for subtraction)") ;
while (Serial.available() > 0){Serial.read();}
while(Serial.available()==0){}
// char x=Serial.read();
calSignal = Serial.read();
Serial.println("you have entered : ");
Serial.print(calSignal);
Serial.println("Enter the second number");
while (Serial.available() > 0){Serial.read();}
while(Serial.available()==0){}
b = Serial.parseInt();
Serial.println(" ");
Serial.print("Solution = ");
Serial.print(solve(a,b));
}
hey this works partially it just doesn't allow me to enter the second number
First you have to remember that Serial is S. L. O. W. johnwasser's code runs so fast that it will not consume any more than one character. The second one has just begun transmission when that code thinks it has finished.
Second, the parseInt() function and its friends are very simple blunt objects. They are as dumb as rocks.
The simple solution is to use that other Arduino blunt object, the delay() function. Add enough delay just after parsing the number or operator so that the carriage-return and line-feed characters can arrive. 50 or 100 milliseconds will do it. Then discard those extra characters.
The better solution is to make your own parser which processes single characters when they arrive and leaves the processor available for other simultaneous tasks.
you have entered : 4 Please enter an operator
(x, * or . is allowed for multiplication, / for division, + for addition and - for subtraction)
you have entered : xEnter the second number
Solution = 0
serial monitor
doesnt let me add the second number still
int a;
int b;
char calSignal;
void setup() {
// put your setup code here, to run once:
Serial.begin(2000000);
Serial.println("Enter the First number");
}
int solve(int a,int b) {
int result;
switch (calSignal) {
case '+' :
result = a + b;
return result;
break;
case '-' :
result = a - b;
return result;
break;
case 'x' :
result = a * b;
return result;
break;
case '/' :
result = a / b;
return result;
break;
case '*' :
result = a * b;
return result;
break;
case '.' :
result = a * b;
return result;
break;
default :
Serial.println("What was that? Invalid Operator");
}
}
void loop() {
while (Serial.available()==0){}
a = Serial.parseInt();
Serial.println(" ");
Serial.print("you have entered : ");
Serial.print(a);
Serial.println(" Please enter an operator");
Serial.println("(x, * or . is allowed for multiplication, / for division, + for addition and - for subtraction)") ;
while (Serial.available() > 0){Serial.read();delay(50);}
while(Serial.available()==0){}
// char x=Serial.read();
calSignal = Serial.read();
Serial.print("you have entered : ");
Serial.print(calSignal);
Serial.println("Enter the second number");
while (Serial.available() > 0){Serial.read();delay(50);}
while(Serial.available()==0){}
b = Serial.parseInt();
Serial.println(" ");
Serial.print("Solution = ");
Serial.print(solve(a,b));
}
this works but their are some glitches
the serial monitor shows this when i do 4x4
Enter the First number
you subtraction)
4 Please enter an operator
(x, * or . is allowed for multiplication, / for division, + for addition and - for subtraction)
you have entered : xEnter the second number
Solution = 16
you subtraction)
0 Please enter an operator
(x, * or . is allowed for multiplication, / for division, + for addition and - for subtraction)
Can someone just rewrite my code with the make your own parser thing and the delay stuff and all that
I want it to be like I enter 2 suppose hit enter, enter x hit enter and then 3 and then hit enter and i get my solution!
tada!
do you know how I can do that? seriously I thought this would take 15 mins...
I dunno it feels like too much to ask for someone to rewrite the code
what do I do?
it's all ok right now!
it just doesn't let me enter the second number so I guess it isn't ok after all...
int a;
int b;
char calSignal;
void setup() {
// put your setup code here, to run once:
Serial.begin(2000000);
Serial.println("Enter the First number");
}
int solve(int a,int b) {
int result;
switch (calSignal) {
case '+' :
result = a + b;
return result;
break;
case '-' :
result = a - b;
return result;
break;
case 'x' :
result = a * b;
return result;
break;
case '/' :
result = a / b;
return result;
break;
case '*' :
result = a * b;
return result;
break;
case '.' :
result = a * b;
return result;
break;
default :
Serial.println("What was that? Invalid Operator");
}
}
void loop() {
while (Serial.available()==0){}
a = Serial.parseInt();
//Serial.println(" ");
//Serial.print("you have entered : ");
Serial.print(a);
//Serial.println(" Please enter an operator");
//Serial.println("(x, * or . is allowed for multiplication, / for division, + for addition and - for subtraction)") ;
while (Serial.available() > 0){Serial.read();delay(100);}
while(Serial.available()==0){}
// char x=Serial.read();
calSignal = Serial.read();
//Serial.print("you have entered : ");
Serial.print(calSignal);
//Serial.println("Enter the second number");
while (Serial.available() > 0){Serial.read();delay(100);}
while(Serial.available()==0){}
b = Serial.parseInt();
Serial.print(b);
Serial.print("Solution = ");
Serial.print(solve(a,b));
}
it
Enter the First number
4x0Solution = 0
the serial monitor shows this for good ol' 4x4 again
except I didn't enter the last four cause it didn't let me enter it
int a;
int b;
char calSignal;
void setup() {
Serial.begin(2000000);
Serial.println("Enter the Calculation");
while (Serial.available()==0){}
a = Serial.parseInt();
Serial.print(a);
calSignal = Serial.read();
Serial.print(calSignal);
b = Serial.parseInt();
Serial.print(b);
Serial.println(" ");
//Serial.print("Solution = ");
Serial.print(solve(a,b));
Serial.println("");
Serial.println("To Do Another Calculation Press The Red Reset Button!");
}
int solve(int a,int b) {
int result;
switch (calSignal) {
case '+' :
result = a + b;
return result;
break;
case '-' :
result = a - b;
return result;
break;
case 'x' :
result = a * b;
return result;
break;
case '/' :
result = a / b;
return result;
break;
case '*' :
result = a * b;
return result;
break;
case '.' :
result = a * b;
return result;
break;
default :
Serial.println("What was that? Invalid Operator");
}
}
void loop() {
}
this works
thankyou for your time everyone!!!!
Enter the Calculation
4x4
16
To Do Another Calculation Press The Red Reset Button!