Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Pins
- const int lockPin = 3;
- const int readerPin = 2;
- // reader state storage vars
- int currentReaderState;
- int lastReaderState;
- int lockState;
- /**
- * Init
- */
- void setup()
- {
- // Setup ping
- pinMode(lockPin, OUTPUT);
- pinMode(readerPin, INPUT);
- // Close the lock on powerup (LOW = LOCKED)
- digitalWrite(lockPin, LOW);
- // Setup current state of reader (HIGH = DENY)
- currentReaderState = HIGH;
- lastReaderState = HIGH;
- }
- /**
- * Main loop
- */
- void loop()
- {
- currentReaderState = digitalRead(readerPin);
- // Readers state is changed
- if (currentReaderState != lastReaderState) {
- // If new state is ALLOW (LOW) then toggle the lock
- if (currentReaderState == LOW) {
- lockState = (lockState == LOW) ? HIGH : LOW;
- digitalWrite(lockPin, lockState);
- }
- // Debounce
- delay(50);
- }
- // Save last state
- lastReaderState = currentReaderState;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement