Cannot get a FOR statement to work

While I press the keypad I want the LCD to print the number and I want it to loop four times to show me the entered code while I type it. I can get it to work properly while using IF statement but since I will be doing a lot of similar coding throughout the project I want to avoid a lot of code by using FOR statement instead. I have tried a lot of different codes but cannot get it to do what I want. Could someone please help me out and tell me what I am doing wrong?

    if (bCodeInit == LOW)
    {
      lcd.clear ();
      lcd.print("SET CODE:");
      SettingCode = 0;
      bCodeInit = HIGH;
    }
    for (int i=0; i<=3; i++)
    {
      nextDigit:
      if (keypressed != NO_KEY || SettingCode == i)
      { 
        Code[i] = keypressed;
        if (keypressed = NO_KEY)
        {
          lcd.setCursor(i, 2);
          lcd.print(Code[i]);
          SettingCode = SettingCode+1;
          goto nextDigit;
        }
      }
    }

You do know it will run 4 times no matter if it reads NO_KEY and SettingCode isn't equal to i?

And what on earth is that goto doing there?

For a better answer, post all your code.

septillion:
You do know it will run 4 times no matter if it reads NO_KEY and SettingCode isn't equal to i?

And what on earth is that goto doing there?

For a better answer, post all your code.

You are right, off course it will. The problem is that I am still new to this kind of programming. I have a lot of experience in ABB industrial robots so I am used to it reading and executing every single line before it jumps to the next. The GOTO was a desperate attempt to get the program to wait until the key is no longer pressed, but I realize that won't work. What I am missing is a statement to use to get it to wait for me to release the key within the FOR loop. Maybe that is impossible and I need to go for IF statements after all. :confused:

A while loop would be better than using goto. You should avoid the use of goto unless you're absolutely sure there's no better option. Even then you should spend some more time thinking about it and you'll probably come up with an alternative.

        if (keypressed = NO_KEY)

That = should be ==

Pete

pert:
A while loop would be better than using goto. You should avoid the use of goto unless you're absolutely sure there's no better option. Even then you should spend some more time thinking about it and you'll probably come up with an alternative.

while - Arduino Reference

I know. I tried that first but it didn't work. I guess I will have to go for the IF statement after all. I am used to rapid and PLC ladder programming so when I program a PLC with structured text I get too confused.

The main difference compared to ladder programming is that you want the loop() to just loop. Just remember how many times you did something and just skip over it as long as it's not time to do it again. :slight_smile: That's called non-blocking and will allow you do do more then a single thing like slow human interaction.

The only time you should ever ever consider using goto is if you are writing an example of poor programming
practice :slight_smile:

C has switch, return, break & continue, between them they cover every conceivable need for goto in a much
clearer/cleaner way.

Rather than incrementing i in the for statement, only increment it if a key has been pressed.

  for (int i=0; i<=3; )
    {
      keypressed = read_the_keypad();
      if (keypressed != NO_KEY)
      {
        Code[i] = keypressed;
        lcd.setCursor(i, 2);
        lcd.print(Code[i]);
        i++;
    }
  }

PaulMurrayCbr:
Rather than incrementing i in the for statement, only increment it if a key has been pressed.

  for (int i=0; i<=3; )

{
      keypressed = read_the_keypad();
      if (keypressed != NO_KEY)
      {
        Code[i] = keypressed;
        lcd.setCursor(i, 2);
        lcd.print(Code[i]);
        i++;
    }
  }

Ah, thanks! That sounds like the coding I was looking for. Will try. :slight_smile: