Simple program fails to decrement variable

Hey all,

I'm goofing off with a 5" LCD driven by the RA8875. It shouldn't matter that much, but I'll say so to be thorough.

I was experimenting a bit just to see how simple it really is to print out a periodically changing variable.

So after including all my appropriate libraries for the screen and graphics (the adafruit RA8875 and GPX.h),

I thought I would punch out some psuedo-code to see what happens.

Its a simple ~minute long bomb tick timer. I thought I would leave it out on the table for the roommates to see in the morning.

Anyways, all seems to work well as far as formatting and timing.

The problem is the output. In the seconds column. It is one of the two variables that even change throughout the program, yet it will not change correctly.

The program prints the following integers into the "Seconds" place on the lcd as well as in the serial port.

Here is the output of the program:

RA8875 start
Found RA8875
60
255
52
255
52
255
52
255
52
255

and on and on

How sad. I can't even properly decrement a variable. Any ideas on what I'm doing wrong? I cannot for the life of me determine why the variable does not simple decrease by one when it needs to. loopCounter only became volatile as an attempted fix to no avail. I've tried changing the variable to a uint16_t and a uint32_t but the effect is the same. Is it the way the avr compiler treats the datatype within the sprintf() macro? Can this be resolved?

volatile uint8_t loopCounter;
   char superString[8];

void setup() 
{
  Serial.begin(9600);
  Serial.println("RA8875 start");

  /* Initialise the display using 'RA8875_480x272' or 'RA8875_800x480' */
  if (!tft.begin(RA8875_800x480)) {
    Serial.println("RA8875 Not Found!");
    while (1);
  }

  Serial.println("Found RA8875");
  


  tft.displayOn(true);
  tft.GPIOX(true);      // Enable TFT - display enable tied to GPIOX
  tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
  tft.PWM1out(255); 

  
  tft.graphicsMode();


tft.fillScreen(RA8875_BLACK);
tft.textMode();
tft.textColor(white, black);
loopCounter = 60;
tft.textSetCursor(150, 100);
  tft.textEnlarge(2);
tft.textWrite("Time till Detonation:", 0);
tft.textEnlarge(3);
tft.textColor(redOrange, black);
loopCounter = 60;
}



void loop() 
{
  float xScale = 1024.0F/tft.width();
  float yScale = 1024.0F/tft.height();

tft.textSetCursor(250, 200);

if(loopCounter <= 9)
{
  sprintf(superString, "00:00:0%d", loopCounter);
}
else
{
    sprintf(superString, "00:00:%d", loopCounter);
}

  loopCounter = loopCounter - 1;

if(loopCounter == 0)
{
    tft.textWrite("Just Kidding.", 0);
    delay(3000);
    tft.textWrite("Resetting..", 0);
    delay(1500);
    loopCounter = 60;
}
else
{
tft.textWrite(superString, 8);
}
delay(1000);

}

superString needs to be bigger if you want to store 8 characters in it, since you need a spot for the null terminator.

Please use CTRL-T to auto format your code to improve readability before posting.. Thank you

troyerta:
Here is the output of the program:

RA8875 start

Found RA8875
60
255
52
255
52
255
52
255
52
255

The first two lines of the output correspond to Serial.println() calls in setup(). Where do the subsequent numbers come from? I don't see any code in the sketch you posted that would print these to the serial port.

}

  loopCounter = loopCounter - 1;

if(loopCounter == 0)

I think you are missing a } before if(loopCounter and that probably means you have a suplus } somewhere else.

...R