Go Down

Topic: strtod() call causes arduino to reset (Read 2 times) previous topic - next topic

mellery

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: [Select]
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;
}

mem

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


mellery

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.


mem

#3
Mar 06, 2008, 09:23 pm Last Edit: Mar 06, 2008, 09:29 pm by mem Reason: 1
The strtod function works on my arduino using verion 010

Code: [Select]

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

nkcelectronics

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: [Select]
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;

mellery

shouldn't

return strtod("1.0", NULL);  

work then?  It's resetting my board

nkcelectronics

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.

kg4wsv

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


nkcelectronics

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

mellery

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

kg4wsv

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


mellery

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?

nkcelectronics

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"?

mellery

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)

mellery

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.

Go Up