Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Password.h>
- #include <Keypad.h>
- Password password = Password( "0000" );
- const byte ROWS = 4;
- const byte COLS = 3;
- char keys[ROWS][COLS] = {
- {
- '1','2','3' }
- ,
- {
- '4','5','6' }
- ,
- {
- '7','8','9' }
- ,
- {
- '*','0','#' }
- };
- byte rowPins[ROWS] = {
- 3, 4, 5, 6};
- byte colPins[COLS] = {
- 7, 8, 9};
- Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- byte Closed = 0;
- byte Gerkon = 2;
- byte LedRed = 12;
- byte LedGreen = 13;
- byte MotorPin1 = 0;
- byte MotorPin2 = 1;
- byte MotorPinGo = 11;
- boolean programMode = false;
- int speakerPin = 10;
- void setup(){
- pinMode(MotorPin1, OUTPUT);
- pinMode(MotorPin2, OUTPUT);
- pinMode(LedRed, OUTPUT);
- pinMode(LedGreen, OUTPUT);
- pinMode(speakerPin, OUTPUT);
- pinMode(Gerkon, INPUT);
- digitalWrite(Gerkon, HIGH);
- digitalWrite(LedRed, LOW);
- digitalWrite(LedGreen, LOW);
- digitalWrite(MotorPin1, LOW);
- digitalWrite(MotorPin2, LOW);
- digitalWrite(MotorPinGo, LOW);
- keypad.addEventListener(keypadEvent);
- keypad.setDebounceTime(100);
- startupSound();
- }
- void loop(){
- char key = keypad.getKey();
- if (Closed==1) {
- digitalWrite(LedRed,HIGH);
- digitalWrite(LedGreen,LOW);
- }
- else{
- digitalWrite(LedRed,LOW);
- digitalWrite(LedGreen,HIGH);
- }
- }
- void keypadEvent(KeypadEvent key){
- switch (keypad.getState()){
- case PRESSED:
- switch (key){
- case '#':
- guessPassword();
- digitalWrite(LedRed, LOW);
- break;
- case '*':
- CloseBolt();
- digitalWrite(LedRed, LOW);
- digitalWrite(LedGreen, LOW);
- break;
- default :
- if (key != NO_KEY){
- }
- password.append(key);
- digitalWrite(LedRed, HIGH);
- analogWrite(speakerPin, 128);
- break;
- }
- break;
- case RELEASED:
- switch (key){
- default :
- digitalWrite(LedRed, LOW);
- analogWrite(speakerPin, 0);
- break;
- }
- break;
- }
- }
- void guessPassword(){
- if (password.evaluate()){
- if (Closed==1) {
- digitalWrite(LedGreen,HIGH);
- OpenBolt();
- digitalWrite(LedGreen,LOW);
- password.reset();
- }
- }
- else{
- digitalWrite(LedGreen,LOW);
- password.reset();
- errorSound();
- }
- }
- void startupSound()
- {
- int note = (1/587.33)*500000;
- for (int _=0;_<100000/note;_++){
- delayMicroseconds(note);
- digitalWrite(speakerPin, HIGH);
- delayMicroseconds(note);
- digitalWrite(speakerPin, LOW);
- }
- note = (1/523.25)*500000;
- for (int _=0;_<100000/note;_++){
- delayMicroseconds(note);
- digitalWrite(speakerPin, HIGH);
- delayMicroseconds(note);
- digitalWrite(speakerPin, LOW);
- }
- note = (1/659.26)*500000;
- for (int _=0;_<100000/note;_++){
- delayMicroseconds(note);
- digitalWrite(speakerPin, HIGH);
- delayMicroseconds(note);
- digitalWrite(speakerPin, LOW);
- }
- }
- void errorSound()
- {
- int n = 2000;
- for (int vez=0;vez<2;vez++){
- for (int _=0;_<30;_++){
- delayMicroseconds(n);
- digitalWrite(speakerPin, HIGH);
- delayMicroseconds(n);
- digitalWrite(speakerPin, LOW);
- }
- delay(50);
- }
- }
- void openSound()
- {
- int n = 300;
- for (int vez=0;vez<3;vez++){
- for (int _=0;_<100;_++){
- delayMicroseconds(n);
- digitalWrite(speakerPin, HIGH);
- delayMicroseconds(n);
- digitalWrite(speakerPin, LOW);
- }
- delay(100);
- }
- }
- void OpenBolt()
- {
- if (Closed == 1){
- digitalWrite(MotorPin1, LOW);
- digitalWrite(MotorPin2, HIGH);
- digitalWrite(MotorPinGo, HIGH);
- delay(30);
- digitalWrite(MotorPin1, LOW);
- digitalWrite(MotorPin2, LOW);
- digitalWrite(MotorPinGo, LOW);
- Closed=0;
- openSound();
- }
- }
- void CloseBolt()
- {
- if (Closed == 0){
- closeSound1();
- do
- {
- delay(1);
- } while (digitalRead(Gerkon)==1);
- delay(3000); //wait 3 seconds
- digitalWrite(MotorPin1, HIGH);
- digitalWrite(MotorPin2, LOW);
- digitalWrite(MotorPinGo, HIGH);
- delay(30);
- digitalWrite(MotorPin1, LOW);
- digitalWrite(MotorPin2, LOW);
- digitalWrite(MotorPinGo, LOW);
- Closed=1;
- closeSound2();
- }
- }
- void closeSound1()
- {
- int n = 200;
- for (int vez=0;vez<2;vez++){
- for (int _=0;_<100;_++){
- delayMicroseconds(n);
- digitalWrite(speakerPin, HIGH);
- delayMicroseconds(n);
- digitalWrite(speakerPin, LOW);
- }
- delay(100);
- }
- }
- void closeSound2()
- {
- int n = 450;
- for (int _=0;_<110;_++){
- delayMicroseconds(n);
- digitalWrite(speakerPin, HIGH);
- delayMicroseconds(n);
- digitalWrite(speakerPin, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement