Beginner requires help with for loop.

My apologies for such a simple query, however I’ve spent almost two days on this and it’s driving me insane!

I’m trying to display a countdown timer on a 16x2 LCD display, counting down each second (so I can interrupt the process if necessary), however the for loop doesn’t work as I expected it to and I can’t work out why. The display is simulated via the serial port for the purposes of this post.

First of all, the formatting doesn’t work and then the for loop only seems to execute once.

I’ve isolated the section of code that is giving me problems :

int timeDelay=15; 

void setup() {
  Serial.begin(9600);
}

void loop() {
  int sCounter; 
  String sSeconds;  // use this string to format output to always two characters

    Serial.print("Delay=");
  Serial.println(timeDelay);
  Serial.print("sCounter=");
  Serial.println(sCounter);

  for(int seconds = timeDelay; seconds > 0; seconds--) {
    Serial.print("seconds=");
    Serial.println(String(seconds));

    if(seconds=0) { 
      sSeconds = "00"; 
    }
    else if((seconds < 10) && (seconds > 0)) {
      sSeconds = "0" + String(seconds);
    } 
    else { 
      sSeconds = String(seconds); 
    }

    Serial.print("sSeconds=");
    Serial.println(sSeconds);
    Serial.println();
    delay(5000);  // Normally set to 1000 for approx 1 second - can be calibrated.
  }
}
  int sCounter; 
  String sSeconds;  // use this string to format output to always two characters

    Serial.print("Delay=");
  Serial.println(timeDelay);
  Serial.print("sCounter=");
  Serial.println(sCounter);

sCounter is a local variable, no initialized. Why would you want to print garbage?

    Serial.println(String(seconds));

There is no excuse for using the String class to convert the int to a string, and wrapping it in a memory wasting class, only to have the println() method have to unwrap it. The println() method KNOWS how to print an int.

    if(seconds=0) {

Assigning a value to seconds in an if statement is worse than useless. ==, surely?

sptintf() would piss away far fewer resources than the String class, and be far easier for real programmers to read.

Thank you Paul - I knew the problem was a simple one, but have spent hours looking at the code, only to miss the "="!

sCounter was a throwback from the original code - sorry for including that red herring.

I am not familiar with sptintf() - is that in an add-on library? I was using String because the ultimate output is to an I2C LCD display, so it's lcd.print(String(seconds));

I do appreciate your assistance. I'm not a programmer, I'm afraid.

I am not familiar with sptintf() - is that in an add-on library?

It should have been sprintf(), and google knows all about it.