I'm working on my first real Arduino app, a clock using words rather than digits, employing a Nokia 5110 LCD and a DS1307 RTC. However I'm having major issues with string corruption, lockups and reboots of my Uno and after much effort still have no idea why.
I've reduced the program to something very simple which uses no other hardware (see below). It simply loops, incrementing the seconds from 30 and Serial.println's them as a string (the computation of which is the only significant code). I compile using the IDE Arduino 0022, upload to my Uno, then connect to the serial monitor (@ 38k4). I see "thirty", "thirty-one" etc but nothing after "thirty-seven" (and in fact that's corrupted as "thirty- even"). The program seems to have frozen.
int clock_Second = 30; // stops at "thirty- even"
//int clock_Second = 40; // stops at "twenty-two"
int clock_Minute = 2;
int clock_Hour = 1;
int clock_DayOfWeek = 3;
int clock_DayOfMonth = 4;
int clock_Month = 5;
int clock_Year = 11;
bool clock_PM = true;
#define CLOCK_MAX_LINE 8
int clock_DisplayedMinute;
String clock_Lines[CLOCK_MAX_LINE];
char* clock_OrdinalNames[] =
{
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen",
"twenty", "thirty", "forty", "fifty"
};
String clock_GetNumberName(int Number)
{
// return the string representation of Number, eg 21 -> "twenty-one"
String Name = "";
if (0 <= Number && Number < 60)
{
if (Number <= 20)
{
// 0..20 are in there in full
Name = clock_OrdinalNames[Number];
}
else
{
// get the tens part
Name = clock_OrdinalNames[20 + ((Number - 20) / 10)];
if (Number % 10 != 0)
{
// and the units part
Name += "-";
Name += clock_OrdinalNames[Number % 10];
}
}
}
return Name;
}
void clock_ShowTime(void)
{
Serial.println(clock_Lines[0]);
Serial.println("-----------");
}
void clock_FormatTime(void)
{
int Hour = clock_Hour; // unused remnants of original program
int Minute = clock_Minute;
String LineStr;
clock_Lines[0] = clock_GetNumberName(clock_Second);
}
void setup(void)
{
Serial.begin(38400);
}
void loop(void)
{
clock_FormatTime();
clock_ShowTime();
// advance the "clock"
clock_Second++;
if (clock_Second > 59)
{
clock_Second = 0;
clock_Minute++;
if (clock_Minute > 59)
{
clock_Minute = 0;
clock_Hour++;
if (clock_Hour > 12)
{
clock_Hour = 1;
clock_PM = !clock_PM;
}
}
}
}
I can't see anything wrong with the code, perhaps someone else can. I'd suspect some kind of resource exhaustion, but what? I'm not explicitly using the heap and I can't see any recursion. I'd also suspect a rogue pointer or array over-run, but I don't think that's happening.
Note that slight changes to the code make the problem change or apparently vanish (eg, starting the count at 40 halts at "twenty-two").
Tremendously frustrating, I hope someone can help.
It works fine if I use Arduino 0021 (sketch is 4310 vs 4465 bytes with 0022), so I'm suspicius of the compiler too, unlikely though that may be.