Pages: [1]   Go Down
Author Topic: Error function Arduino nano  (Read 665 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all smiley

I Have this function:
(Dividing the number of seconds  ( TT long variable) into hours, minutes and seconds)

//*********** CALCULO DEL TIEMPO TOTAL  ******************************************

String TiempoTotal (unsigned long Cantidad){
     
      unsigned long TT = Cantidad * 10;           
     
      unsigned long Remanente1 = (TT % 3600);
      unsigned long H = (TT - Remanente1)/3600;
      unsigned long S = Remanente1 % 60;
      unsigned long M = ( Remanente1 - S) / 60;
      String Hs= String(H);
      if (H < 10) Hs= "0" + Hs;
      String Min = String(M);
      if (M < 10) Min= "0" + Min;
      String Seg = String(S);
      if (S < 10) Seg= "0" + Seg;
     
     
     
      return  " "  + Hs + "Hs. " + Min + "m. "+ Seg + "s.    ";
     
   }


When I invoke with :  lcd.print ( TiempoTotal (Disparos)); (ex Disparos=100)  this function break mi Arduino nano and make it crazy.... smiley-confuse
What am I doing wrong?

Thanks in advance  OSO from Argentina!  smiley

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46128
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What am I doing wrong?
Using the String class.
Logged

Quebec
Offline Offline
Jr. Member
**
Karma: 3
Posts: 78
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This works on an arduino 2009, the rest of the sketch I used to test is:

Code:
void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
//  lcd.print("hello, world!");
  lcd.print ( TiempoTotal (1000));
}


void loop() {
}
« Last Edit: February 23, 2013, 09:36:44 am by semicolo » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What am I doing wrong?
Using the String class.
Hi...
How make it Please?
Thanks.
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
What am I doing wrong?
Using the String class.
Hi...
How make it Please?
Thanks.

C-style strings and sprintf()
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This works on an arduino 2009, the rest of the sketch I used to test is:

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
//  lcd.print("hello, world!");
  lcd.print ( TiempoTotal (1000));
}


void loop() {
}



Hello Semicolo, thanks for your response.

Here a sample of  program :

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
// initialize the library with the numbers of the interface pins

void setup() {
   lcd.begin(16, 2);
  
}

void loop() {
  
 lcd.clear();
 lcd.print ( TiempoTotal (1000));
 delay(1000);
}

String TiempoTotal (unsigned long Cantidad){
    
      unsigned long TT = Cantidad;          
    
      unsigned long Remanente1 = (TT % 3600);
      unsigned long H = (TT - Remanente1)/3600;
      unsigned long S = Remanente1 % 60;
      unsigned long M = ( Remanente1 - S) / 60;
      String Hs= String(H);
      if (H < 10) Hs= "0" + Hs;
      String Min = String(M);
      if (M < 10) Min= "0" + Min;
      String Seg = String(S);
      if (S < 10) Seg= "0" + Seg;
    
      return  " "  + Hs + "Hs. " + Min + "m. "+ Seg + "s.    ";
    
   }


The display lcd show some strange signs, then shows nothing more ...
will display problem ?
May be because I use pins 6 and 7? (for others applications works fine)
Can you help me?

Thanks!

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46128
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why not move the lcd.print() to TiempoTotal()? Then, TiempoTotal() does not need to return a value. Use multiple lcd.print() calls in TiempoTotal(), so you don't need to use any Strings.
Logged

Quebec
Offline Offline
Jr. Member
**
Karma: 3
Posts: 78
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does the liquidcrystal->hello_world example work?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@Oso57: How to use this forum


Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please note that, at present, the String library has bugs as discussed here and here.

In particular, the dynamic memory allocation used by the String class may fail and cause random crashes.

I recommend reworking your code to manage without String. Use C-style strings instead (strcpy, strcat, strcmp, etc.).

Alternatively, install the fix described here:
  http://arduino.cc/forum/index.php/topic,145765
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all...
I've finally solved the problem, thanks to everyone for the tips.

Nic : malloc.c not solve the bug in this case.
Arrch:  sprintf() does not work very well, for example, sprintf( Result," %02u   %02u   %02 ",A,B,C) or
sprintf( Result," %d   %d   %d ",A,B,C) ,  does not work well.
PaulS:  Was a good idea to move the lcd.print() to TiempoTotal().


Finally my function code was well :
Code:

int TiempoTotal (unsigned long Cantidad){
     
      char Hs[10];
      char Min[10];
      char Seg[10];
      char Respuesta[16];
     
      unsigned long TT = Cantidad * Retardo;           
     
      unsigned long Remanente1 = (TT % 3600);
      unsigned long H = (TT - Remanente1)/3600;
      unsigned long S = Remanente1 % 60;
      unsigned long M = ( Remanente1 - S) / 60;
      sprintf(Hs," %02uHs. ",H);           
      sprintf(Min,"%02um. ",M); 
      sprintf(Seg,"%02us. ",S); 
      //sprintf(Respuesta,"%d:%d:%d  ", H,M,S);
      //lcd.setCursor(0, 2);
      //lcd.print (Respuesta);
   
      lcd.setCursor(0, 2);
      lcd.print (  Hs );
      lcd.setCursor(7, 2);
      lcd.print ( Min );
      lcd.setCursor(12, 2);
      lcd.print ( Seg );
   
      return 0;
}

Thanks again to all

Greeting Oso57 from Argentina,.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 549
Posts: 46128
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Arrch:  sprintf() does not work very well, for example, sprintf( Result," %02u   %02u   %02 ",A,B,C) or
sprintf( Result," %d   %d   %d ",A,B,C) ,  does not work well.
What was the result of the two calls? The first one contains an invalid format specifier. The second assumes ints, not long (signed or unsigned).

Though why H, M, and S are longs in your code is a mystery. The range of values for M and S are 0 to 60. Those values would fit in a byte just as easily as in a 4-byte long.
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arrch:  sprintf() does not work very well, for example, sprintf( Result," %02u   %02u   %02 ",A,B,C) or
sprintf( Result," %d   %d   %d ",A,B,C) ,  does not work well.

Good to see it's working, but sprintf() works just fine when you use it correctly.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arrch:  sprintf() does not work very well, for example, sprintf( Result," %02u   %02u   %02 ",A,B,C) or
sprintf( Result," %d   %d   %d ",A,B,C) ,  does not work well.

Good to see it's working, but sprintf() works just fine when you use it correctly.

PaulS, Arrrch ...

sprintf( Result," %02u", A ) 

Since A a long value works fine for me, this padded with leading zero if the value is less than 10) 

I understand that the code is not optimal, but is the result of looking again and again and again the solution of the problem, it did not and still do not know that the bug with Strings ().
Years ago I program in Visual Basic, but C is new to me
I will learn more about  sprintf() ( for me Visual basic is more, more simple. Ex: Result=Format( A ,"00") )
I will learn more C.

Thanks for your help.

« Last Edit: February 23, 2013, 11:07:00 am by Oso57 » Logged

Pages: [1]   Go Up
Jump to: