I'm working on a project with this code:
#include <string.h>
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available()) {
String input = String(Serial.read());
Serial.println("original: " + input);
char inputarray[100];
input.toCharArray(inputarray, 100);
char *message = inputarray;
char* num;
num = strtok(message, ":");
String numstr(num);
Serial.println(numstr);
num = strtok(NULL, ":");
String numstr2(num);
Serial.println(numstr);
}
delay(500);
}
It's supposed to read input from the Serial port and parse it so that an input of "10:15:20" would return 3 outputs, "10", "15", and "20".
My issue is that when I send the string "10:15:20" in the Serial terminal, it returns this:
original: 49
49
49
original: 48
48
48
original: 58
58
58
original: 49
49
49
original: 53
53
53
original: 58
58
58
original: 50
50
50
original: 48
48
48
Now, I'm really confused. I tried entering a single number, say, '1' into the serial, and it returns this:
original: 49
49
49
So, not only does it think '1' is '49', when I enter something like "10:15:20" it loops the program 8 times while using that statement as 'input'.
Why is this happening? Is there something wrong with my code, or is it something wrong with the arduino/arduino software?
Any Help is greatly appreciated!!!!
-Gabriel
EDIT: Thanks to Mitch Wheat for bringing to my attention that the serial terminal was outputting my 'numstr' variable as ASCII.... Now I'm just wondering:
- How do I convert char array 'num' to an int so that I can see the correct output in the Serial Monitor
- It doesn't seem as if I'm getting the right output in my serial monitor.... "10:15" doesn't seem to return two statements "10" and "15"...
I also tried to print the output using atoi() like this:
num = strtok(message, ":");
Serial.println(atoi(num));
But to no avail... (I'm not sure if I was using atoi correctly, so this may be irrelevant but I'll add it anways.
BIG EDIT:
Ok. So, thanks to Alphonsos_Pangas, I was able to get it to print correctly and verified that my strtok() was working correctly.
Now, however, my issue is when I try to convert the string that I got from strtok(); to an int, I get an error. I've been using this code:
#include <string.h>
String data[4];
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available()) {
String input = String(Serial.readStringUntil((char)13));
Serial.println("original: " + input);
//Serial.println(atoi(input.c_str()));
// Serial.println(strtok(input.c_str(), ":"));
data[0] = ((char*)strtok((char*)input.c_str(), ":"));
Serial.println(data[0]);
int testint = data[0].toInt;
data[1] = (strtok(NULL, ":"));
Serial.println(data[1]);
data[2] = (strtok(NULL, ":"));
Serial.println(data[2]);
data[3] = (strtok(NULL, ":"));
Serial.println(data[3]);
}
delay(500);
Serial.println("end");
}
It all works correctly except this line:
int testint = data[0].toInt;
It's supposed to convert the String (The number that I put in first) to an int, but when I try to compile it it says this:
cannot convert 'String::toInt' from type 'long int (String::)()const' to type 'int'
I'm not sure what I'm doing wrong...