In this Instructables i will use a DigiSpark with a CD4021 Shift Register and read 6 mechanical switches.
Depending on the switch pressed, the DigiSpark will send some key strokes back to the PC. Making a sort of a Macro keypad, where we can assign each tactile switches to send key stroke.
Key Features :
• Programmable keys
• Shortcut keys With one press of a button.
• Volume control keys (for windows)
The Digispark is an Attiny85 based microcontroller development board similar to the Arduino line, only cheaper, smaller, and a bit less powerful. IT use the familiar Arduino IDE the Digispark is a great way to jump into electronics, or perfect for when an Arduino is too big or too much.
Specification:
Watch this video to learn how to install Digispark drivers (That is created by Brainy Bits)
the Best Tutorial that i found.
https://brainy-bits.com/ check their site for more information
And Check this link to learn how to connect and program the Digispark using the Arduino IDE
https://digistump.com/wiki/digispark/tutorials/con...
Make sure to use Arduino IDE version 1.6.7
Because i got avr errors when uploading the code.
Pins Connections (digispark to shift-register)
• Digispark GND -------------- CD4021 GND/VSS
• Digispark 5V/VCC ---------- CD4021 VCC/VDD
• Digispark (P2 , D2) --- CD4021 LOAD / (pin 09 )
• Digispark (P1 , D1) --- CD4021 CLOCK / (pin 10)
• Digispark (P0 , D0) -- CD4021 SER OUT / Q8 / (pin 03)
Tactile Buttons (8 switches)
One leg of the Tactile Switches/ mechanical switch is connected to the 5V line.
The other leg of the Tactile Switches/mechanical switches are connected to pin 1-2-3-4-5-6-7 of the CD4021.
Pins 1-2-3-4-5-6-7 also have some 10K resistors connected to ground for switch pullups.
Look At the circuit layout for further clarifications.
Check step 4 to learn how to install the libraries.
Before verifying the code , change the board to Digispark default .this will the ensure that the Digikeyboard.h is present in Arduino IDE . otherwise the digispark libraries are not present in the IDE even though its installed properly.
Each button has its unique binary value depending on the CD4021 pin , therefore to find out which is which upload this test code to the digispark
open the notepad and press the buttons and check the output.
To have the DigiSpark act as a Keyboard, we will be using a library called “DigiKeyboard” created by DigiStump.
Check the PDF file to find out the Arduino key word for each keyboard key.
To send a key stroke this command is used.
DigiKeyboard.sendKeyStroke();
To print something like "hello" this command is used.
DigiKeyboard.print ("Hello");
To put a delay this command is used:
DigiKeyboard.delay();
To press two keys at once :
separate the two key commands with a comma
eg:
DigiKeyboard.sendKeyStroke(KEY_C , MOD_CONTROL_LEFT);//copy shortcut (ctrl +v).
DigiKeyboard.sendKeyStroke(KEY_L , MOD_GUI_LEFT);//windows lock shortcut (windows logo key + L)
To press three keys at once:
separate two keys with a comma and the third command with a
DigiKeyboard.sendKeyStroke(KEY_ENTER , MOD_CONTROL_LEFT | MOD_SHIFT LEFT);
(Ctrl + Shift + Enter)
To only press the windows key:
DigiKeyboard.sendKeyStroke(0, MOD_GUI_LEFT );
To execute a command with a single press:
To open "CMD" in administrator mode , assign a key to this code and change the binary value .
if (RegisterValue == B10)
DigiKeyboard.sendKeyStroke(0,MOD_GUI_LEFT ); // press the windows key
DigiKeyboard.delay(1000); // wait a second
DigiKeyboard.print("cmd");
DigiKeyboard.delay(1000); // wait a second
DigiKeyboard.sendKeyStroke(KEY_ENTER,MOD_CONTROL_LEFT | MOD_SHIFT_LEFT ); // Ctrl+shift+enter
DigiKeyboard.delay(1000); // wait a second
DigiKeyboard.sendKeyStroke(KEY_ARROW_LEFT );
DigiKeyboard.sendKeyStroke(KEY_ENTER );
UsefulWindows 10 shortcut keys
After uploading is done , remove the USB cable and plug it back again.
If you receive and error saying the USB is not recognized try unplugging and plugging back it again and while its initializing press any key repeatedly until it works.
Download and install this (Adafruit-Trinket-USB) library to your Arduino IDE.
upload the code and change according to your needs.
when using this code no need to press a key when plugin in the cable.
Example code: This replicate the exit action with a button.
// EXIT APPLICATION(Alt + F4)
TrinketHidCombo.pressKey(KEYCODE_MOD_LEFT_ALT, KEYCODE_F4);
TrinketHidCombo.pressKey(0, 0); // releasing keyboard.
Download and install this library (Adafruit-Trinket-USB) in your Arduino IDE.
upload this code and change the register value to match the button.
TrinketHidCombo.pressMultimediaKey(MMKEY_MUTE); // mute
TrinketHidCombo.pressMultimediaKey(MMKEY_VOL_UP); // volume up
TrinketHidCombo.pressMultimediaKey(MMKEY_VOL_DOWN); // volume down