The program is not working as expected

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.i done many changes but the programe is not working as i expected it to.

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

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

What does it actually do that is wrong ?

So describe:

  • What you expect it to do
  • What it actually does

i am very bad at programming can you pls guide me

I need to make the vibration sesor to detect input and if it does the buzzer should get activated......but when i compiled the program it doesnt have an error and after i run it it doesnt work properly.

your knowledge about programming is improvable.
But what you can do right now is writing down a detailed description of what your program actually does.

Does the display stay dark and shows nothing?
Does your buzzer keep buzzing all the time or does the buzzer nothing at all?

can you type any code and this makes the buzzer stop ?

Give a description of what your program does.
Or if you are lazy about typing this record a video
and upload it to youtube

best regards Stefan

The truth is i copied different part of program from somewhere else and edited using my minimal programming skill.

There is no importance with the display but i used the alarmIsActive as global boolian variable.If it is false the system resets.If vibration sensor gets value the buzzer should activate.No delay just need to activate buzzer.

If this happens when we input a password of 888 through keypad the buzzer should stop playing.

This is the logic i implemented in the program.And i used different part of the program from example sketches.And i dont know what went wrong.

As long as you don't write a description what your actual program does you force other users
to buy all the components you are using
wire it all together and then upload the program to see how the program behaves

or second option you force all users to emulate your whole program in their heads.
This is much more work than reading a description of what your program does.

Whatever it is: write a description what your program does.
If it does not even compile write it!
add a question that is as specific as you can ask!

You sketch never sets alarmIsActive to true, so your code never reads the vibration pin.

Also, why are you initializing the SPI? You are not using it.

Start small, and learn as you go.

Get the vibration sensor working alone, then get the buzzer working alone. There are plenty of examples for each on the web. Once you understand how those two things work, put them together in one program.

Then work on the keypad and display, again alone.

1 Like
alarmIsActive == false;

This is incorrect syntax here. One = for assiggnment two == for comparison. You are assigning a value here.

yeah thats the problem.....what should i do to make the vibration sensor read the input and acivate buzzer and if i enter password buzzer should stop.But in my program while i take off first if statement vibration is detected buzzer gets actvated....but not detecting keypad.

it kind of worked while entering the password buzzer stopped but nothing is shown on the display.

I second that
and as one option to learn programming
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

That is not assignment. It is a comparison with the result discarded. Maybe the OP wanted an assignment, but it didn't happen in that statement.

That is because you are only checking the keypad if alarmIsActive is false. Lots of these things should be done outside your if() statements.

But like others have stated, take a step back and start smaller/simpler.

And it's a correct syntax, it's the semantics that is wrong. :wink:

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.