String to int Example
i'm beginner c/c++ code i'm working through "examples" given under "learning". 1 of these called 'string int' doesn't seem work if cut , paste compiler. here code
i think wrong should have #include< ctype.h> isdigit work i've tried , still no output serial monitor after entering numbers in. thought ctype library included anyway maybe instring.toint @ fault. please suggest reason why snippet fails run? thank you
code: [select]
/*
string integer conversion
reads serial input string until sees newline, converts
string number if characters digits.
circuit:
no external components needed.
created 29 nov 2010
tom igoe
example code in public domain.
*/
string instring = ""; // string hold input
void setup() {
// open serial communications , wait port open:
serial.begin(9600);
while (!serial) {
; // wait serial port connect. needed leonardo only
}
// send intro:
serial.println("\n\nstring toint():");
serial.println();
}
void loop() {
// read serial input:
while (serial.available() > 0) {
int inchar = serial.read();
if (isdigit(inchar)) {
// convert incoming byte char
// , add string:
instring += (char)inchar;
}
// if newline, print string,
// string's value:
if (inchar == '\n') {
serial.print("value:");
serial.println(instring.toint());
serial.print("string: ");
serial.println(instring);
// clear string new input:
instring = "";
}
}
}
i think wrong should have #include< ctype.h> isdigit work i've tried , still no output serial monitor after entering numbers in. thought ctype library included anyway maybe instring.toint @ fault. please suggest reason why snippet fails run? thank you
what out put , input did give it?
mark
mark
Arduino Forum > Using Arduino > Programming Questions > String to int Example
arduino
Comments
Post a Comment