Linux Format - UK (2020-03)

(Antfer) #1
http://www.techradar.com/pro/linux March 2020 LXF260 43

Raspberry Pi projects


Get started with Ardiuino


delay(100);
}
delay(1000);
}
Click on the tick icon to check your code, and then
click on the arrow to flash the code to the Arduino. In a
few seconds the board will reboot and the LED will
quickly flash three times, then pause, and repeat the
process. You have successfully tested your Arduino and
are now ready to move on to the next project.

Project 2 Job saver
In this project we’ll build a simple switch that when
pressed will trigger a new tab to open in Firefox and
Chrome and open a page that looks like MS Outlook but
is really Reddit in disguise. This is handy for work, school
and home, but do so at your own risk!

Hardware setup
Using a breadboard we need to connect a pushbutton
(momentary switch) over the central channel so that
the legs are spread over the gap. Now, using two male to
male jumper wires, connect the top-left leg of the
button to GND on the Arduino, then connect the top-
right leg to pin 2 of the Arduino.

Coding the project
In the Arduino IoT Cloud, click on Arduino Web Editor
and connect the Arduino to your computer. Create a
new sketch in the Sketchbook, and the first step is to
import the Keyboard library that will enable our Arduino
to emulate a USB keyboard.
#include <Keyboard.h>
We can type this into the code, or we can go to Libraries
and then search for “Keyboard” and then click Include.
The next step is to configure the pin used for the
button, and to start the keyboard interface. For this we
will use a function called setup, which will run once,
when the board is powered up. Using pinMode, we can
tell the Arduino that we wish to use pin 2 as an input,
and that the internal pull-up resistor should pull the pin
high so that when the button is pressed it connects pin
2 (high) to GND. This will then pull pin 2 low and trigger
the event.
void setup() {
pinMode(2, INPUT_PULLUP);
Keyboard.begin();

Adding libraries
to our code is
simple via the
Arduino IoT Cloud
editor. Libraries
offer additional
features, such as
USB keyboard
emulation.

}
In order to continually run our code, we need to
create a loop that will contain the code. The first section
of code in the loop is a test to see if the button has been
pressed. Using digitalRead we can check pin 2, and if
the pin is HIGH, the button has not been pressed. This
means the code should take no action and keep waiting
for input. We should use a 50ms delay to prevent
accidental activation.
void loop() {
while (digitalRead(2) == HIGH) {
delay(50);
}
But what happens if the button is pressed? In this
case a delay is used to add 100ms delay before pressing
a series of keys to open a tab. To press a key we use
Keyboard.press and then pass the details of the key.
For keys such as Ctrl, Alt, Shift we need to provide the
details in a set manner, including whether it is the left
or right of each key. For regular character keys (letters,
numbers, punctuation) we pass the key as a string.
We then set a short 100ms delay to ensure that the
keypresses are registered.
delay(100);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(‘t’);
delay(100);
Right now the keys are pressed down, and if left too
long this could cause issues. So to release the keys we
use Keyboard.releaseAll before moving on. When a new
tab is opened in Firefox/Chrome, the focus of the cursor
will be in the address bar, and in the next line of code
we’ll paste in a URL for the Reddit/Outlook website.
Then we release the keys once again before a final
100ms delay ends the loop.
Keyboard.releaseAll();
Keyboard.println(“http://pcottle.github.io/
MSOutlookit/”);
Keyboard.releaseAll();
delay(100);
}
Click on the tick icon to check your code, and then
click on the arrow to flash the code to the Arduino. In a
few seconds the board will reboot and be ready for use.
So go ahead, press the button and make sure that it
works before the boss walks in!

A button is used to trigger a series of events that opens a new browser
tab and a website that looks like an email client.

3332March 0 h2rSenshSnigtom March 2020LXF260 43


Raspberry Pi projects


delay(100);
}
delay(1000);
}
Click on the tick icon to check your code, and then
click on the arrow to flash the code to the Arduino. In a
few seconds the board will reboot and the LED will
quickly flash three times, then pause, and repeat the
process. You have successfully tested your Arduino and
are now readytomoveontothenextproject.


Project2 Jobsaver
In this project we’ll build a simple switch that when
pressed will trigger a new tab to open in Firefox and
Chrome and open a page that looks like MS Outlook but
is really Reddit in disguise. This is handy for work, school
andhome,butdosoatyourownrisk!


Hardwaresetup
Usingabreadboardweneedtoconnectapushbutton
(momentaryswitch)overthecentralchannelsothat
thelegsarespreadoverthegap.Now,usingtwomaleto
malejumperwires,connectthetop-leftlegofthe
buttontoGNDontheArduino,thenconnectthetop-
rightlegtopin 2 oftheArduino.


Codingtheproject
IntheArduinoIoTCloud,clickonArduinoWebEditor
andconnecttheArduinotoyourcomputer.Createa
newsketchintheSketchbook,andthefirststepisto
importtheKeyboardlibrarythatwillenableourArduino
toemulateaUSBkeyboard.
#include<Keyboard.h>
Wecantypethisintothecode,orwecangotoLibraries
andthensearchfor“Keyboard”andthenclickInclude.
Thenextstepistoconfigurethepinusedforthe
button,andtostartthekeyboardinterface.Forthiswe
willuseafunctioncalledsetup,whichwillrunonce,
whentheboardispoweredup.UsingpinMode,wecan
telltheArduinothatwewishtousepin 2 asaninput,
andthattheinternalpull-upresistorshouldpullthepin
highsothatwhenthebuttonispresseditconnectspin
2 (high)toGND.Thiswillthenpullpin 2 lowandtrigger
theevent.
voidsetup(){
pinMode(2,INPUT_PULLUP);
Keyboard.begin();


Adding libraries
to our code is
simple via the
Arduino IoT Cloud
editor. Libraries
offer additional
features, such as
USB keyboard
emulation.

}
In order to continually run our code, we need to
create a loop that will contain the code. The first section
of code in the loop is a test to see if the button has been
pressed. Using digitalRead we can check pin 2, and if
the pin is HIGH, the button has not been pressed. This
means the code should take no action and keep waiting
for input. We should use a 50ms delay to prevent
accidental activation.
void loop() {
while (digitalRead(2) == HIGH) {
delay(50);
}
But what happens if the button is pressed? In this
case a delay is used to add 100ms delay before pressing
a series of keys to open a tab. To press a key we use
Keyboard.press and then pass the details of the key.
For keys such as Ctrl, Alt, Shift we need to provide the
details in a set manner, including whether it is the left
or right of each key. For regular character keys (letters,
numbers, punctuation) we pass the key as a string.
We then set a short 100ms delay to ensure that the
keypresses are registered.
delay(100);
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(‘t’);
delay(100);
Right now the keys are pressed down, and if left too
long this could cause issues. So to release the keys we
use Keyboard.releaseAll before moving on. When a new
tab is opened in Firefox/Chrome, the focus of the cursor
will be in the address bar, and in the next line of code
we’ll paste in a URL for the Reddit/Outlook website.
Then we release the keys once again before a final
100ms delay ends the loop.
Keyboard.releaseAll();
Keyboard.println(“http://pcottle.github.io/
MSOutlookit/”);
Keyboard.releaseAll();
delay(100);
}
Click on the tick icon to check your code, and then
click on the arrow to flash the code to the Arduino. In a
few seconds the board will reboot and be ready for use.
So go ahead, press the button and make sure that it
works before the boss walks in!

A button is used to trigger a series of events that opens a new browser
tab and a website that looks like an email client.
Free download pdf