0
Offline
Newbie
Karma: 0
Posts: 12
Arduino rocks
|
 |
« on: March 06, 2008, 02:16:45 pm » |
Hello, I'm having a problem converting a string to a double (or float), whenever I call strtod() the arduino resets I've also tried calling atof() with the same result. I've posted my function below, if i hardcode it to return a float everything works fine. I'm using ubuntu hardy heron if that matters. Has anyone had this problem? Or how can I troubleshoot this? Thanks double search_string(char key, char instruction[], int string_size) { char temp[10] = ""; for (int i=0; i<string_size; i++) { if (instruction[i] == key) { i++; int k = 0; while (instruction[i] != (' '|NULL)) { temp[k] = instruction[i]; i++; k++; } return strtod(temp, NULL); } } return 0; }
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #1 on: March 06, 2008, 02:43:28 pm » |
how can I troubleshoot this? Serial.print(temp); just before the strtod(temp, NULL); if temp is not what you expect then you can add some more printing to see how your parsing code is working
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 12
Arduino rocks
|
 |
« Reply #2 on: March 06, 2008, 02:54:17 pm » |
Thanks for the reply
Serial.print(temp); prints the string I expect.
Also I tried
return strtod("1.0", NULL);
which also reset the board.
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #3 on: March 06, 2008, 03:23:18 pm » |
The strtod function works on my arduino using verion 010 void loop(){ double d; Serial.print("hello "); d = strtod("1.0", NULL); int i = (int)d ; Serial.print(i); Serial.println(" world"); }
does print: hello 1 world Simplify your sketch to see if you are running out of memory or have some other problem elsewhere
|
|
|
|
« Last Edit: March 06, 2008, 03:29:04 pm by mem »
|
Logged
|
|
|
|
|
USA
Offline
Sr. Member
Karma: 0
Posts: 452
Freeduino rocks
|
 |
« Reply #4 on: March 17, 2008, 03:35:37 pm » |
Hello, I'm having a problem converting a string to a double (or float), whenever I call strtod() the arduino resets I've also tried calling atof() with the same result. I've posted my function below, if i hardcode it to return a float everything works fine. I'm using ubuntu hardy heron if that matters. Has anyone had this problem? Or how can I troubleshoot this? Thanks double search_string(char key, char instruction[], int string_size) { char temp[10] = ""; for (int i=0; i<string_size; i++) { if (instruction[i] == key) { i++; int k = 0; while (instruction[i] != (' '|NULL)) { temp[k] = instruction[i]; i++; k++; } return strtod(temp, NULL); } } return 0; } temp is not being properly terminated with 0 after the while loop. You need to add temp[k] = 0;
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 12
Arduino rocks
|
 |
« Reply #5 on: March 17, 2008, 03:43:10 pm » |
shouldn't
return strtod("1.0", NULL);
work then? It's resetting my board
|
|
|
|
|
Logged
|
|
|
|
|
USA
Offline
Sr. Member
Karma: 0
Posts: 452
Freeduino rocks
|
 |
« Reply #6 on: March 19, 2008, 10:49:56 pm » |
shouldn't
return strtod("1.0", NULL);
work then? It's resetting my board return strtod("1.0", NULL); works just fine. Maybe you have a faulty atmega168? Post the complete code so I can try... all the examples posted so far work perfect... no reset.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Faraday Member
Karma: 6
Posts: 2504
|
 |
« Reply #7 on: March 20, 2008, 06:53:57 am » |
Depending on the rest of your program, you may be simply using up the last bit of RAM with the strod() call. When you use too much RAM, it can wrap around to address 0 again and this has been known to cause resets.
-j
|
|
|
|
|
Logged
|
|
|
|
|
USA
Offline
Sr. Member
Karma: 0
Posts: 452
Freeduino rocks
|
 |
« Reply #8 on: March 20, 2008, 03:42:10 pm » |
Yes, that is why adding the trailing '\0' is always a safe programming rule in C
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 12
Arduino rocks
|
 |
« Reply #9 on: March 23, 2008, 08:52:47 pm » |
void setup() { Serial.begin(9600); Serial.println("start"); }
void loop() { strtod("1.0", NULL); }
--- That code causes my board to reset. Is it possible the string.c libraries in Ubuntu Hardy Heron are bad? I know it worked on gutsy, but then I didn't run this code for a while and I updated in the mean time. To see if it is my compiler is there a hex file generated that I could attach here? Thanks
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Faraday Member
Karma: 6
Posts: 2504
|
 |
« Reply #10 on: March 24, 2008, 07:39:28 am » |
Is it possible the string.c libraries in Ubuntu Hardy Heron are bad? Well, yeah, possible, but not relevant to this discussion - you're using the string functions that come with Arduino, not those that come with your OS. -j
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 12
Arduino rocks
|
 |
« Reply #11 on: March 24, 2008, 08:20:31 am » |
rats, in that case is there a hex file arduino creates I could upload here for someone to try to see if it is my board or something on the compiler side?
|
|
|
|
|
Logged
|
|
|
|
|
USA
Offline
Sr. Member
Karma: 0
Posts: 452
Freeduino rocks
|
 |
« Reply #12 on: March 24, 2008, 04:35:53 pm » |
void setup() { Serial.begin(9600); Serial.println("start"); }
void loop() { strtod("1.0", NULL); }
--- That code causes my board to reset. Is it possible the string.c libraries in Ubuntu Hardy Heron are bad? I know it worked on gutsy, but then I didn't run this code for a while and I updated in the mean time. To see if it is my compiler is there a hex file generated that I could attach here? Thanks Please, what do you mean by "reset"?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 12
Arduino rocks
|
 |
« Reply #13 on: March 25, 2008, 09:16:06 am » |
Reset as in reseting the atmel, same result as pushing the reset button on the board. If the code was
void setup() { Serial.begin(9600); Serial.println("start"); }
void loop() { strtod("1.0", NULL); Serial.println("finished"); }
finished would never be print, and start would be printed over and over (sometimes truncated or with unprintable characters added on)
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 12
Arduino rocks
|
 |
« Reply #14 on: March 28, 2008, 09:14:10 am » |
I can confirm theres a problem with the compiler that ships with hardy heron, I tried my code compiled with gutsy gibbon and it works fine. I think I need to file a bug report for ubuntu, not sure for which package though.
|
|
|
|
|
Logged
|
|
|
|
|
|