0
votes

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:

  1. How do I convert char array 'num' to an int so that I can see the correct output in the Serial Monitor
  2. 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...

1
"So, not only does it think '1' is '49'" : ascii '1' IS 49Mitch Wheat
@MitchWheat So does that mean my code IS parsing the two number correctly, it's just not outputting in the same language that I inputted?Gabriel Ruoff
"it's just not outputting in the same language that I inputted?" - it has nothing to do with language: it's representation. chars are encoded intsMitch Wheat
@MitchWheat So how do I go about converting my char array to an int? I tried doing Serial.println(atoi(num)); because I thought that atoi converts char arrays to ints, but it says: "invalid conversion from 'int' to 'const char*' [-fpermissive]" when I try to upload it to the board, which makes it seem like atoi is supposed to convert an int to a char array....?Gabriel Ruoff
hint: atoi converts a string to an integer...cplusplus.com/reference/cstdlib/atoiMitch Wheat

1 Answers

0
votes

I don't have Arduino installed in the PC I 'm using right now, so I hope I get this right...

For one thing, your code is way more complex than necessary You can simplify

num = strtok(message, ":");
String numstr(num);
Serial.println(numstr);

num = strtok(NULL, ":");
String numstr2(num);
Serial.println(numstr);

as

Serial.println(strtok(input.c_str(), ":"));
Serial.println(strtok(NULL, ":"));

Also,

String input = String(Serial.Serial.readStringUntil((char)13));

Will probably be closer to what you want (assuming your input strings are terminated with CR)

if (Serial.available()) {
   String input = String(Serial.Serial.readStringUntil((char)13));
   Serial.println("original: " + input);
   Serial.println(atoi(input.c_str()) ;
   Serial.println(strtok(input.c_str(), ":"));
   Serial.println(strtok(NULL, ":"));
}

Will hopefully do what you want.