Skip to main content
char* ampms[]= {"am", "pm"};

void loop() // run over and over again
{     //get time from RTC          
     getDateDs1307(&second, &minute, &hour); 

      //change to 12 hour format.
      if (hour>12) { hr = hour - 12; ampm=1; } else { hr = hour; } 

      //add leading 0 for hours
      if (hr<10) { lcd.print("0"); }
      
      //print hour in 12 hour format.
      lcd.print(hr, DEC);

      lcd.print(":");

      //add leading 0 for minutes
      if (minute<10) { lcd.print("0");}

      //print minutes
      lcd.print(minute, DEC); 

      //print am or pm using array.
      dataFile.print(ampms[ampm]);          
}
char* ampms[]= {"am", "pm"};

void loop() // run over and over again
{     //get time from RTC          
     getDateDs1307(&second, &minute, &hour); 

      //change to 12 hour format.
      if (hour>12) { hr = hour - 12; ampm=1; } else { hr = hour; } 

      //add leading 0 for hours
      if (hr<10) { lcd.print("0"); }
      
      //print hour in 12 hour format.
      lcd.print(hr, DEC);

      lcd.print(":");

      //add leading 0 for minutes
      if (minute<10) { lcd.print("0");}

      //print minutes
      lcd.print(minute, DEC); 

      //print am or pm using array.
      dataFile.print(ampms[ampm]);          
}
void loop() // run over and over again
{     //get time from RTC          
     getDateDs1307(&second, &minute, &hour); 

      //change to 12 hour format.
      if (hour>12) { hr = hour - 12; } else { hr = hour; } 

      //add leading 0 for hours
      if (hr<10) { lcd.print("0"); }
      
      //print hour in 12 hour format.
      lcd.print(hr, DEC);

      lcd.print(":");

      //add leading 0 for minutes
      if (minute<10) { lcd.print("0");}

      //print minutes
      lcd.print(minute, DEC); 

      //print am or pm using array.
      if (hour>12) { lcd.print("am");} else {lcd.print("pm"); }        
}
void loop() // run over and over again
{     //get time from RTC          
     getDateDs1307(&second, &minute, &hour); 

      //change to 12 hour format.
      if (hour>12) { hr = hour - 12; } else { hr = hour; } 

      //add leading 0 for hours
      if (hr<10) { lcd.print("0"); }
      
      //print hour in 12 hour format.
      lcd.print(hr, DEC);

      lcd.print(":");

      //add leading 0 for minutes
      if (minute<10) { lcd.print("0");}

      //print minutes
      lcd.print(minute, DEC); 

      //print am or pm using array.
      if (hour>12) { lcd.print("am");} else {lcd.print("pm"); }        
}
char* ampms[]= {"am", "pm"};

void loop() // run over and over again
{     //get time from RTC          
     getDateDs1307(&second, &minute, &hour); 

      //change to 12 hour format.
      if (hour>12) { hr = hour - 12; ampm=1; } else { hr = hour; } 

      //add leading 0 for hours
      if (hr<10) { lcd.print("0"); }
      
      //print hour in 12 hour format.
      lcd.print(hr, DEC);

      lcd.print(":");

      //add leading 0 for minutes
      if (minute<10) { lcd.print("0");}

      //print minutes
      lcd.print(minute, DEC); 

      //print am or pm using array.
      dataFile.print(ampms[ampm]);          
}
void loop() // run over and over again
{     //get time from RTC          
     getDateDs1307(&second, &minute, &hour); 

      //change to 12 hour format.
      if (hour>12) { hr = hour - 12; } else { hr = hour; } 

      //add leading 0 for hours
      if (hr<10) { lcd.print("0"); }
      
      //print hour in 12 hour format.
      lcd.print(hr, DEC);

      lcd.print(":");

      //add leading 0 for minutes
      if (minute<10) { lcd.print("0");}

      //print minutes
      lcd.print(minute, DEC); 

      //print am or pm using array.
      if (hour>12) { lcd.print("am");} else {lcd.print("pm"); }        
}
char* ampms[]= {"am", "pm"};

void loop() // run over and over again
{     //get time from RTC          
     getDateDs1307(&second, &minute, &hour); 

      //change to 12 hour format.
      if (hour>12) { hr = hour - 12; ampm=1; } else { hr = hour; } 

      //add leading 0 for hours
      if (hr<10) { lcd.print("0"); }
      
      //print hour in 12 hour format.
      lcd.print(hr, DEC);

      lcd.print(":");

      //add leading 0 for minutes
      if (minute<10) { lcd.print("0");}

      //print minutes
      lcd.print(minute, DEC); 

      //print am or pm using array.
      dataFile.print(ampms[ampm]);          
}
void loop() // run over and over again
{     //get time from RTC          
     getDateDs1307(&second, &minute, &hour); 

      //change to 12 hour format.
      if (hour>12) { hr = hour - 12; } else { hr = hour; } 

      //add leading 0 for hours
      if (hr<10) { lcd.print("0"); }
      
      //print hour in 12 hour format.
      lcd.print(hr, DEC);

      lcd.print(":");

      //add leading 0 for minutes
      if (minute<10) { lcd.print("0");}

      //print minutes
      lcd.print(minute, DEC); 

      //print am or pm using array.
      if (hour>12) { lcd.print("am");} else {lcd.print("pm"); }        
}
Source Link
futurebird
  • 425
  • 5
  • 15

What will use less memory, an array or if ... else?

I'm trying to make my sketch smaller. Right now I use an array for the AM/PM part of my time display. My thinking is this makes it easy to change the formatting:

char* ampms[]= {"am", "pm"};

void loop() // run over and over again
{     //get time from RTC          
     getDateDs1307(&second, &minute, &hour); 

      //change to 12 hour format.
      if (hour>12) { hr = hour - 12; ampm=1; } else { hr = hour; } 

      //add leading 0 for hours
      if (hr<10) { lcd.print("0"); }
      
      //print hour in 12 hour format.
      lcd.print(hr, DEC);

      lcd.print(":");

      //add leading 0 for minutes
      if (minute<10) { lcd.print("0");}

      //print minutes
      lcd.print(minute, DEC); 

      //print am or pm using array.
      dataFile.print(ampms[ampm]);          
}

I could do this instead:

void loop() // run over and over again
{     //get time from RTC          
     getDateDs1307(&second, &minute, &hour); 

      //change to 12 hour format.
      if (hour>12) { hr = hour - 12; } else { hr = hour; } 

      //add leading 0 for hours
      if (hr<10) { lcd.print("0"); }
      
      //print hour in 12 hour format.
      lcd.print(hr, DEC);

      lcd.print(":");

      //add leading 0 for minutes
      if (minute<10) { lcd.print("0");}

      //print minutes
      lcd.print(minute, DEC); 

      //print am or pm using array.
      if (hour>12) { lcd.print("am");} else {lcd.print("pm"); }        
}

No more am/pm array. But, I have to add an if... is one better than the other for memory? Why?