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?
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.
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="";
}
}
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);
}
}
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) >