Advertisement
gersonfer

desafio2019

Jul 23rd, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.70 KB | None | 0 0
  1. /*
  2.  * This is a sketch to answer the challenge proposed to the students from the Arduino Class from https://cursodearduino.net
  3.  * You can watch the proposed challenge at https://youtu.be/OqxAMhe8Pq0
  4.  * Features added:  Password change timeout (suggested);
  5.  *                  Access lock after 10 seconds of granted access (new).
  6.  * The author is Gerson Ferreira
  7.  * Credits to Flavio Guimaraes from http://youtube.com/BrincandoComIdeias &
  8.  *                                  http://cursodearduino.net &    
  9.  *                                  https://www.facebook.com/paginaBrincandoComIdeias/ &
  10.  *                                  https://www.instagram.com/canalbrincandocomideias/
  11.  * No messages are sent to serial port
  12.  * 19-07-2019 - v1 Created by Gerson Ferreira;
  13.  * 20-07-2019 - v2 Button entry is blocked when the gate is open
  14.  * 21-07-2019 - v3 Access denied (gate closed) after 10 seconds of granted access
  15. */
  16.  
  17. #include <OneButton.h>                      //  https://github.com/mathertel/OneButton
  18. #include <TimerOne.h>                       //  https://github.com/PaulStoffregen/TimerOne
  19. #include <EEPROM.h>                         //  EEPROM library
  20.  
  21. #define POT     A3                           //  potentiometer pin
  22. #define LED0    2                            //  led pins
  23. #define LED1    3
  24. #define LED2    4
  25. #define LED3    5
  26. #define LED4    6
  27. #define LED5    7
  28. #define LED6    8
  29. #define LED7    9
  30. #define BUTTON  10                             //  button pin
  31. #define COUNTER_PWD   9                        //  you have (COUNTER_PWD + 1) seconds to type a choosed new password
  32. #define COUNTER_GATE  9                        //  the gate will be open for (COUNTER_GATE + 1) seconds
  33. #define BUZZER  12                             //  buzzer pin
  34. #define GATE    11                             //  gate pin
  35.  
  36. byte  ledMapped;                               //  current led
  37. byte  ledMappedPrevious  = 0;                  //  previous
  38. byte  ledMappedPosterior = 0;                  //  next
  39. byte  nOC                = 0;                  //  number Of Clicks
  40. byte  numSet;
  41. byte  chosenPwd[4];                            //  4 digit chosen password
  42. byte  savedPwd[4]        = {1, 2,  3,  4};      // 4 digit default password
  43. bool  longPress = false;                        // true after choosing password update (long press on button)
  44. bool  openedGate = false;                       // true for gate opened, false for gate closed
  45. byte  counterPwd  = 0;                          // timer password counter
  46. byte  counterGate = 0;                          // timer gate counter
  47.  
  48. OneButton     button(BUTTON,  true);            // create button object to control a button
  49.  
  50. // declared functions ( The explanations are inside the functions)
  51. void click();
  52. void longPressStop();
  53. void flashLed(byte count);
  54. bool  verifyPwd();
  55. void updatePwd();
  56. void readPwd();
  57. void  clockCounter();
  58. void  alarmIt(byte timing, byte cycleOne, byte cycleTwo);
  59. void  unlockGate();
  60.  
  61. void setup() {
  62.   pinMode(POT,    INPUT);                       // set mode  
  63.   pinMode(LED0,   OUTPUT);
  64.   pinMode(LED1,   OUTPUT);
  65.   pinMode(LED2,   OUTPUT);
  66.   pinMode(LED3,   OUTPUT);
  67.   pinMode(LED4,   OUTPUT);
  68.   pinMode(LED5,   OUTPUT);
  69.   pinMode(LED6,   OUTPUT);
  70.   pinMode(LED7,   OUTPUT);
  71.   pinMode(BUZZER, OUTPUT);
  72.   pinMode(GATE,   OUTPUT);
  73.  
  74.   button.attachClick(click);                             // one  click button call function
  75.   button.attachLongPressStop(longPressStop);             // long press button call function
  76.   digitalWrite(GATE,  HIGH);                             // initial position
  77.   readPwd();                                             // read 4 digits password from EEPROM
  78.   Timer1.initialize(1000000);                            // set timer at each 1 second  
  79. }
  80.  
  81. void loop() {
  82.   button.tick();
  83.   ledMapped = map(analogRead(POT), 0,  1023, 0,  10);     // map it
  84.   ledMapped = constrain(ledMapped, 1, 10);                // limit it
  85.   digitalWrite(ledMapped,         HIGH);                  // on
  86.   digitalWrite(ledMappedPrevious, LOW);                   // the others will be off
  87.   digitalWrite(ledMappedPosterior,LOW);
  88.   ledMappedPrevious   = ledMapped - 1;                    // set position
  89.   ledMappedPosterior  = ledMapped + 1;
  90.   if (counterPwd > COUNTER_PWD && longPress == true) {    // do we still have time in setting a new password ?
  91.     Timer1.stop();                                        // stop timer
  92.     flashLed(1);                                          // flash leds
  93.     alarmIt(2,  10, 20);                                  // alarm
  94.     nOC = 0;                                              // reset number Of Clicks
  95.     counterPwd = 0;                                       // reset timer password counter
  96.     longPress  = false;                                   // cancel new password setting operation
  97.   }
  98.   if (counterGate > COUNTER_GATE && openedGate == true) {
  99.     Timer1.stop();                                        // stop timer
  100.     flashLed(1);                                          // flash leds
  101.     alarmIt(2,  5,  10);                                  // alarm
  102.     counterGate = 0;                                      // reset timer gate counter
  103.     digitalWrite(GATE,  HIGH);                            //  closing the gate
  104.     openedGate = false;                                   // gate is closed
  105.   }
  106. }
  107.  
  108. void click() {
  109. // to play with a simple click button
  110.   if (openedGate == false) {                              // only if the gate is closed
  111.     nOC++;                                                // click count
  112.     chosenPwd[nOC - 1] = ledMapped - 1;                   // save one digit chosed
  113.     if (nOC == 4) {                                       // last digit ?
  114.       nOC = 0;                                            // then reset counter
  115.       if  (longPress) {                                   // shall we update the password ?
  116.         longPress = false;                                // unset password update
  117.         nOC = 0;                                          // reset counter *
  118.         Timer1.stop();                                    // stop timer    *
  119.         counterPwd  = 0;                                  // reset timer password counter *
  120.         flashLed(3);                                      // flash leds
  121.         updatePwd();                                      // updated password
  122.         readPwd();                                        // update the cache;            
  123.       } else {
  124.         if (verifyPwd()) {
  125.           flashLed(1);                                    // flash leds
  126.           unlockGate();                                   // good password. Open the gate !
  127.         } else {
  128.           flashLed(2);                                    // flash leds
  129.           alarmIt(1,  80, 100);                           // bad password. AlarmIt !
  130.           flashLed(2);                                    // again
  131.         }
  132.       }
  133.     }
  134.   } else {                                                // gate is already open. no password typing will be accepted
  135.     flashLed(1);                                          // flash
  136.     alarmIt(1,  4,  8);                                   // alarm
  137.   }
  138. } // click
  139.  
  140. void longPressStop() {
  141. // To play with a long press click button
  142.   if (openedGate == false) {                              //  only if the gate is closed
  143.     flashLed(3);                                          //  blink all
  144.     longPress = true;                                     //  set password update
  145.     nOC = 0;                                              //  reset number Of Clicks
  146.     Timer1.start();                                       //  starts timer
  147.     Timer1.attachInterrupt(clockCounter);                 //  links it to a function
  148.   } else {                                                //  gate is open. no password change will be accepted            
  149.     flashLed(1);                                          //  flash
  150.     alarmIt(1,  4,  8);                                   //  alarm
  151.   }
  152. } // longPressStop
  153.  
  154. void flashLed(byte count) {
  155. // Just to flash the leds;
  156. // Flash until count is no longer greater than zero
  157.   while(count > 0 ) {  
  158.     digitalWrite(LED0, HIGH);                              // on
  159.     digitalWrite(LED1, HIGH);
  160.     digitalWrite(LED2, HIGH);
  161.     digitalWrite(LED3, HIGH);
  162.     digitalWrite(LED4, HIGH);
  163.     digitalWrite(LED5, HIGH);
  164.     digitalWrite(LED6, HIGH);
  165.     digitalWrite(LED7, HIGH);
  166.     delay(100);    
  167.     digitalWrite(LED0, LOW);                               // off
  168.     digitalWrite(LED1, LOW);
  169.     digitalWrite(LED2, LOW);
  170.     digitalWrite(LED3, LOW);
  171.     digitalWrite(LED4, LOW);
  172.     digitalWrite(LED5, LOW);
  173.     digitalWrite(LED6, LOW);
  174.     digitalWrite(LED7, LOW);
  175.     delay(100);          
  176.     count--;                                               // decrement count
  177.   }
  178. } // flashLed
  179.  
  180. bool  verifyPwd() {
  181. // Check password typed against last saved password
  182.   if (savedPwd[0] ==  chosenPwd[0] &&
  183.       savedPwd[1] ==  chosenPwd[1] &&
  184.       savedPwd[2] ==  chosenPwd[2] &&
  185.       savedPwd[3] ==  chosenPwd[3]   ) {
  186.     return true;                          
  187.    } else {
  188.     return false;
  189.    }      
  190. } // verifyPwd
  191.  
  192. void updatePwd()  {
  193. // Update  chose password to EEPROM
  194.   EEPROM.update(0,  chosenPwd[0]);                       // update EEPROM
  195.   EEPROM.update(1,  chosenPwd[1]);
  196.   EEPROM.update(2,  chosenPwd[2]);
  197.   EEPROM.update(3,  chosenPwd[3]);      
  198. } // updatePwd
  199.  
  200. void readPwd()  {
  201. // Read password from EEPROM
  202.   savedPwd[0] = EEPROM.read(0);                         // read EEPROM
  203.   savedPwd[1] = EEPROM.read(1);
  204.   savedPwd[2] = EEPROM.read(2);
  205.   savedPwd[3] = EEPROM.read(3);      
  206. } // readPwd
  207.  
  208. void  clockCounter() {
  209. // Call back function for time count
  210.   if (longPress) {                                     // on password update
  211.     counterPwd++;                                      // count time  
  212.   }
  213.   if (openedGate) {                                   // on gate opened
  214.     counterGate++;                                    // count time    
  215.   }
  216. } // clockCounter
  217.  
  218. void  alarmIt(byte timing, byte cycleOne, byte cycleTwo) {
  219. // Buzzer alarm
  220.   byte i;                                          
  221.   byte  countBuzzer = timing;                         // init counter
  222.   while (countBuzzer > 0){
  223.     for (i = 0; i < cycleOne; i++){                  // cycles of sound
  224.        digitalWrite (BUZZER, HIGH);                  // This will turn the buzzer ON
  225.        delay (1) ;                                   // Giving a Delay of 1ms will set frequency 1
  226.        digitalWrite (BUZZER, LOW);                   // This will turn the buzzer OFF
  227.        delay (1) ;                                   // Giving a delay ms
  228.      }
  229.      for (i = 0; i < cycleTwo; i++){                 // cycles of sound
  230.        digitalWrite (BUZZER, HIGH);                  // This will turn the buzzer ON
  231.        delay (2) ;                                   // Giving a delay of 2ms will set frequency 2
  232.        digitalWrite (BUZZER, LOW);                   // This will turn the buzzer OFF
  233.        delay (2) ;                                   // Giving a delay of 2ms
  234.      }
  235.      countBuzzer--;                                  // decrease
  236.    }  
  237. } //  alarmIt
  238.  
  239. void  unlockGate()  {
  240. // Open the gate after a successful password
  241.   digitalWrite(GATE,  LOW);                         // unlock the gate
  242.   alarmIt(2,  5,  10);                              // alarm
  243.   openedGate = true;                                // the gate is open
  244.   counterGate = 0;                                  // init time counter
  245.   Timer1.start();                                   // starts timer
  246.   Timer1.attachInterrupt(clockCounter);             // links it to a function  
  247. } //  unlockGate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement