how to convert data from serial.read() to int???
That depends on what you transferred over the serial interface (in what format the int got transferred). You can send it in decimal ASCII, hexadecimal ASCII, binary, BCD (binary coded decimal) and many other formats. The most common used formats are binary and decimal ASCII, which one did you choose?
nmaolana:
how to convert data from serial.read() to int???
Example in some simple servo code.
//zoomkat 7-30-10 serial servo test
//type servo position 0 to 180 in serial monitor
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
myservo.write(n);
readString="";
}
}
You probably want Serial.parseInt(). Check the reference for details.
ASCII is ASCII, there is no decimal ASCII or hexadecimal ASCII.
ASCII is ASCII, there is no decimal ASCII or hexadecimal ASCII.
ASCII is ASCII but an integer in ASCII can be transferred in different formats. "247" is usually the decimal notation but it could be octal also and it may be written as "F7" which is then hexadecimal. Maybe you as native speaker can build a better sounding sentence for this.
My apologies pylon. You are right it's a language thing. The format is ASCII, the base is 8,10,16 etc.
in Serial monitor I choose newline, data can show but if I choose no line ending data wouldn't show....
any body can tell me what is differences???
Because I cant control the program if in serial monitor i must choose newline ...
in Serial monitor I choose newline, data can show but if I choose no line ending data wouldn't show....
any body can tell me what is differences???
The difference is, as should be obvious, the line ending that is sent after your data.
Your code, that you couldn't be bothered to post, clearly expects some kind of packet terminator.