Emergency Lock Button

My last project uses a Digispark (ATtiny85) as an USB keyboard to send the lock screen shortcut (Ctrl-Alt-L) when a single button is pressed. To bring some fun, I bundled the whole in an emergency stop push button.

Project objectives

  • Lock screen when button is pressed
  • Allow cancel when pressed by an evil co-worker
  • Do something of a Digispark

Material

Electronics of the project were bought on eBay from chinese resellers.

  • Emergency push button (~ 4-7 $)
  • Digispark (~ 3-4 $)

I then 3D printed a cap for the micro USB connector (white part in the picture). The cap also holds the Digispark board. On featured image, the connector is at an angle due to the pins I soldered on the board. The female connectors I put on them didn’t allowed me to turn the board inside the housing of the button.

Source code

Code provided below can be compiled using Arduino IDE provided by DigiStump. The firmware contains a neat trick allowing to cancel a press on the button. In fact you’ll have a 2 second countdown before shortcut is sent. If emergency button is released within this period, action is canceled.

#include "DigiKeyboard.h"
#define KEY_DELETE 76

bool state = false;
int countdown = -1;

void setup() {
  pinMode(2, INPUT);
  digitalWrite(2, HIGH); // Pull-up
}

void loop() {
  // Kept from sample
  // this is generally not necessary but with some older systems it seems to
  // prevent missing the first character after a delay:
  DigiKeyboard.sendKeyStroke(0);

  int sensorValue = digitalRead(2);
  if (sensorValue == 1) {
    // Push button is pressed
    if (countdown == 0) {
      // It's the final count down ! Send shortcut Ctrl-Alt-L
      if (state == false) {
        DigiKeyboard.sendKeyStroke(KEY_L, MOD_ALT_LEFT | MOD_CONTROL_LEFT);
        state = true;
      }
    } else if (countdown == -1) {
      // We were waiting, start countdown
      countdown = 20; // countdown * 100 = <time in milliseconds>
      state = false;
    } else {
      // Still pressed, continue the count down
      countdown--;
    }
  } else {
    // Push button is released (Short circuit -> GND)
    // Reset
    countdown = -1;
  }

  // Kept from sample
  // It's better to use DigiKeyboard.delay() over the regular Arduino delay()
  // if doing keyboard stuff because it keeps talking to the computer to make
  // sure the computer knows the keyboard is alive and connected
  DigiKeyboard.delay(100);
}

 Conclusion

This project was really fun to do. Most of all, it’s really fun to use on an every day basis.

Leave a Reply

Your email address will not be published. Required fields are marked *