I'm trying to create a code that whenever I type in a numer in the keypad it will disable my buzzer

I've done a lot with the code so it may be completely ruined from all the tries, but here's where I am now:

"And note that I have to have a loop whenever the smoke is detected, as I am trying to build a smoke sensor so I will only be able to disable it when the code is put in, even though the smoke is gone."

#include <Keypad.h>
int piezo = 4;
int led = 3;
int sensor = A0;
int sensorValue = 0;
const byte ROWS = 4;
const byte COLS = 4;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
byte rowPins[ROWS] = {12,11,10,9};
byte colPins[COLS] = {8,7,6,5};
Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
const String password = "1234";
String input_password;
void setup()
{
Serial.begin(9600);
digitalWrite(led, 0);
}
void loop()
{
char key = keypad.getKey();
sensorValue = analogRead(sensor);
Serial.println(sensorValue, DEC);
  while (sensorValue > 100)
{
  char key = keypad.getKey();
  Serial.println(key);
  digitalWrite(piezo, 1);
  tone(4, 2500);
  delay(1000);
  noTone(4);
  delay(250);
  if (key != NO_KEY){
    Serial.println(key); //uncomment line to test input
    switch (key) {
      case '5':
        digitalWrite(piezo, 0);
        break;

  } 

}
}
}
    

Welcome to the forum

Let's start with the basics

What type of buzzer do you have ?
Does it buzz if you connect 5V to its terminals (an active buzzer) or does it need to be driven with the tone() function (a passive buzzer) ?

I ask because your sketch tries to do both

Is it just one digit on the keypad to turn it off or a multi-character code?

You could have a global state variable (a bool for example) that will tell you if the alarm was triggered, in which case you check for the key.

void loop() {
  if (alarmOn) {
    // check for key and turn buzzer off when detected and set alarmOn to false
  } else {
    // check for smoke and turn buzzer on when detected and set alarmOn to true
  }
}

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