Loading...
Pages: [1]   Go Down
Author Topic: Type 2 numbers in serial mon. and it should give me the multiplication of both  (Read 340 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi i have to make this programm for school but i dont get how to do it.my code looks like this
Code:
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
Logged

Online Online
Sr. Member
****
Karma: 23
Posts: 444
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The semicolon in this line is wrong, it causes the if to be ignored and the block always executes.
Code:
if(Serial.available() > 0 && a!=0);{
Remove the ; and see if it works.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Code:
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
Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3118
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Strange. That doesn't happen to me. Did you change anything other than removing the ';' from the second if?
Logged

Netherlands
Offline Offline
Tesla Member
***
Karma: 87
Posts: 9368
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Pages: [1]   Go Up
Print
 
Jump to: