Alarm System code

Can someone please tell me why this code isn’t working? it’s compiling and uploading but does not show anything on the LCD or keypad.

#include <NewTone.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3f,20,4);
  
#include <Keypad.h>
 const int buzzer = A1;
 const int trigPin = A0;
 const int echoPin = A0;
long duration;
int distance, initialDistance, currentDistance, i;
int screenOffMsg =0;
String password="1234";
String tempPassword;
boolean activated = false; // State of the alarm
boolean isActivated;
boolean activateAlarm = false;
boolean alarmActivated = false;
boolean enteredPassword; // State of the entered password to stop the alarm
boolean passChangeMode = false;
boolean passChanged = false;
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keypressed;
//define the cymbols on the buttons of the keypads
char keyMap[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {5, 6, 7, 8}; //Row pinouts of the keypad
byte colPins[COLS] = {13, 12, 11, 10}; //Column pinouts of the keypad
Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS); 
 
void setup() { 
  lcd.begin(16,2); 
  pinMode(buzzer, OUTPUT); // Set buzzer as an output
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}
void loop() {
  if (activateAlarm) {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Alarm will be");
    lcd.setCursor(0,1);
    lcd.print("activated in");
   
    int countdown = 9; // 9 seconds count down before activating the alarm
    while (countdown != 0) {
      lcd.setCursor(13,1);
      lcd.print(countdown);
      countdown--;
      tone(buzzer, 700, 100);
      delay(1000);
    }
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Alarm Activated!");
    initialDistance = getDistance();
    activateAlarm = false;
    alarmActivated = true;
  }
  if (alarmActivated == true){
      currentDistance = getDistance() + 10;
      if ( currentDistance < initialDistance) {
        tone(buzzer, 1000); // Send 1KHz sound signal 
        lcd.clear();
        enterPassword();
      }
    }
  if (!alarmActivated) {
    if (screenOffMsg == 0 ){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("A - Activate");
      lcd.setCursor(0,1);
      lcd.print("B - Change Pass");
      screenOffMsg = 1;
    }
    keypressed = myKeypad.getKey();
     if (keypressed =='A'){        //If A is pressed, activate the alarm
      tone(buzzer, 1000, 200);
      activateAlarm = true;            
    }
    else if (keypressed =='B') {
      lcd.clear();
      int i=1;
      tone(buzzer, 2000, 100);
      tempPassword = "";
      lcd.setCursor(0,0);
      lcd.print("Current Password");
      lcd.setCursor(0,1);
      lcd.print(">");
      passChangeMode = true;
      passChanged = true;   
      while(passChanged) {      
      keypressed = myKeypad.getKey();
      if (keypressed != NO_KEY){
        if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
            keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
            keypressed == '8' || keypressed == '9' ) {
         tempPassword += keypressed;
         lcd.setCursor(i,1);
         lcd.print("*");
         i++;
         tone(buzzer, 2000, 100);
        }
      }
      if (i > 5 || keypressed == '#') {
        tempPassword = "";
        i=1;
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("Current Password");
        lcd.setCursor(0,1);
        lcd.print(">"); 
      }
      if ( keypressed == '*') {
        i=1;
        tone(buzzer, 2000, 100);
        if (password == tempPassword) {
          tempPassword="";
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print("Set New Password");
          lcd.setCursor(0,1);
          lcd.print(">");
          while(passChangeMode) {
            keypressed = myKeypad.getKey();
            if (keypressed != NO_KEY){
              if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
                  keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
                  keypressed == '8' || keypressed == '9' ) {
                tempPassword += keypressed;
                lcd.setCursor(i,1);
                lcd.print("*");
                i++;
                tone(buzzer, 2000, 100);
              }
            }
            if (i > 5 || keypressed == '#') {
              tempPassword = "";
              i=1;
              tone(buzzer, 2000, 100);
              lcd.clear();
              lcd.setCursor(0,0);
              lcd.print("Set New Password");
              lcd.setCursor(0,1);
              lcd.print(">");
            }
            if ( keypressed == '*') {
              i=1;
              tone(buzzer, 2000, 100);
              password = tempPassword;
              passChangeMode = false;
              passChanged = false;
              screenOffMsg = 0;
            }            
          }
        }
      }
    }
   }
 }
}
void enterPassword() {
  int k=5;
  tempPassword = "";
  activated = true;
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print(" *** ALARM *** ");
  lcd.setCursor(0,1);
  lcd.print("Pass>");
      while(activated) {
      keypressed = myKeypad.getKey();
      if (keypressed != NO_KEY){
        if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
            keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
            keypressed == '8' || keypressed == '9' ) {
          tempPassword += keypressed;
          lcd.setCursor(k,1);
          lcd.print("*");
          k++;
        }
      }
      if (k > 9 || keypressed == '#') {
        tempPassword = "";
        k=5;
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print(" *** ALARM *** ");
        lcd.setCursor(0,1);
        lcd.print("Pass>");
      }
      if ( keypressed == '*') {
        if ( tempPassword == password ) {
          activated = false;
          alarmActivated = false;
          noTone(buzzer);
          screenOffMsg = 0; 
        }
        else if (tempPassword != password) {
          lcd.setCursor(0,1);
          lcd.print("Wrong! Try Again");
          delay(2000);
          lcd.clear();
          lcd.setCursor(0,0);
          lcd.print(" *** ALARM *** ");
          lcd.setCursor(0,1);
          lcd.print("Pass>");
        }
      }    
    }
}
// Custom function for the Ultrasonic sensor
long getDistance(){
  int i=10;
  
  while( i<=10 ) {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration*0.034/2;
 // sumDistance += distance;
  }
 // int averageDistance= sumDistance/10;
  return distance;
}

Your LCD is not set correct, This line is saying
LiquidCrystal_I2C lcd(0x3f,20,4);
Then this lcd.begin(16,2);
Is it 16X2 or 20X4 ??

in the your code you are only defining pins 20 and 4, Your also not turning the back light on if it’s like mine.
Try using this in your code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address & pins

void setup() {
  lcd.begin (20,4); // for 20 X 4  LCD module change to 16,2 for 2x16 LCD
  lcd.setBacklightPin(3,POSITIVE); //set back light pin
  lcd.setBacklight(HIGH); //Turn the back light on the LCD
}

If that does not work the try
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address & pins
If that fails search for the I2C scanner on here this will show all the I2C devices attached to your Arduino

It is saying that positive was not declared in this scope. Let me try and makes the changes to my code like you said and see if it works

It is saying that positive was not declared in this scope

Copy and paste the exact error messages you are seeing

does not show anything on the LCD or keypad.

I wouldn't normally expect to see anything on a keypad, unless it had an integral display.

So i found l2C through scanner and it was 0x3F. I tried this code to see if it works but to no avail.
The LCD just flashes and goes black.

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

LiquidCrystal_I2C lcd(0x3F,16,2);  

void setup()
{
  lcd.init();                      // initialize the lcd 
 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.print("Hello, world!");
}

void loop()
{
}

Just try this then, I may have a different library to you installed
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3); // Set the LCD I2C address

This is the error code i receive

Arduino: 1.6.12 (Windows 10), Board: "Arduino/Genuino Uno"

sketch_mar31a:3: error: no matching function for call to 'LiquidCrystal_I2C::LiquidCrystal_I2C(int, int, int, int, int, int, int, int, int)'

 LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3);  // Set the LCD I2C address & pins

                                                   ^

C:\Users\me\AppData\Local\Temp\arduino_modified_sketch_700508\sketch_mar31a.ino:3:51: note: candidates are:

In file included from C:\Users\me\AppData\Local\Temp\arduino_modified_sketch_700508\sketch_mar31a.ino:2:0:

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:91:4: note: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, t_backlighPol)

    LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t En, uint8_t Rw, uint8_t Rs, 

    ^

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:91:4: note:   candidate expects 10 arguments, 9 provided

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:88:4: note: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, uint8_t)

    LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t En, uint8_t Rw, uint8_t Rs, 

    ^

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:88:4: note:   candidate expects 8 arguments, 9 provided

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:69:4: note: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, t_backlighPol)

    LiquidCrystal_I2C(uint8_t lcd_Addr, uint8_t En, uint8_t Rw, uint8_t Rs,

    ^

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:69:4: note:   candidate expects 6 arguments, 9 provided

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:67:4: note: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, uint8_t, uint8_t)

    LiquidCrystal_I2C( uint8_t lcd_Addr, uint8_t En, uint8_t Rw, uint8_t Rs);

    ^

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:67:4: note:   candidate expects 4 arguments, 9 provided

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:53:4: note: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, t_backlighPol)

    LiquidCrystal_I2C (uint8_t lcd_Addr, uint8_t backlighPin, t_backlighPol pol);

    ^

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:53:4: note:   candidate expects 3 arguments, 9 provided

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:51:4: note: LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t)

    LiquidCrystal_I2C (uint8_t lcd_Addr);

    ^

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:51:4: note:   candidate expects 1 argument, 9 provided

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:38:7: note: constexpr LiquidCrystal_I2C::LiquidCrystal_I2C(const LiquidCrystal_I2C&)

 class LiquidCrystal_I2C : public LCD 

       ^

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:38:7: note:   candidate expects 1 argument, 9 provided

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:38:7: note: constexpr LiquidCrystal_I2C::LiquidCrystal_I2C(LiquidCrystal_I2C&&)

C:\Users\me\Documents\Arduino\libraries\NewliquidCrystal/LiquidCrystal_I2C.h:38:7: note:   candidate expects 1 argument, 9 provided

exit status 1
no matching function for call to 'LiquidCrystal_I2C::LiquidCrystal_I2C(int, int, int, int, int, int, int, int, int)'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I’ve had a look for the I2C library and I now understand where you are getting your setup from which is different to mine,
Has your LCD got a trim pot on the back for the contrast ?
What type of LCD/controller is on it has a back pack ??
You could try my library attached, Just cut and past your library to another folder, Make Arduino IDE is not open, unzip my library and copy and paste it into the Arduino libraries folder located in my documents, Then try this code I used to test my LCD and have just tried it on my setup all working, If this does not work then it must be contrast issue.

//####################################################
//# Include all the libraries                        #
//####################################################
#include <LiquidCrystal_I2C.h> // I2C LCD display only for intial setup
#include  <Wire.h> //Wire libary 

//####################################################
//#  Setup first power up                            #
//####################################################
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
void setup() {

lcd.begin(16,2);// General LCD 2 x 8 with backlight
lcd.setBacklightPin(3,POSITIVE); //set back light pin
  lcd.setBacklight(HIGH); //Turn the back light on the LCD
  lcd.setCursor(0, 0);
  lcd.print("POWERING");
  lcd.setCursor(0, 1);
  lcd.print("  UP  ");// Print something on the display to show
  delay(1000);                                   // it's working.
  lcd.clear();


}
//####################################################
//#  Main Lop starts here                            #
//####################################################
void loop() {
 lcd.setCursor(0, 0);
lcd.print("I CAN SEE TEXT");
lcd.setCursor(0, 1);
lcd.print("line 2 working");

 }

LiquidCrystal_I2C.zip (20.3 KB)

Hi,
You need to wire the I2C like this. Try it and see if it work for you,
I do not why they not allow you to define the SDA and the clock pins in the sketch. It is forced to used A4 and A5 in the library.

//Warning you must connect the Sda to pin A4 and the Clock to pin A5