Problem with code...Need help please

Hi there.

At first, I want to tell that I am a newbie, so please don't shout at me :slight_smile:

I am trying to write a code, that activates a magnetic door sensor after an alarm is switched on. For tak I wrote a script - tookexamples from this forum, but after line void checkPassword(){
** if (password.evaluate()) //if password is correct**
** {** i can't make my sensor to work at all....

Can any one help me please?

The source code:

//check the entered password
void checkPassword(){
if (password.evaluate()) //if password is correct
{
//int Magnet;
lcd.setCursor(0,1);
lcd.print("Correct PIN"); //print a message on serial monitor
digitalWrite(13, HIGH); //turn ON an LED
delay(500);
digitalWrite(13,LOW);
//if (digitalRead(12)==HIGH){
// digitalWrite(11, stan);
lcd.clear ();
lcd.setCursor(0,0);
lcd.print ("Alarm 01");
digitalWrite(11,HIGH);
delay (1500);
digitalWrite(11,LOW);
int Magnet=analogRead(A1);
digitalWrite(13,Magnet);
digitalWrite(11,Magnet);
//digitalRead(12);

// else{}

password.reset(); //reset password

}
else
{
lcd.setCursor(0,1);
lcd.print("Zly PIN"); //if password is wrong
digitalWrite(13, HIGH); //blink an LED
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
password.reset(); //reset password

}
}

This is not a complete sketch, don't post code fragments if you're not absolutely sure they show the complete problem.
And use code tags to post code otherwise the forum system may hide parts of it.

... in addition to what @pylon said ...

does the project work without the password stuff?

If not, first get your project to work without any attempt to do passwords. When that works, think about how to add a password system.

It would probably also be a good idea to start the password system with a short sketch that just blinks the onboard LED when the password is accepted. And start with visible passwords or it will be a nightmare to debug.

...R

Hi there again!

Thnx for your reply, but tell you the truth, I am giving UP =(

Below is my script, please if anyone can tell me, what am I doing wrong? I just want to enter a password, after enetering it, the reed switch goes "on", so whenever I open a door, windows, etc, the "alarm" goes on. All it does, is let's me to enter my password, and reads only once my reed switch. If I add this to void loop (), everything works great, but then my keyboard and LCD want work as I want it to.... Hope you can understand me (sorry 4 my english - Poland ;))

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
#include <Password.h>

Password password = Password ("1234");

LiquidCrystal_I2C lcd(0x21,16,2); //PCF8574 

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] = {6,7,8,9}; //connect to the row pinouts of the keypad 
byte colPins[COLS] = {2,3,4,5}; //connect to the column pinouts of the keypad 


Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );


void setup() {
  //Serial.begin (9600);
  lcd.init ();
  lcd.backlight ();
  lcd.print ("ENTER PIN");
  lcd.setCursor (0,1);
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad

  
  pinMode(13, OUTPUT);   //led
  pinMode(12, INPUT);    //reed switch for DOOR / WINDOW
  digitalWrite(12, HIGH);  //reed switch for DOOR / WINDOW
  pinMode(11,OUTPUT); //buzzer
}

void loop(){
  
  char key = keypad.getKey();
  
  }
  //check the keypad events
void keypadEvent(KeypadEvent keyPress)
{
  switch (keypad.getState())
  {
    case PRESSED:
    lcd.print(keyPress); //print the keypress
    switch (keyPress)
    {
	  case '#':
            checkPassword();
          break;

          case '*':
            password.reset();
            digitalWrite(13, LOW);
            lcd.setCursor(0,1);
            lcd.print("                ");
          break;

          default:
          password.append(keyPress);
     }
  }
}

//check the entered password
void checkPassword(){
  if (password.evaluate()) //if password is correct
  {
    lcd.setCursor(0,1);
    lcd.print("CORRECT PIN"); //print a message on LCD
    digitalWrite(13, HIGH); //turn ON an LED
    
    // HERE BELOW I HAVA A PROBLEM - ONLY WORKS ONCE, AND ONLY CHECKS PIN 12 ONCE - what am I doing wrong?
    int states;
    states = digitalRead(12);
    digitalWrite(13, states); //LIGHT LED ON IF WINDOWS OPEN
    digitalWrite(11, states); //TURN BUZZER ON, IF WINDOWS OPEN
    password.reset(); //reset password
    
    
  }
  else
  {
    lcd.setCursor(0,1);
    lcd.print(" WRONG PIN"); //if password is wrong
    digitalWrite(13, HIGH); //blink an LED
    delay(100);
    digitalWrite(13, LOW);
    delay(100);
    digitalWrite(13, HIGH);
    delay(100);
    digitalWrite(13, LOW);
    password.reset(); //reset password
  }
}

You have at least two libraries used which are not part of the IDE and there is no link to them (I forgive about the I2C version of the LiquidCrystal library). If we can assume that the keypad library also works you must at least provide the link to your password library because it may be the cause of your problems.

    digitalWrite(13, HIGH); //turn ON an LED
    
    // HERE BELOW I HAVA A PROBLEM - ONLY WORKS ONCE, AND ONLY CHECKS PIN 12 ONCE - what am I doing wrong?
    int states;
    states = digitalRead(12);
    digitalWrite(13, states); //LIGHT LED ON IF WINDOWS OPEN

Are you sure about setting the pin 13 this way? If states is HIGH it does not change and if states is LOW you never see the initial HIGH.
Use Serial and Insert Serial...( states) to really know what states is.

I am using these libraries - http://playground.arduino.cc/Code/Password and http://playground.arduino.cc/uploads/Code/keypad.zip.

As to "my" source code, I still don't know what's wrong. As I wrote earlier, the code works fine without these few lines (

    int states;
    states = digitalRead(12);
    digitalWrite(13, states); //LIGHT LED ON IF WINDOWS OPEN
    digitalWrite(11, states); //TURN BUZZER ON, IF WINDOWS OPEN
    password.reset(); //reset password
  • I want them, cause that's my point - I want an easy way to monitor my doors and windows at home :slight_smile:

When I write only this code, everything also works well - when I open a window, the alarm goes ON, when I close it, it goes OFF:

 void setup() {
    pinMode(13, OUTPUT);   //led
    pinMode(12, INPUT);    //reed switch (magnet)
    digitalWrite(12, HIGH);  //reed swith HIGH
    pinMode(11,OUTPUT); //buzzer
}

void loop(){
  int state;
  state = digitalRead(12);  
  digitalWrite(13, state);
  digitalWrite(11, state);
}

I think (I don't do programing, so I think it maybe this), it's because, in the above code, everything is in void loop (), and in my new code with a keypad with password and LCD, my reed switch code is in line

void checkPassword(){
  if (password.evaluate()) //if password is correct

and it does'nt "loop". Is it possible I'm right? If so, please tell me, what should I write in the code, so it would start to work...

Thanks again for all your help!

Please describe in detail what happens if you run the posted code. You wrote that it works once but after that only partly. What does happen if doesn't work? Does it still write "CORRECT PIN" on the LCD? Just doesn't change anything on pin 13? Notice that we are not familiar with your project so you have to provide more information.