0

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);      
    }




  }
      }
  • 1
    Please post a minimal, complete, verifiable example. – Mark Smith Mar 3 '17 at 20:51
  • You might want to add some delay, or wait till the button is released. Otherwise, when you press for example 1, it will keep appending 1s till it reaches 4. – Gerben Mar 4 '17 at 12:34
1
  1. Without knowing how the pass.* works, it will be impossible to pinpoint the problem.
  2. 'key' gets an ASCII value. Is that would you are expecting?
  3. Does pass.* reset (start over) once 4 digits?
  4. Just a guess, but I believe that *.waitForKey() does not consume the key.
  5. The algorithm only gets the key four times; then, it checks.
  • Password pass = Password( "1331" ); that`s how pass. works. – Kalin Lalov Mar 3 '17 at 22:26
  • Could you just write me another algorithm to check for the password? I think this one is not good. – Kalin Lalov Mar 3 '17 at 22:28
  • There's nothing wrong with the excerpt you posted. The problem is with code you haven't posted. As I requested, please provide a minimal, complete, verifiable example of your code, and then someone may be able to help. We don't know how you have your equipment connected, what libraries you're using and so on, so we cannot test our code or be sure it will work for you. – Mark Smith Mar 4 '17 at 7:28
1

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.

0

This is classical indexing problem:

while with <= 4 actually runs five (5) rounds.

So, use < 4.

  • Ok. I didint have access to my pc so I wrote what I remembered of my code. Here is the full one – Kalin Lalov Mar 4 '17 at 9:12
  • ... Unless i starts at 1, which we can't tell because we have a snippet of code without context. – Mark Smith Mar 4 '17 at 9:16
  • I wrote what I remembered of my code - please don't post "remembered" code. Wait until you are at your PC and post the actual code. Posting "remembered" code just wastes everyone's time. – Nick Gammon Mar 4 '17 at 10:01
  • Assuming i starts at zero, while(i<=4,i++) executes zero times, not 5 times. – Nick Gammon Mar 4 '17 at 10:21
0

In case anyone was wondering, heres the code I came up with and now it works.

#include <Keypad.h>
int sensor = 11;
int buzzer = 10;
int red = 12;
int i = 0;
String Pass = "1331";
String tempPass = "";

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);
StartingBuzz();
keypad.waitForKey();
digitalWrite(red,HIGH);
digitalWrite(buzzer,HIGH);
  delay(200);
  digitalWrite(buzzer, LOW);
  delay(60000);
}

void StartingBuzz()
{
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);
  }

void UnlockBuzz()
{
  delay(200);
       tone(buzzer,3250,200);
       delay(300);
       tone(buzzer,3500,200);
       delay(300);
       tone(buzzer,4000,200);
  }

void loop() {
  i=0;//resets the password-checking loop.
  tempPass = "";//resets the guessed password.
  char state = digitalRead(sensor);
  if (state == HIGH)
  {
    digitalWrite(buzzer, HIGH);
    A:
    {
    while(i < 4)
    {
    char key = keypad.getKey();
    if(key != NO_KEY)
    {
      tempPass += key;
      i++;
      }
    }
    }
    if(Pass == tempPass)
    {       
      digitalWrite(buzzer, LOW);
      digitalWrite(red, LOW);
      UnlockBuzz();
      keypad.waitForKey();
      digitalWrite(buzzer,HIGH);
      delay(200);
      digitalWrite(buzzer, LOW);
      digitalWrite(red, HIGH);
      delay(60000);      
    }
    else
    { tempPass = "";
       i=0;
      goto A;
      }




  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.