Pages: [1] 2   Go Down
Author Topic: strtod() call causes arduino to reset  (Read 1991 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Code:
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 Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The strtod function works on my arduino using verion 010

Code:
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 Offline
Sr. Member
****
Karma: 0
Posts: 452
Freeduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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


Code:
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 Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

shouldn't

return strtod("1.0", NULL);  

work then?  It's resetting my board
Logged

USA
Offline Offline
Sr. Member
****
Karma: 0
Posts: 452
Freeduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Sr. Member
****
Karma: 0
Posts: 452
Freeduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, that is why adding the trailing '\0' is always a safe programming rule in C
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Faraday Member
**
Karma: 7
Posts: 2526
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Sr. Member
****
Karma: 0
Posts: 452
Freeduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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 Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: