Need help very badly

I am having issues with my program.
The modules are keypad,Buzzer,vibration sensor,LCD i2c .

At first LCD should show locker.
What i need is if vibration sensor picks an input the buzzer should act non stop.The LCD should show warning. And if a password of '888' is pressed the LCD should come back to normal and Buzzer should stop.
But my code doesnt have any error.But it is not working with my modules.

full code i wrote :

#include <Keypad.h>
#include <SPI.h>
// Create instances
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
char initial_password[4] = {'8', '8', '8'};  // Variable to store initial password
char password[4];   // Variable to store users password
char key_pressed = 0; // Variable to store incoming keys
uint8_t i = 0;  // Variable used for counter
// defining how many rows and columns our keypad have
const byte rows = 4;
const byte columns = 4;
// Keypad pin map
char hexaKeys[rows][columns] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};


// Initializing pins for keypad
byte row_pins[rows] = {9, 8, 7, 6};
byte column_pins[columns] = {5, 4, 3};
// Create instance for keypad
Keypad keypad_key = Keypad( makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);






constexpr uint8_t vib_pin = 7;
constexpr uint8_t buzz = 12;

void setup() {

  pinMode(vib_pin, INPUT);
  pinMode(buzz, OUTPUT);

  lcd.init();   // LCD screen
  lcd.backlight();
  SPI.begin();      // Init SPI bus
  lcd.clear(); // Clear LCD screen

}

void loop()
{


  int val;
  val = digitalRead(vib_pin);
  if (val == 1)
  {
    digitalWrite(buzz, HIGH);
    {
      key_pressed = keypad_key.getKey(); // Storing keys
      if (key_pressed)
      {
        password[i++] = key_pressed; // Storing in password variable
        lcd.print("*");
      }
      if (i == 3) // If 3 keys are completed
      {
        delay(200);
        if (!(strncmp(password, initial_password, 3))) // If password is matched
        {
          lcd.clear();
          lcd.print("Pass Accepted");
          digitalWrite(buzz, LOW);
          lcd.clear();
          i = 0;

        }
        else    // If password is not matched
        {
          lcd.clear();
          lcd.print("WARNING");


          lcd.clear();
          i = 0;

        }
      }
    }
  }
}

Post what you have done… no one will help you cheat. If you have not done anything, then face the consequences of your decision…

1 Like

I was doing research on the topic but i am very bad at programming but good at hardware stuffs.

You still have a bit of time. Start by learning how to manage the components individually. There are zillions tutorials for each

—-
please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It’s barely readable as it stands. (also make sure you indented the code in the IDE before copying, that’s done by pressing ctrlT on a PC or cmdT on a Mac)

i did like that....i cannot find any error in the code....i think it is a logic error

You are waiting for 4 keys and your pwd is 888 ?

so i should change into 3 right...?

Worth trying (modify also the 4 in strncmp() test)

nah still the same the buzzer is making sound if the vibration is detected but it is not accepting the password.

Don’t modify the code of the first post, the discussion does not mean anything now…

i m sorry i didnt knew that ....i will revert it...if it is a problem :cold_face:

The keypad is only active if the digitalRead stays triggered. You should not loop this part. If the alarm is triggered, then you should only deal with the keypad

i wanted to enter the passward only if the buzzer is triggered....so what is the possible things i should i do to make the program currect...pls guide me

Create a global Boolean variable alarmIsActive, initialized to false.
In the loop you test this variable.

  • If it’s false you do the digitalRead and make the variable true if it’s triggered and activate the buzzer
  • else you listen to the keypad and reset the alarmIsActive variable to false and turn the buzzer off when you have the right pwd

I think you meant this way...the code contains no error....let me test with components

#include <Keypad.h>
#include <SPI.h>
// Create instances
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
char initial_password[4] = {'8', '8', '8'};  // Variable to store initial password
char password[4];   // Variable to store users password
char key_pressed = 0; // Variable to store incoming keys
uint8_t i = 0;  // Variable used for counter
// defining how many rows and columns our keypad have
const byte rows = 4;
const byte columns = 4;
// Keypad pin map
char hexaKeys[rows][columns] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};


// Initializing pins for keypad
byte row_pins[rows] = {9, 8, 7, 6};
byte column_pins[columns] = {5, 4, 3};
// Create instance for keypad
Keypad keypad_key = Keypad( makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);

boolean alarmIsActive = false;




constexpr uint8_t vib_pin = 7;
constexpr uint8_t buzz = 12;

void setup() {

  pinMode(vib_pin, INPUT);
  pinMode(buzz, OUTPUT);

  lcd.init();   // LCD screen
  lcd.backlight();
  SPI.begin();      // Init SPI bus
  lcd.clear(); // Clear LCD screen

}

void loop()
{

  if (alarmIsActive = false)
  {
    int val;
    val = digitalRead(vib_pin);
    if (val == 1)
    {
      digitalWrite(buzz, HIGH);
    }
  }
  if (alarmIsActive = true)
  {

    key_pressed = keypad_key.getKey(); // Storing keys
    if (key_pressed)
    {
      password[i++] = key_pressed; // Storing in password variable
      lcd.print("*");
    }
    if (i == 3) // If 3 keys are completed
    {
      delay(200);
      if (!(strncmp(password, initial_password, 3))) // If password is matched
      {
        lcd.clear();
        lcd.print("Pass Accepted");
        digitalWrite(buzz, LOW);
        lcd.clear();
        i = 0;
        alarmIsActive = false;

      }
      else    // If password is not matched
      {
        lcd.clear();
        lcd.print("WARNING");


        lcd.clear();
        i = 0;
        alarmIsActive = false;
      }
    }
  }
}

Comparison is == … :wink:
I also said to change the value of alarmIsActive when you trigger
An if-else is simpler than two if when it’s the opposite value you are testing (and a bool is already a truth value so == true is not needed and to test if it’s false we write if (! alarmIsActive ) {…

the program still doesnt work :persevere:

#include <Keypad.h>
#include <SPI.h>
// Create instances
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
char initial_password[4] = {'8', '8', '8'};  // Variable to store initial password
char password[4];   // Variable to store users password
char key_pressed = 0; // Variable to store incoming keys
uint8_t i = 0;  // Variable used for counter
// defining how many rows and columns our keypad have
const byte rows = 4;
const byte columns = 4;
// Keypad pin map
char hexaKeys[rows][columns] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};


// Initializing pins for keypad
byte row_pins[rows] = {9, 8, 7, 6};
byte column_pins[columns] = {5, 4, 3};
// Create instance for keypad
Keypad keypad_key = Keypad( makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);

boolean alarmIsActive = false;




constexpr uint8_t vib_pin = 2;
constexpr uint8_t buzz = 12;

void setup() {

  pinMode(vib_pin, INPUT);
  pinMode(buzz, OUTPUT);

  lcd.init();   // LCD screen
  lcd.backlight();
  SPI.begin();      // Init SPI bus
  lcd.clear(); // Clear LCD screen

}

void loop()
{

  if (alarmIsActive == true)
  {
    int val;
    val = digitalRead(vib_pin);
    if (val == 1)
    {
      digitalWrite(buzz, HIGH);
      alarmIsActive == false;
    }
  }
  if (! alarmIsActive)
  {

    key_pressed = keypad_key.getKey(); // Storing keys
    if (key_pressed)
    {
      password[i++] = key_pressed; // Storing in password variable
      lcd.print("*");
    }
    if (i == 3) // If 3 keys are completed
    {
      delay(200);
      if (!(strncmp(password, initial_password, 3))) // If password is matched
      {
        lcd.clear();
        lcd.print("Pass Accepted");
        digitalWrite(buzz, LOW);
        lcd.clear();
        i = 0;
        alarmIsActive = false;

      }
      else    // If password is not matched
      {
        lcd.clear();
        lcd.print("WARNING");


        lcd.clear();
        i = 0;
        alarmIsActive = false;
      }
    }
  }
}

Right, so now is the time for you to learn to debug it. Start by inserting Serial.println() statements at various points in the code to check whether you are reaching them, and what the values of any variables are.

For example, try something like:

Serial.print(password);
Serial.print(" ");
Serial.println(initial_password)

...and put that after

if (i == 3) //If 3 keys are completed
{

That will tell you what it "thinks" the entered password is, and what it should be.

You use the Boolean the opposite way. When the alarm is active you neeed to check the keypad, when it’s not you need to check if it’s triggered


if (alarmIsActive) {
  // here check keypad
} else {
  // here check if alarm gets triggered
}

And change the name of the i Variable to something that makes sense, like pwdCounter or pwdIndex… try to make your program more readable

when changed the comparison sighn the keypad worked....while i enter password buzzer stopped....but nothing is showing on the display....

#include <Keypad.h>
#include <SPI.h>
// Create instances
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
char initial_password[4] = {'8', '8', '8'};  // Variable to store initial password
char password[4];   // Variable to store users password
char key_pressed = 0; // Variable to store incoming keys
uint8_t pwdINTEX = 0;  // Variable used for counter
// defining how many rows and columns our keypad have
const byte rows = 4;
const byte columns = 4;
// Keypad pin map
char hexaKeys[rows][columns] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};


// Initializing pins for keypad
byte row_pins[rows] = {9, 8, 7, 6};
byte column_pins[columns] = {5, 4, 3};
// Create instance for keypad
Keypad keypad_key = Keypad( makeKeymap(hexaKeys), row_pins, column_pins, rows, columns);

boolean alarmIsActive = false;




constexpr uint8_t vib_pin = 2;
constexpr uint8_t buzz = 12;

void setup() {

  pinMode(vib_pin, INPUT);
  pinMode(buzz, OUTPUT);

  lcd.init();   // LCD screen
  lcd.backlight();
  SPI.begin();      // Init SPI bus
  lcd.clear(); // Clear LCD screen

}

void loop()
{


  int val;
  val = digitalRead(vib_pin);
  if (val == 1)
  {
    digitalWrite(buzz, HIGH);
    lcd.clear();
    lcd.println("U R undr thft");
    alarmIsActive = true;
  }

  if (alarmIsActive = true)
  {

    key_pressed = keypad_key.getKey(); // Storing keys
    if (key_pressed)
    {
      password[pwdINTEX++] = key_pressed; // Storing in password variable
      lcd.print("*");
    }
    if (pwdINTEX == 3) // If 3 keys are completed
    {
      delay(200);
      if (!(strncmp(password, initial_password, 3))) // If password is matched
      {
        lcd.clear();
        lcd.print("Pass Accepted");
        digitalWrite(buzz, LOW);
        
      
        lcd.clear();
        pwdINTEX = 0;
        alarmIsActive = false;

      }
      else    // If password is not matched
      {
        lcd.clear();
        lcd.print("WARNING");


        lcd.clear();
        pwdINTEX = 0;
        alarmIsActive = false;
      }
    }
  }
}