Go Down

Topic: trying to split a char (Read 1 time) previous topic - next topic

fbriggs4

I have tried to split the char as if it were an array but I get an error when I try to compile. For example if I do this it throws as error.

Code: [Select]

char time;
int hour;

hour = time[0];


I am not sure how to get grab the data other than using a char so I have been stuck using that. Can I convert a char to a char array and then use the suggested method?


PaulS

Quote
I am not sure how to get grab the data other than using a char

It would help if you'd use standard terminology as reflected in the names of the methods/functions you are using. There is no class that has a grab() method, that I know of, and there is no grab() function on the Arduino.

You can only read characters from the serial port. There are methods in the HardwareSerial class that allow you to read more than one character, but, underneath, they read one character at a time. You have to store those characters in an array if you want to deal with more than one of them.

Trying to use array notation on a scalar variable WILL cause the compiler to laugh at you.

fbriggs4

#7
Dec 11, 2012, 03:50 pm Last Edit: Dec 11, 2012, 03:53 pm by fbriggs4 Reason: 1
PaulS- fair enough, I will try to be more accurate with my terminology. I read each incoming byte on a software serial port and put it into a char array. I split the data using strtok() and use atoi, atol and atof to populate the variables. All of this is working well and I have successfully split all the data based on the comma delimiter and populated all my variables. If all I needed was to print the time variable I would be good. However, I need to do some math with the hours, minutes and seconds and to the best of my knowledge this requires an int. I did not use atol because if the time is 000517 it would populate the time as 517 and drop all the zeros. If it drops the zeros I am not sure what index number represents HhMmSs. Because of this I decided to put the time variable as a char which keeps the zero(s) as a place holder. Now I need to somehow split the char and make int hours, int minutes, int seconds.

Code: [Select]

valPosition = strtok(GPGGAC, delimiters);
 i = 0;
 while(valPosition != NULL || i < 10){
   i++;  
   valPosition = strtok(NULL, delimiters); //Here we pass in a NULL value, which tells strtok to continue working with the previous string
   if(i==1){
     //time = atol(valPosition); //drops the zeros so I don't know how to split it like this
      time = *valPosition; //saves as a char but I dont know how to split it and change to int
     Serial.print(F("time = "));
     Serial.println(time);
   }
 



PaulS

Quote
time = *valPosition; //saves as a char but I dont know how to split it and change to int

valPosition is a pointer. Pointers and arrays are so closely related that you can, for the most part, says that they are the same thing. For your purpose, valPosition and someArray[6] are the same thing.

Code: [Select]
char hrStg[3], mnStg[3], scStg[3];

hrStg[0] = valPosition[0];
hrStg[1] = valPosition[1];
hrStg[2] = '\0';

mnStg[0] = valPosition[2];
mnStg[1] = valPosition[3];
mnStg[2] = '\0';

seStg[0] = valPosition[4];
seStg[1] = valPosition[5];
seStg[2] = '\0';

int hour = atoi(hrStg);
int min = atoi(mnStg);
int sec = atoi(seStg);

PeterH


I need to do some math with the hours, minutes and seconds and to the best of my knowledge this requires an int.


Guix has already given you some code to convert a string containing hhmmss format into integer hours, minutes and seconds.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up