Conversion of string to int

Hi everyone! Thank you for your help!

I can’t understand how to convert a string to an int value.

This

pos = stringa.toInt();

returns 0 even if stringa contains “180” (actually

Serial.println(stringa); 

prints “180” on the serial monitor).

Here is the complete code.

#include <Servo.h>

Servo myservo; 

int pos = 0; 
int servoPin = 3;
String stringa;

void setup() {

Serial.begin(9600);

myservo.attach(servoPin); 
myservo.write(0);
}

void loop() {

while (Serial.available()) 
    {
    Serial.println("Available");
    Serial.println(Serial.readString());
    stringa = Serial.readString();
    Serial.println(stringa);    
    pos = stringa.toInt();  
    Serial.println(pos);
    myservo.write(pos);
  }
}

1. Use atoi() function to convert a string into integer.

2. Example (I have used blocking instruction with 1-sec default timeout option.)
(1) Upload the following sketch.

char myData[20];

void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  byte n = Serial.available();
  if( n!=0)
  {
    byte m = Serial.readBytesUntil('\n', myData, 20);
    myData[m] = '\0';  //insert null charcater
    int x = atoi(myData); //converts string to int
    Serial.println(x);    //shows 180
  }
}

(2) Bring the Serail Monitor (Fig-1) with Bd = 9600 and 'Line ending tab' at 'No line ending' option.
(3) Enter 180 in the InputBox of the Serial Monitor (Fig-1) and click on the Send button.


Figure-1:

(4) Check that 180 has appeared on the OutputBox of Serial Monitor (Fig-2).
smy.png
Figure-2:

smy.png

    Serial.println(Serial.readString());
    stringa = Serial.readString();

You are reading the String twice. The first one removes it from the buffer so the second one fails

UKHeliBob:     Serial.println(Serial.readString());     stringa = Serial.readString();

You are reading the String twice. The first one removes it from the buffer so the second one fails

Ok, thank you! Actually that was the problem; now it works!

GolamMostafa:
1. Use atoi() function to convert a string into integer.

2. Example (I have used blocking instruction with 1-sec default timeout option.)
(1) Upload the following sketch.

char myData[20];

void setup()
{
 Serial.begin(9600);
}

void loop()
{
 byte n = Serial.available();
 if( n!=0)
 {
   byte m = Serial.readBytesUntil('\n', myData, 20);
   myData[m] = '\0';  //insert null charcater
   int x = atoi(myData); //converts string to int
   Serial.println(x);    //shows 180
 }
}



**(2)** Bring the Serail Monitor (Fig-1) with Bd = 9600 and 'Line ending tab' at 'No line ending' option.
**(3)** Enter 180 in the InputBox of the Serial Monitor (Fig-1) and click on the Send button.
![SerialMonitorX.png|746x474](upload://ekt4vqUAiGFuKMULT7uTZHW7sHg.png)
Figure-1:

**(4)** Check that 180 has appeared on the OutputBox of Serial Monitor (Fig-2).
![smy.png|647x482](upload://4LDFNa1hd0gCXgjSDEyuNxSAh8S.png)
Figure-2:

I'll try any way!

But what about int()?

salventre: But what about int()?

int() is used to cast data types and not convert from one data type to another.

toInt() method is not recommended as it uses String class which causes memory fragmentation in AVR MCUs.

Actually that was the problem

I know. That is why I pointed it out

Danois90: int() is used to cast data types and not convert from one data type to another.

Sorry, what does that mean?

salventre: Sorry, what does that mean?

This is a cast:

float number = 123.54;
int casted_to_int_1 = int(number); //123
int casted_to_int_2 = (int)number; //123
int rounded_to_int = round(number); //124

You cannot cast from a string representation of a number to a numeric value since this operation requires a conversion.