Convert string to long integer on Arduino

I need to convert a string to a long integer on the Arduino.

For example:

I want to convert the string "1600" to an integer equal to 1600.

How could I do that in C++ in the Arduino?

Although I know its not correct, I tried something like the following: intDist2Move = int(strDist2Move);

Of course, that did not work but you can see where I am going with it.

Thanks for the help.

Coby.

long lint = atol("1600");


Rob

Rob,

Thanks for your help.
I tried your suggestion and did get an error.
Perhaps, it has something to do with the way I am declaring it in the beginning?
I am still a rookie with C++.

Here is my code:
// Declaration of string
String strSteps = String();

strSteps = "1600";
long steps2move = atol(strSteps);

Here is the error I get:
error: cannot convert 'String' to 'const char*' for argument '1' to 'long int atol(const char*)'

Thanks, again.
Coby.

atol() and kin cannot take String. They need null-terminated character strings.

Thanks, I am making a little headway, but I am stuck again.

Here's my entire code:
Compiler stops at "charSteps[ ] = strSteps;" with the following error:
expected primary-expression before ']' token

int dirPin = 4;
int stepperPin= 3;
int sleepPin=7;
String strSteps = "";
char charSteps[] = "";

#define INLENGTH 16
#define INTERMINATOR 13
char inString[INLENGTH+1];
int inCount;
long steps2move;
boolean blnDir2move;


void setup () {
pinMode (dirPin, OUTPUT);
pinMode (stepperPin, OUTPUT);
pinMode(sleepPin,OUTPUT);
Serial.begin(9600);
Serial.flush();
}


// Wire Connections --------------------------------
// 4-Wire From Motor (CA/XX & CB/YY)
// Arduino o3 to Step @ EasyDriver
// Arduino o4 to Dir @ EasyDriver
// Arduino o7 to Sleep @ EasyDriver
// Gnd&Pwr @ EasyDriver to Battery -/+ Respectively

// Software Logic ----------------------------------
// Arduino o3 & o4 are the only items needed to interact with
// Send serial message


//void loop() {
////step(false,160);
////delay(500);
//  if (Serial.available() > 0) {
//    delay(25);
    
void loop() {
  if (Serial.available() > 0){
      delay(75);
      inCount = 0;
      do {
        inString[inCount] = Serial.read();       // get it
        if (inString [inCount] == INTERMINATOR) break;{
          (++inCount < INLENGTH);
          inString[inCount] = 0;  // null terminate the string
        }
      }
      while (Serial.available());             // wait for input
      
      if (inString[0] == '0') { // TURRET MOVE COMMAND
           steps2move = 0;
           for (int i=2;i < inCount;i++){
               strSteps += char(inString[i]);
           }
          
           charSteps[ ] = strSteps;
           long steps2move = atol(charSteps);
           
            if (inString[1] == '0'){
              blnDir2move = true;
            }
            else{
              blnDir2move = false;
            }

            Serial.println(blnDir2move);
           step(blnDir2move,100);
      }
      Serial.flush();
    }
}


      


void step(boolean dir,int steps){
digitalWrite(sleepPin,HIGH);
digitalWrite(dirPin,dir);
//delay(50);


  for(int i=0;i < steps;i++){
  digitalWrite(stepperPin, LOW);
  digitalWrite(stepperPin, HIGH);
  delayMicroseconds(3000);
  }
  
digitalWrite(sleepPin,LOW);
}

Thanks Rob!
Your input got me pointed in the right direction and I have now fixed my problem and moved on.

Used char type: charSteps[x]
I filled the char with a loop, then assigned it using atol as you said.

Here is my final code posted if anyone needs the same example:

#define FLXSTR 16
#define INLENGTH 16
#define INTERMINATOR 13

int dirPin = 4;
int stepperPin= 3;
int sleepPin=7;
String strSteps = "";
char charSteps[FLXSTR];


char inString[INLENGTH+1];
int inCount;
long steps2move;
boolean blnDir2move;


void setup () {
pinMode (dirPin, OUTPUT);
pinMode (stepperPin, OUTPUT);
pinMode(sleepPin,OUTPUT);
Serial.begin(9600);
Serial.flush();
}


// Wire Connections --------------------------------
// 4-Wire From Motor (CA/XX & CB/YY)
// Arduino o3 to Step @ EasyDriver
// Arduino o4 to Dir @ EasyDriver
// Arduino o7 to Sleep @ EasyDriver
// Gnd&Pwr @ EasyDriver to Battery -/+ Respectively

// Software Logic ----------------------------------
// Arduino o3 & o4 are the only items needed to interact with
// Send serial message


//void loop() {
////step(false,160);
////delay(500);
//  if (Serial.available() > 0) {
//    delay(25);
    
void loop() {
  if (Serial.available() > 0){
      delay(75);
      inCount = 0;
      do {
        inString[inCount] = Serial.read();       // get it
        if (inString [inCount] == INTERMINATOR) break;{
          (++inCount < INLENGTH);
          inString[inCount] = 0;  // null terminate the string
        }
      }
      while (Serial.available());             // wait for input
      
      if (inString[0] == '0') { // TURRET MOVE COMMAND
           steps2move = 0;
           
           int x = 0;
           for (int i=2;i < inCount;i++){
               charSteps[x] = char(inString[i]);
               x += 1;
           }
          
           
           long steps2move = atol(charSteps);

           
            if (inString[1] == '0'){
              blnDir2move = true;
            }
            else{
              blnDir2move = false;
            }

           step(blnDir2move,steps2move);
      }
      Serial.flush();
    }
}


      


void step(boolean dir,int steps){
digitalWrite(sleepPin,HIGH);
digitalWrite(dirPin,dir);
//delay(50);


  for(int i=0;i < steps;i++){
  digitalWrite(stepperPin, LOW);
  digitalWrite(stepperPin, HIGH);
  delayMicroseconds(3000);
  }
  
digitalWrite(sleepPin,LOW);
Serial.print(1); //Confirm Move Completed
delay(50);
}

Why not just use the toInt function, like this?

String strSteps = String();
strSteps = "1600";
long steps2move = strSteps.toInt();

Also, if you're using the string objects, you might want to copy these 3 files into your hardware/arduino/cores/arduino directory. They fix many bugs in the string class.

http://code.google.com/p/arduino/issues/detail?id=468

Here is an easier way, look up toCharArray
zhrs is the input string , [] is the array size (number of characters in string +1)
dhrs is the int output.

char tarray[3];
zhrs.toCharArray(tarray, sizeof(tarray));
dhrs = atoi(tarray);

Rock

I want to know how Arduino handles floats as the ATmega ?And how to print the sensors to serial monitor?

I want to know how Arduino handles floats as the ATmega ?

You want to explain this question a little better? And, perhaps explain the relevance to the thread you are posting in.

And how to print the sensors to serial monitor?

You can't. Sensors are hardware. They can't be printed. Unless you have a rep-rap machine.

Float numbers work fine. How Arduino handles them is pretty much like any other number, just put a variable in your code and give it "float" type.

As for printing to the Serial Monitor, just use Serial.print() like you would with an int or any other variable.

How Arduino actually handles floats could be answered in terms Arduino core code, avr-libc library code, the build process as implemented by Compiler.java, internal gcc compiler details, numerical formats (mantissa, exponent, normalization, rounding strategies, etc), binary data representations, logic circuitry, transistor-level CMOS logic implementation, analog characteristics of digital logic circuitry, propagation of electrons and changes in their energy states, electric and magnetic fields, semiconductor material physics, sub-atomic particles, and as-yet-to-be-discovered scientific understanding of matter and energy (eg, "grand unified theory"). But if you're at a beginner level, where you're not sure how to print a float (exactly the same as any other type of variable), learning exactly how Arduino handles floats would only distract you from learning the most basic skills to simply make use of them in your sketch.