the if-case is taken only one time in the while-loop, why?

Hi together,

the idea of my first "program" is, that I'm iterating and everytime when the iterator variable has a "2" as a digit the ledPin is lightning for 5 seconds.
And a second iterator(waiter) is counting down, everytime, when the ledPin should light.

The Problem is that the if-case is only functioning with the number two.
After two the programm never comes back into the if-case and I don't understand why.

Could anyone help and I know that it is maybe a bloody noob question, because I am one.

int ledPin = 13; 

void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  Serial.println("MyFirstOwnArduinoProject");
  Serial.println();
}

void loop()
{
  int iterator = 0;
  int fromLength = 0;



  while(true){
    String testIterator = String(iterator);
    int toLength = String(iterator).length();
    
    Serial.print("testIterator: " );
    Serial.println(testIterator);
    
    
    Serial.print("toLength :" );
    Serial.println(toLength );
    
    Serial.print("iterator: " );
    Serial.println(iterator);
    if(testIterator.substring(fromLength).equals("2") )
    {

       Serial.println("LIGHTNING++++++++++");
      digitalWrite(ledPin, HIGH);
      delay(5000);
      digitalWrite(ledPin, LOW);
      delay(1000);
      int test0 = 0;
      int sek = 2000;
      while(sek !=test0)
      {
        sek--;
        Serial.println(sek);
      }

    }

    iterator++;  
    continue;
  }
}
 int iterator = 0;

Did you mean "static"?

What is that "continue" for ?

The continue keyword has almost no legitimate use in C/C++

AWOL: does he need it? He is in an infinite while loop and I don't see a break, so it never comes out of it.

@michinyon: continue skips the instructions at the end. could be that the failure? could it be that it is skipping the instruction(the if-case) at the beginning?

@AWOL and econjack : i think econjack is right, because the iteration increments and the output is fine.

Nevertheless

  1. I added static to: static int iterator; => same probleme like before
  2. deleted "continue;" => same probleme like before

hotcoffee:
the idea of my first "program" is, that I'm iterating and everytime when the iterator variable has a "2" as a digit the ledPin is lightning for 5 seconds.
And a second iterator(waiter) is counting down, everytime, when the ledPin should light.

The Problem is that the if-case is only functioning with the number two.
After two the programm never comes back into the if-case and I don't understand why.

Could anyone help and I know that it is maybe a bloody noob question, because I am one.

Well, where to start? It never comes back into the if case because the substring is never equal to 2 again. Substring, with just one argument, takes that to be the starting position of the substring, and without an end argument, goes to the end of the string.

If you run it a while, you'll see that fromlength is always one higher than the index of the last character in the String.

If you want to turn on the LED any time there is a '2' in the String, you probably want the .indexof() function.

Frankly, your program baffles me. What is the purpose of sek? you count it down from 2000 to 0, then iterate. Why?

continue is totally useless where it is. It is meant to skip instructions within the innermost enclosing loop and continue the loop from the top. In this case, the innermost enclosing loop is the while, and without the continue, it just starts the while loop again anyway.

michinyon,
There are uses for continue. I've used it mainly to discard further operations on something that I already know, will not be necessary.

Hi lar3cry,

at first sorry for my bad english and thanks for your answer:

Well, where to start? It never comes back into the if case because the substring is never equal to 2 again. Substring, with just one argument, takes that to be the starting position of the substring, and without an end argument, goes to the end of the string.

I used this example and I thought and think it's, still, more or less ok. Or I don't understand the example right?

 // substring(index) looks for the substring from the index position to the end:
  if (stringOne.substring(19) == "html") {
    Serial.println("It's an html file");

The variable fromLenght is always 0. I gave me the output with Serial.println(fromLength);

If you want to turn on the LED any time there is a '2' in the String, you probably want the .indexof() function.

That's also an idea to find the right way, but it would be perfectly, if i or someone could find my mistake here now. =(

Frankly, your program baffles me. What is the purpose of sek? you count it down from 2000 to 0, then iterate. Why?

I wanted to the lightning, so i decided to give the light time to glow. That's why i am incrementing for a while at a finding point.
sek should mean seconds. A mistake of me.

continue is totally useless where it is. It is meant to skip instructions within the innermost enclosing loop and continue the loop from the top. In this case, the innermost enclosing loop is the while, and without the continue, it just starts the while loop again anyway.

thanks

hotcoffee:
I used this example and I thought and think it's, still, more or less ok. Or I don't understand the example right?
http://arduino.cc/en/Tutorial/StringSubstring

Well, you didn't understand the example. In the example, stringOne contained a string that ended with "html", and since the index pointed to the 'h' in "html", and since you did not use an end index, it returned the string from index 19 to the end of the string, which hapened to be "html". Try this code to see what happens.

  // substring(index) looks for the substring from the index position to the end:
  String stringOne = "Content-Type: text/html";
  if (stringOne.substring(19) == "html") {
    Serial.println("It's an html file");
  }
  stringOne = "Content-Type: text/html and other stuff";    
  if (stringOne.substring(19) == "html") {
    Serial.println("It's an html file");
  }
  else {
    Serial.println( stringOne.substring(19));
  }

The result will be:
It's an html file
html and other stuff

The variable fromLenght is always 0. I gave me the output with Serial.println(fromLength);

Oops.. yes, you're right. But it doesn't matter, because testIterator will never again = "2".

If you want to turn on the LED any time there is a '2' in the String, you probably want the .indexof() function.

That's also an idea to find the right way, but it would be perfectly, if i or someone could find my mistake here now. =(

It all depends on what you want to do. If you want to turn on the LED when testIterator starts with "2", then use charAt(), or startsWith(). Check the reference section for many other ways of checking for a particular character in the String.

I wanted to the lightning, so i decided to give the light time to glow. That's why i am incrementing for a while at a finding point.
sek should mean seconds. A mistake of me.

You already tell it to glow for 5 seconds, using delay(). You do know that nothing else happens during a delay(), right?

Now, after all that, you should be aware that every time you change a String (the String object, not the string thatis an array of char), a new copy will be made, and the memory from the old one deallocated. Before you know it, your sketch will crash. Guaranteed, when it runs out of sufficiently large chunks of memory to allocate a new String.

You should read up on C strings, and use those instead.