Rohan Barnwal
Published © GPL3+

TabEscaper 3000: The Parent-Proximity Auto-Tab Switcher

TabEscaper 3000: A fun Arduino gadget that auto-switches tabs when someone's near. Learn, laugh and hide your screen

BeginnerFull instructions provided1
TabEscaper 3000: The Parent-Proximity Auto-Tab Switcher

Things used in this project

Hardware components

Arduino Leonardo
Arduino Leonardo
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Connection

Code

Code

Arduino
// Arduino Leonardo: HC-SR04 on pins 6 (TRIG) and 7 (ECHO).
// Send Ctrl+Tab once when distance < 10 cm (will not repeat continuously).

#include <Keyboard.h>

const uint8_t TRIG_PIN = 6;
const uint8_t ECHO_PIN = 7;

const float THRESHOLD_CM = 30.0;     // trigger threshold
const unsigned long PULSE_TIMEOUT = 30000UL; // microseconds (30 ms) timeout

bool sentOnce = false; // ensures single send while object stays < threshold

void setup() {
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  digitalWrite(TRIG_PIN, LOW);
  Serial.begin(9600);
  // Allow the USB stack to settle before sending HID events
  delay(1000);
  Keyboard.begin();
}

void loop() {
  float distance = readDistanceCM();

  Serial.print("Distance (cm): ");
  if (distance >= 0) {
    Serial.println(distance, 2);
  } else {
    Serial.println("timeout");
  }

  if (distance >= 0 && distance < THRESHOLD_CM) {
    if (!sentOnce) {
      sendCtrlTab();
      sentOnce = true;
      Serial.println("Sent Ctrl+Tab");
    }
  } else {
    // reset flag when object moves away (allows next Ctrl+Tab when it comes close again)
    sentOnce = false;
  }

  delay(100); // modest sampling interval (10 Hz)
}

float readDistanceCM() {
  // Trigger the sensor: a 10µs HIGH on TRIG
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // Read echo pulse (microseconds) with timeout
  unsigned long duration = pulseIn(ECHO_PIN, HIGH, PULSE_TIMEOUT);

  if (duration == 0) {
    // timeout (no echo) -> return -1 to indicate invalid
    return -1.0;
  }

  // Distance in cm: sound speed ~ 343 m/s -> 29.1 µs per cm round-trip
  // distance = (duration / 2) / 29.1
  float distanceCm = (duration / 2.0) / 29.1;
  return distanceCm;
}

void sendCtrlTab() {
  // Press modifier (left ctrl) + tab, hold briefly, then release
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(KEY_TAB);
  delay(100); // hold for 100 ms
  Keyboard.releaseAll();
}

Credits

Rohan Barnwal
34 projects • 35 followers
Rohan Barnwal - maker, hacker, tech enthusiast. I explore new tech & find innovative solutions. See my projects on hackster.io!

Comments