Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This is a sketch to answer the challenge proposed to the students from the Arduino Class from https://cursodearduino.net
- * You can watch the proposed challenge at https://youtu.be/OqxAMhe8Pq0
- * Features added: Password change timeout (suggested);
- * Access lock after 10 seconds of granted access (new).
- * The author is Gerson Ferreira
- * Credits to Flavio Guimaraes from http://youtube.com/BrincandoComIdeias &
- * http://cursodearduino.net &
- * https://www.facebook.com/paginaBrincandoComIdeias/ &
- * https://www.instagram.com/canalbrincandocomideias/
- * No messages are sent to serial port
- * 19-07-2019 - v1 Created by Gerson Ferreira;
- * 20-07-2019 - v2 Button entry is blocked when the gate is open
- * 21-07-2019 - v3 Access denied (gate closed) after 10 seconds of granted access
- */
- #include <OneButton.h> // https://github.com/mathertel/OneButton
- #include <TimerOne.h> // https://github.com/PaulStoffregen/TimerOne
- #include <EEPROM.h> // EEPROM library
- #define POT A3 // potentiometer pin
- #define LED0 2 // led pins
- #define LED1 3
- #define LED2 4
- #define LED3 5
- #define LED4 6
- #define LED5 7
- #define LED6 8
- #define LED7 9
- #define BUTTON 10 // button pin
- #define COUNTER_PWD 9 // you have (COUNTER_PWD + 1) seconds to type a choosed new password
- #define COUNTER_GATE 9 // the gate will be open for (COUNTER_GATE + 1) seconds
- #define BUZZER 12 // buzzer pin
- #define GATE 11 // gate pin
- byte ledMapped; // current led
- byte ledMappedPrevious = 0; // previous
- byte ledMappedPosterior = 0; // next
- byte nOC = 0; // number Of Clicks
- byte numSet;
- byte chosenPwd[4]; // 4 digit chosen password
- byte savedPwd[4] = {1, 2, 3, 4}; // 4 digit default password
- bool longPress = false; // true after choosing password update (long press on button)
- bool openedGate = false; // true for gate opened, false for gate closed
- byte counterPwd = 0; // timer password counter
- byte counterGate = 0; // timer gate counter
- OneButton button(BUTTON, true); // create button object to control a button
- // declared functions ( The explanations are inside the functions)
- void click();
- void longPressStop();
- void flashLed(byte count);
- bool verifyPwd();
- void updatePwd();
- void readPwd();
- void clockCounter();
- void alarmIt(byte timing, byte cycleOne, byte cycleTwo);
- void unlockGate();
- void setup() {
- pinMode(POT, INPUT); // set mode
- pinMode(LED0, OUTPUT);
- pinMode(LED1, OUTPUT);
- pinMode(LED2, OUTPUT);
- pinMode(LED3, OUTPUT);
- pinMode(LED4, OUTPUT);
- pinMode(LED5, OUTPUT);
- pinMode(LED6, OUTPUT);
- pinMode(LED7, OUTPUT);
- pinMode(BUZZER, OUTPUT);
- pinMode(GATE, OUTPUT);
- button.attachClick(click); // one click button call function
- button.attachLongPressStop(longPressStop); // long press button call function
- digitalWrite(GATE, HIGH); // initial position
- readPwd(); // read 4 digits password from EEPROM
- Timer1.initialize(1000000); // set timer at each 1 second
- }
- void loop() {
- button.tick();
- ledMapped = map(analogRead(POT), 0, 1023, 0, 10); // map it
- ledMapped = constrain(ledMapped, 1, 10); // limit it
- digitalWrite(ledMapped, HIGH); // on
- digitalWrite(ledMappedPrevious, LOW); // the others will be off
- digitalWrite(ledMappedPosterior,LOW);
- ledMappedPrevious = ledMapped - 1; // set position
- ledMappedPosterior = ledMapped + 1;
- if (counterPwd > COUNTER_PWD && longPress == true) { // do we still have time in setting a new password ?
- Timer1.stop(); // stop timer
- flashLed(1); // flash leds
- alarmIt(2, 10, 20); // alarm
- nOC = 0; // reset number Of Clicks
- counterPwd = 0; // reset timer password counter
- longPress = false; // cancel new password setting operation
- }
- if (counterGate > COUNTER_GATE && openedGate == true) {
- Timer1.stop(); // stop timer
- flashLed(1); // flash leds
- alarmIt(2, 5, 10); // alarm
- counterGate = 0; // reset timer gate counter
- digitalWrite(GATE, HIGH); // closing the gate
- openedGate = false; // gate is closed
- }
- }
- void click() {
- // to play with a simple click button
- if (openedGate == false) { // only if the gate is closed
- nOC++; // click count
- chosenPwd[nOC - 1] = ledMapped - 1; // save one digit chosed
- if (nOC == 4) { // last digit ?
- nOC = 0; // then reset counter
- if (longPress) { // shall we update the password ?
- longPress = false; // unset password update
- nOC = 0; // reset counter *
- Timer1.stop(); // stop timer *
- counterPwd = 0; // reset timer password counter *
- flashLed(3); // flash leds
- updatePwd(); // updated password
- readPwd(); // update the cache;
- } else {
- if (verifyPwd()) {
- flashLed(1); // flash leds
- unlockGate(); // good password. Open the gate !
- } else {
- flashLed(2); // flash leds
- alarmIt(1, 80, 100); // bad password. AlarmIt !
- flashLed(2); // again
- }
- }
- }
- } else { // gate is already open. no password typing will be accepted
- flashLed(1); // flash
- alarmIt(1, 4, 8); // alarm
- }
- } // click
- void longPressStop() {
- // To play with a long press click button
- if (openedGate == false) { // only if the gate is closed
- flashLed(3); // blink all
- longPress = true; // set password update
- nOC = 0; // reset number Of Clicks
- Timer1.start(); // starts timer
- Timer1.attachInterrupt(clockCounter); // links it to a function
- } else { // gate is open. no password change will be accepted
- flashLed(1); // flash
- alarmIt(1, 4, 8); // alarm
- }
- } // longPressStop
- void flashLed(byte count) {
- // Just to flash the leds;
- // Flash until count is no longer greater than zero
- while(count > 0 ) {
- digitalWrite(LED0, HIGH); // on
- digitalWrite(LED1, HIGH);
- digitalWrite(LED2, HIGH);
- digitalWrite(LED3, HIGH);
- digitalWrite(LED4, HIGH);
- digitalWrite(LED5, HIGH);
- digitalWrite(LED6, HIGH);
- digitalWrite(LED7, HIGH);
- delay(100);
- digitalWrite(LED0, LOW); // off
- digitalWrite(LED1, LOW);
- digitalWrite(LED2, LOW);
- digitalWrite(LED3, LOW);
- digitalWrite(LED4, LOW);
- digitalWrite(LED5, LOW);
- digitalWrite(LED6, LOW);
- digitalWrite(LED7, LOW);
- delay(100);
- count--; // decrement count
- }
- } // flashLed
- bool verifyPwd() {
- // Check password typed against last saved password
- if (savedPwd[0] == chosenPwd[0] &&
- savedPwd[1] == chosenPwd[1] &&
- savedPwd[2] == chosenPwd[2] &&
- savedPwd[3] == chosenPwd[3] ) {
- return true;
- } else {
- return false;
- }
- } // verifyPwd
- void updatePwd() {
- // Update chose password to EEPROM
- EEPROM.update(0, chosenPwd[0]); // update EEPROM
- EEPROM.update(1, chosenPwd[1]);
- EEPROM.update(2, chosenPwd[2]);
- EEPROM.update(3, chosenPwd[3]);
- } // updatePwd
- void readPwd() {
- // Read password from EEPROM
- savedPwd[0] = EEPROM.read(0); // read EEPROM
- savedPwd[1] = EEPROM.read(1);
- savedPwd[2] = EEPROM.read(2);
- savedPwd[3] = EEPROM.read(3);
- } // readPwd
- void clockCounter() {
- // Call back function for time count
- if (longPress) { // on password update
- counterPwd++; // count time
- }
- if (openedGate) { // on gate opened
- counterGate++; // count time
- }
- } // clockCounter
- void alarmIt(byte timing, byte cycleOne, byte cycleTwo) {
- // Buzzer alarm
- byte i;
- byte countBuzzer = timing; // init counter
- while (countBuzzer > 0){
- for (i = 0; i < cycleOne; i++){ // cycles of sound
- digitalWrite (BUZZER, HIGH); // This will turn the buzzer ON
- delay (1) ; // Giving a Delay of 1ms will set frequency 1
- digitalWrite (BUZZER, LOW); // This will turn the buzzer OFF
- delay (1) ; // Giving a delay ms
- }
- for (i = 0; i < cycleTwo; i++){ // cycles of sound
- digitalWrite (BUZZER, HIGH); // This will turn the buzzer ON
- delay (2) ; // Giving a delay of 2ms will set frequency 2
- digitalWrite (BUZZER, LOW); // This will turn the buzzer OFF
- delay (2) ; // Giving a delay of 2ms
- }
- countBuzzer--; // decrease
- }
- } // alarmIt
- void unlockGate() {
- // Open the gate after a successful password
- digitalWrite(GATE, LOW); // unlock the gate
- alarmIt(2, 5, 10); // alarm
- openedGate = true; // the gate is open
- counterGate = 0; // init time counter
- Timer1.start(); // starts timer
- Timer1.attachInterrupt(clockCounter); // links it to a function
- } // unlockGate
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement