system
1
Hi i have to make this programm for school but i dont get how to do it.my code looks like this
int a=0;
int b=0;
int erg=0;
void setup(){
Serial.begin(9600);
}
void loop(){
if (Serial.available() > 0 && a==0){
a=Serial.read()-48;
Serial.print("1.number:");
Serial.println(a,DEC);
}
if(Serial.available() > 0 && a!=0);{
b=Serial.read()-48;
Serial.print("2.number:");
Serial.println(b,DEC);
}
if(a!=0 && b!=0){
erg=a*b;
Serial.print("answer:");
Serial.println(erg,DEC);
}
}
but if i start the serial monitor now it only repeats 2.number:-49
The semicolon in this line is wrong, it causes the if to be ignored and the block always executes.
if(Serial.available() > 0 && a!=0);{
Remove the ; and see if it works.
system
3
now it works if i write the 2. number very fast after the first one but if i wait a little bit it says i have typed the 1. again
1.number:4
1.number:2
1.number:1
1.number:1
1.number:2
2.number:3
answer:6
1.number:5
1.number:2
1.number:2
system
4
Strange. That doesn't happen to me. Did you change anything other than removing the ';' from the second if?
Why do you check if a or b == 0 ?
That are valid integers so the challenge is to write code that could multiply those values too!
hint: use a non digit char e.g. space as separator