<New> how to convert char to int

I try to use atoi() to convert a char to int, but it returns only singal digit.

For example, if the value read was 560, the first serial.read() call will return '5', the second will return '6', and the third will return '0'.

How to read 560 as an integer?

Thank you!!!!

or you can use a char array to store the values,

something like this...

  char char_input[5];  //char array buffer
  int int_output;
  char_input[0] = '5'; //leftmost digit 
  char_input[1] = '6';
  char_input[2] = '0';
  int_output = atoi(char_input);

ckho:
or you can use a char array to store the values,

something like this...

  char char_input[5];  //char array buffer

int int_output;
  char_input[0] = '5'; //leftmost digit
  char_input[1] = '6';
  char_input[2] = '0';
  int_output = atoi(char_input);

Hi ckho,
Thanks for your reply,
But my char value is from sensor reading, so i cannot define the value of leftmost digit or the rightmost digit.
What should i do?

But my char value is from sensor reading,

What should i do?

Post your code using the code tags </> on the top left of the tool bar so that it looks like this

code should be posted within code tags

XingY:
Hi ckho,
Thanks for your reply,
But my char value is from sensor reading, so i cannot define the value of leftmost digit or the rightmost digit.
What should i do?

Then JoeN's suggestion may be better.
Just use Serial.parseInt() to read the value as int from your sensor.

ckho:
Then JoeN's suggestion may be better.
Just use Serial.parseInt() to read the value as int from your sensor.

I try to use parseInt, but the program cannot run. And here is my program.

</>
#include <SoftwareSerial.h>

char Reading[5];
int Num;

char R;

SoftwareSerial sensor(9, 8); // RX, TX
void setup()
{
Serial.begin(9600);
Serial.println("initializing");
sensor.begin(9600);
Serial.println("Success");

}

void loop() {
if(sensor.available()){
R = sensor.read(); //Read and store the sensor reading into a char R
Num = Serial.parseInt(R);

Serial.println(Num);
delay(300);

}

}

Just now i posted a topic, but i didnt put my code with it.

I really appreciate all the replies, but the problem is still here. When i run my code, the output is always 0(single digit).

Guys, PLEASE help me, need to submit this project next week :cold_sweat:

PLEASE PLEASE PLEASE PLEASE

#include <SoftwareSerial.h>

char Reading;
static char Integer[10];
static uint8_t i;

SoftwareSerial sensor(9, 8); // RX, TX
void setup()
{
  Serial.begin(9600);
  Serial.println("initializing");
  sensor.begin(9600);
  Serial.println("Success");
}

void loop() {
  if (sensor.available()) {
    Reading = sensor.read();
    if(  Reading != '\r' && i <10)
    {
    Integer[i++] = Reading;
   
  }
    else
    {
      Integer[i] = '\0';
      i = 0;
      int Num = atoi(Integer);
      Serial.println(Num);
    }
    } 
  
}

Not sure what you are looking for, but the below is a simple demonstration of capturing a string of characters representing a numeric value, then converting the captured String into an integer.

//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
// 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);
  Serial.println("servo-test"); // so I can keep track of what is loaded
}

void loop() {

  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the String readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured String 
    int n = readString.toInt();  //convert readString into a number
    Serial.println(n); //so you can see the integer
    myservo.write(n);
    readString="";
  } 
}

Are you certain that you have a terminating '\r'? Have you confirmed that the sensor is actually sending data over software serial?

When I change your code to hardware serial, and use the serial monitor with carriage return '\r' as the line ending it works as intended.

#include <SoftwareSerial.h>

char Reading;
static char Integer[10];
static uint8_t i;

SoftwareSerial sensor(9, 8); // RX, TX
void setup()
{
  Serial.begin(9600);
  Serial.println("initializing");
  sensor.begin(9600);
  Serial.println("Success");
}

void loop() {    //change loop to read from serial monitor instead of software serial
  if (Serial.available()) {
    Reading = Serial.read();
    if (  Reading != '\r' && i < 10)
    {
      Integer[i++] = Reading;

    }
    else
    {
      Integer[i] = '\0';
      i = 0;
      int Num = atoi(Integer);
      Serial.println(Num);
    }
  }

}

cattledog:
Are you certain that you have a terminating '\r'? Have you confirmed that the sensor is actually sending data over software serial?

When I change your code to hardware serial, and use the serial monitor with carriage return '\r' as the line ending it works as intended.

#include <SoftwareSerial.h>

char Reading;
static char Integer[10];
static uint8_t i;

SoftwareSerial sensor(9, 8); // RX, TX
void setup()
{
  Serial.begin(9600);
  Serial.println("initializing");
  sensor.begin(9600);
  Serial.println("Success");
}

void loop() {    //change loop to read from serial monitor instead of software serial
  if (Serial.available()) {
    Reading = Serial.read();
    if (  Reading != '\r' && i < 10)
    {
      Integer[i++] = Reading;

}
    else
    {
      Integer[i] = '\0';
      i = 0;
      int Num = atoi(Integer);
      Serial.println(Num);
    }
  }

}

how to change to hardware serial?

how to change to hardware serial?

I was just testing your code, which I think should read the char data and convert it to an integer if the data coming over software serial is as expected. Are you sure of the baud rate 9600? Are the RX/TX connected correctly. They may need to be crossed RX>TX and TX>RX between the Arduino and the sensor.

What is the sensor? Does your arduino have multiple hardware serial connections?

cattledog:
I was just testing your code, which I think should read the char data and convert it to an integer if the data coming over software serial is as expected. Are you sure of the baud rate 9600?

What is the sensor? Does your arduino have multiple hardware serial connections?

I think i FIX it !!!

i changed 10 to 5 then the serial monitor start to show correct output...... < if ( Reading != '\r' && i < 10) >

Thank you for your reply!

You are using the method call incorrectly. Instead use:

void loop() {
if(sensor.available()){
 Num = sensor.parseInt();    
 Serial.println(Num);
 delay(300);
}
}

Moderator: merged topics as they are highly correlated.