Go Down

Topic: Help! I get a error (Read 221 times) previous topic - next topic

HankChen

i'm a newbie ,i want to make a Computer with using arduino.
but i get a error,
this is the code:
Code: [Select]

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

long number1;
long number2;
char symbol;
long ans;

void setup() {
  Serial.begin(9600);
  lcd.begin(16,2);
  Serial.println("Welcome to the Number Computer V1");
}

void loop() {
  long _in;
  String Number = "";
  if(Serial.available()) {
    _in = Serial.read;
    Number += _in;
    if(_in == '/n') {
      number1 = Number.substring(0,6);
      number2 = Number.substring(7);
      symbol = Number.charAt(6);
      switch(symbol) {
        case '+' :
          ans = number1 + number2;
          break;
        case '-' :
          ans = number1 - number2;
          break;
        case '*' :
          ans = number1 * number2;
          break;
        case '/' :
          ans = number1 / number2;
          break;
      }
      Serial.print(number1);
      Serial.print(symbol);
      Serial.println(number2);
      Serial.print("Your ans: ");
      Serial.println(ans);
      lcd.setCursor(0,0);
      lcd.print(number1);
      lcd.print(symbol);
      lcd.print(number2);
      lcd.setCursor(0,1);
      lcd.print("Ans:");
      lcd.setCursor(4,1);
      lcd.print(ans);
    }
  }
}


i get this error:
sketch_apr19a:21: error: argument of type 'int (HardwareSerial::)()' does not match 'long int'
sketch_apr19a:24: error: cannot convert 'String' to 'long int' in assignment
sketch_apr19a:25: error: cannot convert 'String' to 'long int' in assignment

who can help me?

PaulS

Code: [Select]
    if(_in == '/n') {
Are you certain that slash is leaning the right way? I'm certain that it isn't.

Code: [Select]
    _in = Serial.read;
Function calls require ().

Serial.read() returns a byte. Why are you storing that byte in a long?

Code: [Select]
      number1 = Number.substring(0,6);
      number2 = Number.substring(7);

The compiler is right. You can't do this. A substring can only be stored in a String.

Peter_n

Tell us what you want to do, we can tell how to do that.
What is the serial data ? A number ?
There is a special function for that : http://arduino.cc/en/Reference/ParseInt

The Serial.read() is a function, you must use the ()
The substring(..) operator on a string returns a string (a sub string), you can not assign that to a number that way.

PaulS replied when I was typing this, he also noticed the /n instead of \n
Is millis() a 'he' or a 'she' ? It can keep you waiting, but it is always on time.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy