Converting String to int

Hello everyone, for the below code. The function (EQUAL()) would return a string of number. How do i convert that string to int, so that the number can be used for comparison. I tried coming out with the code however i got an error message . Thanks for the help (:

Arduino: 1.6.3 (Windows 8.1), Board: "Arduino Uno"

TESTING.ino: In function 'void loop()':

TESTING.ino:28:24: error: converting to 'const String' from initializer list would use explicit constructor 'String::String(int, unsigned char)'

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:222:0,

from TESTING.ino:4:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:147:16: error: initializing argument 1 of 'unsigned char String::operator>(const String&) const'

unsigned char operator > (const String &rhs) const;

^

Error compiling.

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

CODES

#include <Servo.h>

Servo myservoE;
String APAltitude, APAltitude1;
int CodeIn;
int elevator=90;
String setAltitude;
int currentAltitude=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
String setAltitude;
myservoE.attach(3);
}

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

myservoE.write(elevator);
if (Serial.available () > 0)
{
CodeIn=getChar();
if (CodeIn == '=')
{
setAltitude=EQUAL();
setAltitude.toInt();
if (setAltitude > currentAltitude)
{
elevator--;
myservoE.write(elevator);
}
}
}
}

char getChar()// The serial buffer routine to get a character
{
while(Serial.available() == 0);// wait for data
return((char)Serial.read());// Thanks Doug
}// end of getchar Routine.

String EQUAL()
{
CodeIn=getchar();
switch (CodeIn)
{
case 'b' : //getting the Altitude information from FS9
delay(11);
APAltitude = "";
APAltitude += char(Serial.read());
APAltitude += char(Serial.read());
APAltitude += char(Serial.read());
APAltitude += char(Serial.read());
APAltitude += char(Serial.read());
APAltitude +=(" ");
return APAltitude;
}
}

String EQUAL()
{
  CodeIn=getchar();
  switch (CodeIn)
  {
    case 'b' :  //getting the Altitude information from FS9
    delay(11);
    APAltitude = "";
    APAltitude += char(Serial.read());
    APAltitude += char(Serial.read());
    APAltitude += char(Serial.read());
    APAltitude += char(Serial.read());
    APAltitude += char(Serial.read());
    APAltitude +=(" ");
    return APAltitude;
  }
}

It doesn't matter that there may be no data to read? Oh, yes, it does!

Why does that function return a global variable?

setAltitude.toInt();

What is the purpose of calling this method() if you throw away the value returned?

if (setAltitude > currentAltitude)

Does it really make sense to compare a String instance to an int?

The purpose of the return is to return the value that is stored is APAltitude. I am trying to convert the return value (APAltitude) cause it is current in string. So when the APAltitude is change to int, i would compare the int value to an preset value. setAltitiude is just a variable to store the APAltitude. sorry what do you meant by global variable?
Thanks

The purpose of the return is to return the value that is stored is APAltitude.

The whole reason for making APAltitude global was so that you would NOT have to pass it around.

So when the APAltitude is change to int

APAltitude, or setAltitude is NOT changed to an int. The value in the String instance is CONVERTED to an int (actually, a long) and returned by the method.

   long howHighShouldWeBe = setAltitude.toInt();
   if(howHighShouldWeBe > currentAltitude)

You still need to address the fact that you read data from the serial port incorrectly. That one character has arrived is no guarantee that 5 more have arrived, too.

Hey Thanks for the reply Pauls. Kinda of a arduino newbie. can i know how should i ensure that all 5 character have arrived? is it possible to string all 5 character into one whole int(long) ? Thanks

I just Googled "Arduino String to int" and got 433,000 hits. Surely one of those is going to answer your questions. As you hang around here more, you'll find people tend to provide the most help to those posters who clearly demonstrate that they have exhausted all avenues before making a post. I'm not sure that's the case here. From your questions following the answers that have been supplied it's pretty clear you need to spend some time with a beginning C book, especially one geared to the Arduino IDE. Again, Google is your friend and you should be able to find one that appeals to you. Spend the rest of the week with the tutorial, reread what's already been said here, and see if you don't already have the answers to your questions.

Simple servo test code that demonstrates capturing bytes and converting them into an integer. Can you post an example of what is being sent to the arduino?

// zoomkat 7-30-10 serial servo test
// type servo position 0 to 180 in serial monitor
// for writeMicroseconds, use a value like 1500
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
}

void loop() {

  while (Serial.available()) {

    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString
      delay(3);
    } 
  }

  if (readString.length() >0) {
    Serial.println(readString);
    int n = readString.toInt();
    Serial.println(n);
    myservo.writeMicroseconds(n);
    //myservo.write(n);
    readString="";
  } 
}

Zoomcat has given you one way to convert a String object to an int. I removed the Servo objects and calls and his code compiled to 4864 bytes. If you replace the String object with a char array, and use the atoi() function, you can write the program in 3140 bytes, a fairly significant savings of memory. As a general rule, String objects are convenient, but come with some fairly heavy memory costs.

Kinda of a arduino newbie. can i know how should i ensure that all 5 character have arrived?

If

while(Serial.available() == 0);// wait for data

ends when there is one or more characters to read, what do you suppose

while(Serial.available() < 5);// wait for data

would do?