Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
ywpg
Published © Apache-2.0

Make a Big Button for Your Computer!

One big button for your computer, for special occasions: publishing articles, releasing apps, or sending out big emails. 😁

BeginnerFull instructions provided4 hours2,240
Make a Big Button for Your Computer!

Things used in this project

Hardware components

Pro Micro - 5V/16MHz
SparkFun Pro Micro - 5V/16MHz
Or you can use any compatible board with ATmega32U4 as the main chip. e.g.: Arduino Micro, Arduino Leonardo, Beetle, etc.
×1
Slide Switch
Slide Switch
optional
×1
Micro-USB to USB Cable (Generic)
Micro-USB to USB Cable (Generic)
×1
push button box
I brought an 1-way push button box from the Internet. You can either buy one or make one using any push button + box.
×1
wires
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
PCB Holder, Soldering Iron
PCB Holder, Soldering Iron
optional

Story

Read more

Schematics

Connection

Code

As mouse button

Arduino
Use the big button as mouse left button
#include <Mouse.h>

int buttonPin = 9;
bool isDown = false;  // variable to save previous state of button

void setup() {
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH); // "pull up" to set the pin HIGH
}

void loop() {
  // read the status of the pin
  int stat = digitalRead(buttonPin);
  
  // if the button is pressed before and now released,
  // send a "release" event for mouse left button
  if (isDown && stat == HIGH){
    isDown = false;
    Mouse.release(MOUSE_LEFT);
    delay(100);
  }
  
  // if the button is not pressed before and now pressed,
  // send a "press" event for mouse left button
  else if(!isDown && stat == LOW)
  {
    Mouse.press(MOUSE_LEFT);
    isDown = true;
    delay(100);    
  }

  // do nothing if user holding the button down
}

As Enter key

Arduino
Use the big button as Enter key from a keyboard
#include <Keyboard.h>

int buttonPin = 9;

void setup() {
  pinMode(buttonPin, INPUT);
  digitalWrite(buttonPin, HIGH);  // "pull up" to set the pin HIGH
  Keyboard.begin();
}

void loop() {
  // Get status of the pin. 
  // If the button is pressed, the pin will connect to GND (ground),
  // the status will change from HIGH to LOW.
  if (digitalRead(buttonPin) == LOW)  
  {
    // 176 is the key code for Enter
    Keyboard.write(176);   
    
    // sleep for 1 second, so that there won't be too many key events
    delay(1000); 
  }
}

final version

Arduino
Final version, combining two previous code pieces to support both mouse and keyboard event at the same time (through different pin). We can use a hardware switch to decide which pin we will trigger.
#include <Keyboard.h>
#include <Mouse.h>

// different pins for different type of input
int mousePin = 9;
int keyboardPin = 10;
bool isDown = false;

void setup() {
  pinMode(mousePin, INPUT);
  digitalWrite(mousePin, HIGH);  // "pull up" to set the pin HIGH
  pinMode(keyboardPin, INPUT);
  digitalWrite(keyboardPin, HIGH);  // "pull up" to set the pin HIGH
}

void loop() {
  // read the status of pins all at once and save
  //   -- so we don't need to read it multiple times when checking status.
  int statMouse = digitalRead(mousePin);
  int statKey = digitalRead(keyboardPin);

  // althrough our circuit don't support triggering the mouse pin and keyboard pin at the same time, just in case of problem, 
  // we define that mouse pin has higher priority over the keyboard pin.
  // If the "mouse pin" has activity and has been handled, we ignore the status of "keyboard pin".
  bool handled = false;
  
  // mouse has priority.
  
  // if the button is pressed before and now released,
  // send a "release" event for mouse left button
  if (isDown && statMouse == HIGH){
    isDown = false;
    Mouse.release(MOUSE_LEFT);
    delay(100);
    handled = true;
  }
  
  // if the button is not pressed before and now pressed,
  // send a "press" event for mouse left button
  else if(!isDown && statMouse == LOW)
  {
    Mouse.press(MOUSE_LEFT);
    isDown = true;
    delay(100);    
    handled = true;
  }

  // skip keyboard if mouse event is handled
  // but if it's not handled yet, we check status of keyboard pin
  if (!handled){
    if (statKey == LOW){
      // the button is pressed.
      Keyboard.write(176);  // send the key code of Enter key
      // sleep for 1 second so there won't be too many repeated key events
      delay(1000);  
    }
  }
}

Credits

ywpg

ywpg

2 projects β€’ 2 followers
A software engineer curious in electronics: sensors, Arduino, esp8266, IoT, etc.

Comments