user37689: This is code about a keypad lock. The user can unlock the device by entering the secret code. To lock it again they press "#" or "*".  
I want to let the green LED turn on for about 0.5 seconds then return to the first line of the loop: char key = keypad.getKey();. 
The problem is that this code makes the green LED turn on forever after we enter the correct secret code or until we enter "#" or "*".
void loop() {
  char key = keypad.getKey();
  if (key=='*'||key=='#') {
    position = 0;
    setLocked(true);
  }
  if (key==secretCode[position]) {
    position ++;
  }
  if (position ==3) {
    setLocked(false);
  }
  delay(100);
}

void setLocked(int locked) {
  if (locked) {
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, LOW);
  } else {
    digitalWrite(redpin, LOW);
    digitalWrite(greenpin, HIGH);
    delay(500);
  }
}


user33608: I may be missing something, but couldn't you just modify your setLocked function?
void setLocked(int locked) {
  if (locked) {
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, LOW);
  } else {
    digitalWrite(redpin, LOW);
    digitalWrite(greenpin, HIGH);
    delay(500);
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, LOW);
  }
  return;
}


user-1: I was looking at your loop() and it have some problems, like not counting how many digits did the user input and locking upon receiving a single "#" a
Here is some better way:
char secretCode[] = "1234";
char inputCode[] = "xxxx";
int position = 0;

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

void loop() {
  char key = keypad.getKey();

  if (key == '*' || key=='#') {
    int i = 0;
    //  With 4 input digits, now we compare against secret code.
    while (i < sizeof(inputCode) && inputCode[i] == secretCode[i]) {
      i++;
      }
    //  Set locked only if 4 digits are the same.
    setLocked(i == sizeof(inputCode));
    //  Reset input code array.
    position = 0;
  } else {
    //  Key adding digits to the input code.
    inputCode[position++] = key;
    if (position == sizeof(inputCode)) {
      position = 0;
    }
  }  
  delay(100);
}

void setLocked(int locked) {
  if (locked) {
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, LOW);
  } else {
    digitalWrite(redpin, LOW);
    digitalWrite(greenpin, HIGH);
    delay(500);
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, LOW);
  }
  return;
}


37689: <p>This is code about a keypad lock. The user can unlock the device by entering the secret code. To lock it again they press "#" or "*".  </p>

<p>I want to let the green LED turn on for about 0.5 seconds then return to the first line of the loop: <code>char key = keypad.getKey();</code>. </p>

<p>The problem is that this code makes the green LED turn on forever after we enter the correct secret code or until we enter "#" or "*".</p>

<pre><code>void loop() {
  char key = keypad.getKey();
  if (key=='*'||key=='#') {
    position = 0;
    setLocked(true);
  }
  if (key==secretCode[position]) {
    position ++;
  }
  if (position ==3) {
    setLocked(false);
  }
  delay(100);
}

void setLocked(int locked) {
  if (locked) {
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, LOW);
  } else {
    digitalWrite(redpin, LOW);
    digitalWrite(greenpin, HIGH);
    delay(500);
  }
}
</code></pre>
 33608: <p>I may be missing something, but couldn't you just modify your <code>setLocked</code> function?</p>

<pre><code>void setLocked(int locked) {
  if (locked) {
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, LOW);
  } else {
    digitalWrite(redpin, LOW);
    digitalWrite(greenpin, HIGH);
    delay(500);
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, LOW);
  }
  return;
}
</code></pre>
 -1: <p>I was looking at your <code>loop()</code> and it have some problems, like not counting how many digits did the user input and locking upon receiving a single "#" a</p>

<p>Here is some better way:</p>

<pre><code>char secretCode[] = "1234";
char inputCode[] = "xxxx";
int position = 0;

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

void loop() {
  char key = keypad.getKey();

  if (key == '*' || key=='#') {
    int i = 0;
    //  With 4 input digits, now we compare against secret code.
    while (i &lt; sizeof(inputCode) &amp;&amp; inputCode[i] == secretCode[i]) {
      i++;
      }
    //  Set locked only if 4 digits are the same.
    setLocked(i == sizeof(inputCode));
    //  Reset input code array.
    position = 0;
  } else {
    //  Key adding digits to the input code.
    inputCode[position++] = key;
    if (position == sizeof(inputCode)) {
      position = 0;
    }
  }  
  delay(100);
}

void setLocked(int locked) {
  if (locked) {
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, LOW);
  } else {
    digitalWrite(redpin, LOW);
    digitalWrite(greenpin, HIGH);
    delay(500);
    digitalWrite(redpin, HIGH);
    digitalWrite(greenpin, LOW);
  }
  return;
}
</code></pre>
