Advertisement
Guest User

codelock arduino

a guest
Jan 9th, 2012
6,761
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Password.h>
  2. #include <Keypad.h>
  3.  
  4.  
  5. Password password = Password( "0000" );
  6.  
  7. const byte ROWS = 4;
  8. const byte COLS = 3;
  9. char keys[ROWS][COLS] = {
  10.   {
  11.     '1','2','3'  }
  12.   ,
  13.   {
  14.     '4','5','6'  }
  15.   ,
  16.   {
  17.     '7','8','9'  }
  18.   ,
  19.   {
  20.     '*','0','#'  }
  21. };
  22. byte rowPins[ROWS] = {
  23.   3, 4, 5, 6};
  24. byte colPins[COLS] = {
  25.   7, 8, 9};
  26.  
  27. Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
  28.  
  29. byte Closed = 0;
  30. byte Gerkon = 2;
  31. byte LedRed = 12;
  32. byte LedGreen = 13;
  33. byte MotorPin1 = 0;
  34. byte MotorPin2 = 1;
  35. byte MotorPinGo = 11;
  36.  
  37. boolean programMode = false;
  38.  
  39. int speakerPin = 10;
  40.  
  41. void setup(){
  42.   pinMode(MotorPin1, OUTPUT);
  43.   pinMode(MotorPin2, OUTPUT);
  44.   pinMode(LedRed, OUTPUT);
  45.   pinMode(LedGreen, OUTPUT);
  46.   pinMode(speakerPin, OUTPUT);
  47.   pinMode(Gerkon, INPUT);
  48.   digitalWrite(Gerkon, HIGH);
  49.   digitalWrite(LedRed, LOW);
  50.   digitalWrite(LedGreen, LOW);
  51.   digitalWrite(MotorPin1, LOW);
  52.   digitalWrite(MotorPin2, LOW);
  53.   digitalWrite(MotorPinGo, LOW);
  54.  
  55.   keypad.addEventListener(keypadEvent);
  56.   keypad.setDebounceTime(100);
  57.   startupSound();
  58.  
  59. }
  60.  
  61. void loop(){
  62.   char key = keypad.getKey();
  63.  
  64.   if (Closed==1) {
  65.     digitalWrite(LedRed,HIGH);
  66.     digitalWrite(LedGreen,LOW);
  67.   }
  68.   else{
  69.     digitalWrite(LedRed,LOW);
  70.     digitalWrite(LedGreen,HIGH);
  71.   }
  72.  
  73. }
  74.  
  75. void keypadEvent(KeypadEvent key){
  76.  
  77.   switch (keypad.getState()){
  78.   case PRESSED:
  79.     switch (key){
  80.     case '#':
  81.       guessPassword();
  82.       digitalWrite(LedRed, LOW);
  83.       break;
  84.     case '*':
  85.       CloseBolt();
  86.       digitalWrite(LedRed, LOW);
  87.       digitalWrite(LedGreen, LOW);
  88.       break;
  89.     default :
  90.       if (key != NO_KEY){
  91.       }
  92.       password.append(key);
  93.       digitalWrite(LedRed, HIGH);
  94.       analogWrite(speakerPin, 128);
  95.       break;
  96.     }
  97.     break;
  98.   case RELEASED:
  99.     switch (key){
  100.     default :
  101.       digitalWrite(LedRed, LOW);
  102.       analogWrite(speakerPin, 0);
  103.       break;
  104.     }
  105.     break;
  106.   }
  107. }
  108. void guessPassword(){
  109.   if (password.evaluate()){
  110.     if (Closed==1) {
  111.       digitalWrite(LedGreen,HIGH);
  112.       OpenBolt();
  113.       digitalWrite(LedGreen,LOW);
  114.       password.reset();
  115.     }
  116.   }
  117.   else{
  118.     digitalWrite(LedGreen,LOW);
  119.     password.reset();
  120.     errorSound();
  121.   }
  122. }
  123.  
  124. void startupSound()
  125. {
  126.   int note = (1/587.33)*500000;
  127.   for (int _=0;_<100000/note;_++){
  128.     delayMicroseconds(note);
  129.     digitalWrite(speakerPin, HIGH);
  130.     delayMicroseconds(note);
  131.     digitalWrite(speakerPin, LOW);
  132.   }
  133.  
  134.   note = (1/523.25)*500000;
  135.   for (int _=0;_<100000/note;_++){
  136.     delayMicroseconds(note);
  137.     digitalWrite(speakerPin, HIGH);
  138.     delayMicroseconds(note);
  139.     digitalWrite(speakerPin, LOW);
  140.   }
  141.  
  142.   note = (1/659.26)*500000;
  143.   for (int _=0;_<100000/note;_++){
  144.     delayMicroseconds(note);
  145.     digitalWrite(speakerPin, HIGH);
  146.     delayMicroseconds(note);
  147.     digitalWrite(speakerPin, LOW);
  148.   }
  149.  
  150. }
  151.  
  152. void errorSound()
  153. {
  154.   int n = 2000;
  155.   for (int vez=0;vez<2;vez++){
  156.     for (int _=0;_<30;_++){
  157.       delayMicroseconds(n);
  158.       digitalWrite(speakerPin, HIGH);
  159.       delayMicroseconds(n);
  160.       digitalWrite(speakerPin, LOW);
  161.     }
  162.  
  163.     delay(50);
  164.   }
  165. }
  166.  
  167. void openSound()
  168. {
  169.   int n = 300;
  170.   for (int vez=0;vez<3;vez++){
  171.     for (int _=0;_<100;_++){
  172.       delayMicroseconds(n);
  173.       digitalWrite(speakerPin, HIGH);
  174.       delayMicroseconds(n);
  175.       digitalWrite(speakerPin, LOW);
  176.     }
  177.     delay(100);
  178.   }
  179. }
  180.  
  181. void OpenBolt()
  182. {
  183.   if (Closed == 1){
  184.     digitalWrite(MotorPin1, LOW);
  185.     digitalWrite(MotorPin2, HIGH);
  186.     digitalWrite(MotorPinGo, HIGH);    
  187.  
  188.     delay(30);
  189.  
  190.     digitalWrite(MotorPin1, LOW);
  191.     digitalWrite(MotorPin2, LOW);
  192.     digitalWrite(MotorPinGo, LOW);        
  193.     Closed=0;    
  194.     openSound();
  195.   }
  196. }
  197. void CloseBolt()
  198.  
  199. {
  200.   if (Closed == 0){
  201.     closeSound1();
  202.     do
  203.     {
  204.     delay(1);
  205.     } while (digitalRead(Gerkon)==1);
  206.    
  207.     delay(3000); //wait 3 seconds
  208.    
  209.     digitalWrite(MotorPin1, HIGH);
  210.     digitalWrite(MotorPin2, LOW);
  211.     digitalWrite(MotorPinGo, HIGH);    
  212.  
  213.     delay(30);
  214.  
  215.     digitalWrite(MotorPin1, LOW);
  216.     digitalWrite(MotorPin2, LOW);
  217.     digitalWrite(MotorPinGo, LOW);        
  218.     Closed=1;
  219.     closeSound2();
  220.   }
  221. }
  222.  
  223.  
  224.  
  225. void closeSound1()
  226. {
  227.   int n = 200;
  228.   for (int vez=0;vez<2;vez++){
  229.     for (int _=0;_<100;_++){
  230.       delayMicroseconds(n);
  231.       digitalWrite(speakerPin, HIGH);
  232.       delayMicroseconds(n);
  233.       digitalWrite(speakerPin, LOW);
  234.     }
  235.     delay(100);
  236.   }
  237. }  
  238. void closeSound2()
  239. {  
  240.   int n = 450;
  241.   for (int _=0;_<110;_++){
  242.     delayMicroseconds(n);
  243.     digitalWrite(speakerPin, HIGH);
  244.     delayMicroseconds(n);
  245.     digitalWrite(speakerPin, LOW);
  246.   }
  247. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement