Hardware Reference
In-Depth Information
Listing 6-10: Light-Based Computer Lockā€”lock_computer.ino
//Locks your computer when you turn off the lights
const int LIGHT =1; //Light sensor on analog pin 1
const int THRESHOLD =500; //Brightness must drop below this level
//to lock computer
void setup()
{
Keyboard.begin();
}
void loop()
{
int brightness = analogRead(LIGHT); //Read the light level
if (brightness < THRESHOLD)
{
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('l');
delay(100);
Keyboard.releaseAll();
}
}
After loading the program, try flipping the lights off. Your computer should
lock immediately. The following video demo shows this in action. This sketch
implements two new keyboard functions: Keyboard.press() and Keyboard
.releaseAll() . Running Keyboard.press() is equivalent to starting to hold a
key down. So, if you want to hold the Windows key and the L key down at the
same time, you run Keyboard.press() on each. Then, you delay for a short period
of time and run the Keyboard.releaseAll() function to let go of, or release,
the keys. Special keys are defined on the Arduino website: http://a rduino.cc/
en/Reference/KeyboardModifiers . (This definition table is also linked from
the content page for this chapter at www.exploringarduino.com/content/ch6 .)
NOTE Towatchademovideoofthelight-activatedcomputerlock,visit
www.exploringarduino.com/content/ch6 .Youcanalsofindthisvideo
ontheWileywebsiteshownatthebeginningofthischapter.
EmulatingaMouse
Using a two-axis joystick and some pushbuttons, you can use an Arduino
Leonardo to make your own mouse! The joystick will control the mouse location,
and the buttons will control the left, middle, and right buttons of the mouse.
Search WWH ::




Custom Search