Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Alex LiJustway
Published © CC BY-NC

Alarm Protected Shelf

A shelf that rings an alarm when an item is removed

BeginnerWork in progress6 hours95
Alarm Protected Shelf

Things used in this project

Story

Read more

Schematics

Circuit Diagram

Code

Code

C/C++
the code I used to control the circuit
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <HX711_ADC.h>
//include libraries needed

#define calibration_factor -22233.33
#define LOADCELL_DOUT_PIN  12
#define LOADCELL_SCK_PIN  11
int alarmPin = 10;
int buzzerPin = 13;
//define pins used

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 colPins[ROWS] = {9, 8, 7, 6};
byte rowPins[COLS] = {5, 4, 3, 2};
//define keypad


Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2);
HX711_ADC scale(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);  
//initialize keypad, lcd, and load cells

char resetKey = '*';


String code = "1234#";
String input = "";
bool alarmActive = false;
float oldWeight;
int timeOff = 60;
//changable values

void setup() {
  Serial.begin(9600);
  scale.begin();
  scale.start(2000); 
  scale.setCalFactor(calibration_factor);
  scale.tare();
  //initialize scale

  lcd.init(); 
  lcd.backlight();
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("SECURITY ON");
  //initialize LCD

  oldWeight = scale.getData(); 
  //define initial weight

  pinMode(buzzerPin, OUTPUT);
  //initialize buzzer
}

void loop() {
  scale.update();
  char customKey = customKeypad.getKey();
  //check for key presses
  if (customKey) {
    Serial.println(customKey);
    if (customKey == resetKey) {
      input = "";
      lcd.clear();
      //clear input if reset key pressed
    }else {
      input += customKey;
      //add key press to input
    }
    lcd.setCursor(0, 1);
    lcd.print(input);
  }
  if(input==code){
    stopAlarm();
    disableSecurity();
    input = "";
    //disable security if correct code entered
    
  }
  if (fabs(scale.getData() - oldWeight) > 0.2 && !alarmActive) {

    soundAlarm();
    //activate alarm if abnormal weight
  }
}

void soundAlarm() {
  alarmActive = true;
  digitalWrite(alarmPin, HIGH);
  tone(buzzerPin, 1200);
  //activate strobe light and buzzer

  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("ALARM ACTIVE");
  lcd.setCursor(0, 1);
  lcd.print(input);
  //print alarm active on lcd
}

void stopAlarm(){
  noTone(buzzerPin);
  alarmActive = false;
  digitalWrite(alarmPin, LOW);
  //turn off strobe light and buzzer
}

void disableSecurity() {
  if (input == code) {
    for (int i = timeOff; i > 0; i--) {  
      //if code is correct
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("SECURITY OFF FOR");
      lcd.setCursor(0, 1);
      lcd.print(String(i) + " SECONDS");
      delay(1000);
      scale.update();
      oldWeight = scale.getData();
      //disable security and show countdown on LCD
    }
    scale.update();
    oldWeight = scale.getData();
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("SECURITY ON");
    //change oldWeight to after
  }
  
}

Credits

Alex Li
3 projects • 1 follower
I make things. Sometimes they work.
Contact
Justway
2 projects • 0 followers
We are 3D printing,CNC,sheet metal,injection molding manufacturing in China.Our price as low as $1 and fast delivery time.
Contact

Comments

Please log in or sign up to comment.