user31535: Im having a problem with my arduino code.Im trying to make an alarm system with a pir sensor.So when the pir activates and the buzzer starts buzzing i want to be able to type a set password in the keypad and the buzzer should stop buzzing. since my passcode is 4-digit (1331) I wrote this code, but it`s not working.
 while(i<=4,i++)
{
char key = keypad.getKey();

pass.append(key);
}
if(pass.evaluate())
{       
  digitalWrite(buzzer, LOW);
    keypad.waitForKey();
    delay(6000);      
}

My idea is that the program checks 4 times for the pressed key and adds it to the guessed password (im using the password library and the password is defined as well as int i=0;)
But for some reason when I type 1331 the buzzing continues. Please help me with that project and excuse my grammar, English is not my native language.
edit:
the full code:
#include <Password.h>
Password pass = Password( "1331" );


#include <Keypad.h>
int sensor = 11;
int buzzer = 10;
int i = 0;

const byte rows = 4; //four rows
const byte cols = 4; //four columns
char keys[rows][cols] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};




byte rowPins[rows] = {8, 7, 6, 9}; //connect to the row pinouts of the keypad
byte colPins[cols] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );

void setup() {
pinMode(sensor, INPUT);
pinMode (buzzer, OUTPUT);
digitalWrite(buzzer,HIGH);
delay(125);
digitalWrite(buzzer, LOW);
delay(125);
digitalWrite(buzzer,HIGH);
delay(125);
digitalWrite(buzzer, LOW);
delay(125);
digitalWrite(buzzer,HIGH);
delay(125);
digitalWrite(buzzer, LOW);
keypad.waitForKey();
digitalWrite(buzzer,HIGH);
  delay(125);
  digitalWrite(buzzer, LOW);
  delay(6000);






}

void loop() {

  char state = digitalRead(sensor);
  if (state == HIGH)
  {
    digitalWrite(buzzer, HIGH);
    while(i<4,i++)
    {
    char key = keypad.getKey();
    //keypad.waitForKey(); not sure if i should use that

    pass.append(key);
    }
    if(pass.evaluate())
    {       
      digitalWrite(buzzer, LOW);
        keypad.waitForKey();
        delay(6000);      
    }




  }
      }


user29909: 
Without knowing how the pass.* works, it will be impossible to pinpoint the problem.
'key' gets an ASCII value. Is that would you are expecting?
Does pass.* reset (start over) once 4 digits?
Just a guess, but I believe that *.waitForKey() does not consume the key.
The algorithm only gets the key four times; then, it checks.


user10794: This is completely the wrong way of doing a loop. Since Arduino uses C++ I made this test program:
#include <stdio.h>

int main (void)
  {
  printf ("starting\n");
  int i = 0;
  while(i<=4,i++)
     {
     printf ("%i\n", i);
     }
  printf ("done\n");
  return 0;
  }

This prints:
starting
done

Note that the loop is executed zero times. This is because you used the "comma operator".
From https://en.wikipedia.org/wiki/Comma_operator you can see:

In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).

In other words, your loop adds one to i and checks if it is zero (before adding one). Thus the loop terminates immediately.
A better construct would be:
for (i = 0; i < 4; i++)
   {
   // whatever
   }

That is the standard C/C++ way of doing a loop a fixed number of times.

Also, most keypad libraries return NO_KEY if the user hasn't pressed a key.
You should test if keypad.getKey() returns NO_KEY and if so do nothing.
All your code would have done (if it had executed 4 times) was get 4 times NO_KEY (in a few microseconds) which would not be the correct password.

31535: <p>Im having a problem with my arduino code.Im trying to make an alarm system with a pir sensor.So when the pir activates and the buzzer starts buzzing i want to be able to type a set password in the keypad and the buzzer should stop buzzing. since my passcode is 4-digit (1331) I wrote this code, but it`s not working.</p>

<pre><code> while(i&lt;=4,i++)
{
char key = keypad.getKey();

pass.append(key);
}
if(pass.evaluate())
{       
  digitalWrite(buzzer, LOW);
    keypad.waitForKey();
    delay(6000);      
}
</code></pre>

<p>My idea is that the program checks 4 times for the pressed key and adds it to the guessed password (im using the password library and the password is defined as well as int i=0;)
But for some reason when I type 1331 the buzzing continues. Please help me with that project and excuse my grammar, English is not my native language.</p>

<p>edit:
the full code:</p>

<pre><code>#include &lt;Password.h&gt;
Password pass = Password( "1331" );


#include &lt;Keypad.h&gt;
int sensor = 11;
int buzzer = 10;
int i = 0;

const byte rows = 4; //four rows
const byte cols = 4; //four columns
char keys[rows][cols] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};




byte rowPins[rows] = {8, 7, 6, 9}; //connect to the row pinouts of the keypad
byte colPins[cols] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );

void setup() {
pinMode(sensor, INPUT);
pinMode (buzzer, OUTPUT);
digitalWrite(buzzer,HIGH);
delay(125);
digitalWrite(buzzer, LOW);
delay(125);
digitalWrite(buzzer,HIGH);
delay(125);
digitalWrite(buzzer, LOW);
delay(125);
digitalWrite(buzzer,HIGH);
delay(125);
digitalWrite(buzzer, LOW);
keypad.waitForKey();
digitalWrite(buzzer,HIGH);
  delay(125);
  digitalWrite(buzzer, LOW);
  delay(6000);






}

void loop() {

  char state = digitalRead(sensor);
  if (state == HIGH)
  {
    digitalWrite(buzzer, HIGH);
    while(i&lt;4,i++)
    {
    char key = keypad.getKey();
    //keypad.waitForKey(); not sure if i should use that

    pass.append(key);
    }
    if(pass.evaluate())
    {       
      digitalWrite(buzzer, LOW);
        keypad.waitForKey();
        delay(6000);      
    }




  }
      }
</code></pre>
 29909: <ol>
<li>Without knowing how the pass.* works, it will be impossible to pinpoint the problem.</li>
<li>'key' gets an ASCII value. Is that would you are expecting?</li>
<li>Does pass.* reset (start over) once 4 digits?</li>
<li>Just a guess, but I believe that *.waitForKey() does not consume the key.</li>
<li>The algorithm only <em>gets</em> the key four times; then, it checks.</li>
</ol>
 10794: <p>This is completely the wrong way of doing a loop. Since Arduino uses C++ I made this test program:</p>

<pre><code>#include &lt;stdio.h&gt;

int main (void)
  {
  printf ("starting\n");
  int i = 0;
  while(i&lt;=4,i++)
     {
     printf ("%i\n", i);
     }
  printf ("done\n");
  return 0;
  }
</code></pre>

<p>This prints:</p>

<pre><code>starting
done
</code></pre>

<p>Note that the loop is executed <strong>zero</strong> times. This is because you used the "comma operator".</p>

<p>From <a href="https://en.wikipedia.org/wiki/Comma_operator" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Comma_operator</a> you can see:</p>

<blockquote>
  <p>In the C and C++ programming languages, the <strong>comma operator</strong> (represented by the token <code>,</code>) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type).</p>
</blockquote>

<p>In other words, your loop adds one to <code>i</code> and checks if it is zero (before adding one). Thus the loop terminates immediately.</p>

<p>A better construct would be:</p>

<pre><code>for (i = 0; i &lt; 4; i++)
   {
   // whatever
   }
</code></pre>

<p>That is the standard C/C++ way of doing a loop a fixed number of times.</p>

<hr>

<p>Also, most keypad libraries return <code>NO_KEY</code> if the user hasn't pressed a key.</p>

<p>You should test if <code>keypad.getKey()</code> returns <code>NO_KEY</code> and if so do nothing.</p>

<p>All your code would have done (if it had executed 4 times) was get 4 times <code>NO_KEY</code> (in a few microseconds) which would not be the correct password.</p>
