You are on page 1of 8

!"""!" !"##$%& (#)*#"+, -*+."!

"




!"#$%#& () *+,-#".,%) /0 1$.2$+,$

|uu-2j uetting Staiteu - Aiuu-ez
0niveisity of Tasmania 2
!"##$%& (#)*#"+ , -*+.,"/
Objective
Exploie Aiuu-ez
0nueistanu how to impoit libiaiies
Bow to tioubleshoot

-*+.,"/
3"&45#6 has 2u mouules on an Aiuuino main boaiu.

!"#$% '()$*+,
Switch (Push button)
-$%#$% '()$*+,
Buzzei, RuBLEB, BC Notoi anu Step Notoi
-$%#$% ./0,#*123 '()$*+,
FNB (7 segments) anu RuBLCB
!"#$% .4+",(5,3 '()$*+,
Light, Sounu, Tempeiatuie, Bumiuity, S-axis acceleiation, 0ltiasonic, uas, PIR anu Piezo
Sensoi
6(''$"071%0(" '()$*+,
RFIB, Bluetooth, Wi-Fi
|uu-2j uetting Staiteu - Aiuu-ez
0niveisity of Tasmania S
01" 2$3*)*$"1
A 7,("$") is a collection of oiganizeu coue that pioviues extia functionality. The
language that is useu by Aiuuino is an expanueu veision of the C++, so the uevelopei is
fiee to use the C++ libiaiies as well. 0sing these libiaiies, an Aiuuino can communicate
uata with a computei attacheu via a 0SB cable. Seveial libiaiies exist foi communicating
with an Aiuuino.
To use an existing libiaiy in a sketch simply go to the Sketch menu, choose "Impoit
Libiaiy", anu pick fiom the libiaiies available. This will inseit an #incluue statement at
the top of the sketch foi each heauei (.h) file in the libiaiy's foluei. These statements
make the public functions anu constants uefineu by the libiaiy available to youi sketch.
They also signal the Aiuuino enviionment to link that libiaiy's coue with youi sketch
when it is compileu oi uploaueu
1
.
In this unit, we will leain how to get inputs fiom the Switch mouule. It is not easy to
unueistanu how the pins aie uefineu anu function.
480%79 .:$,9 ;$%%(",3


The .8,%9: 2/&47# is a component that connects two points in a ciicuit when you piess
it. The example tuins on an LEB when you piess the button.
We connect thiee wiies to the Aiuuino boaiu. The fiist goes fiom one leg of the
pushbutton thiough a pull-up iesistoi (heie 2.2 K0hms) to the S volt supply. The seconu
goes fiom the coiiesponuing leg of the pushbutton to giounu. The thiiu connects to a
uigital io pin (heie pin 7) which ieaus the button's state
2
.
When the pushbutton is open (un-piesseu) theie is no connection between the two legs
of the pushbutton, so the pin is connecteu to S volts (thiough the pull-up iesistoi) anu
we ieau a BIuB. When the button is closeu (piesseu), it makes a connection between its
two legs, connecting the pin to giounu, so that we ieau a L0W. (The pin is still connecteu
to S volts, but the iesistoi in-between them means that the pin is "closei" to giounu.)

|Button Numbeij



1
!""#$%%&&&'()*+,-.'//%0-%1(/2,-3%4,5)(),06
2
!""#$%%&&&'()*+,-.'//%0-%1+".),(2%3+4!5+"".-
|uu-2j uetting Staiteu - Aiuu-ez
0niveisity of Tasmania 4
(4$#56 (7.*5" 87+"

/* Switch Library - SwitchSerial */

/* ===== Switch Method =====
void begin();
// Waiting pushed key. if push key, return keyNum, else return 0;
uint8_t getPushKey();
uint8_t waitGetPushKey();
=========================== */

#include "Switch.h" //include Switch.h to use the library for switch

Switch pushSwitch; //call class Switch and named as pushSwitch

void setup() {
pushSwitch.begin(); //initialise Switch module
Serial.begin(9600); //initialise Serial Monitor
}

void loop() {
/*assign the variable pushKey as a pressed button number, otherwise the
pushKey will be assigned as 0 (Please refer to Switch module material).*/
uint8_t pushKey = pushSwitch.waitGetPushKey();
if(pushKey > 0) { // if pushKey is higher than 0 (any button is pressed),
Serial.print("Pushed : "); // print out Pushed :
Serial.println(pushKey); // print out the pressed button number
}
}


veiify anu uploau the coues, then it will give an output in the Seiial Nonitoi like below:



|uu-2j Switch
|uu-2j uetting Staiteu - Aiuu-ez
0niveisity of Tasmania S
(.)
(.)
!"# %& '()*+, %(, '&-, .(/+(0/+(%,- 1)"%23

void loop() {
/*assign the variable pushKey as a pressed button number, otherwise the
pushKey will be assigned as 0 (Please refer to Switch module material).*/
uint8_t pushKey = pushSwitch.waitGetPushKey();
if(pushKey > 0) { // if pushKey is higher than 0 (any button is pressed),
Serial.print("Pushed : "); // print out Pushed :
Serial.println(pushKey); // print out the pressed button number
}
}

%&3

void loop() {
/*assign the variable pushKey as a pressed button number, otherwise the
pushKey will be assigned as 0 (Please refer to Switch module material).*/
uint8_t pushKey = pushSwitch.waitGetPushKey();
if(pushKey > 8) { // if pushKey is higher than 0 (any button is pressed),
Serial.print("Pushed : "); // print out Pushed :
Serial.println(pushKey); // print out the pressed button number
}
}

anu then, veiify anu uploau the coues.
The seiial monitoi will uisplay the pusheu switch numbei when the switch numbei is
highei than 8.

<6=>??@AB@C
Tiy to combine two piogiams Blink anu Switch. Fill the blanks in the following coue to
tuin the LEB 0N 0FF using switch. 0se button numbei 1 to tuin 0N, numbei 2 to tuin
0FF.
4&-,3

#include "Switch.h" //include Switch.h to use the library for switch
Switch pushSwitch; //call class Switch and named as pushSwitch

void setup() {
pushSwitch.begin(); //initialise Switch module
Serial.begin(9600); //initialise Serial Monitor
pinMode(13, OUTPUT); //set the pin number 13 for LED
}
void loop() {
/*assign the variable pushKey as a pressed button number, otherwise the
pushKey will be assigned as 0 (Please refer to Switch module material).*/
uint8_t pushKey = pushSwitch.waitGetPushKey();

if(pushKey == ){ // if pushKey is 1 (button number 1 is pressed),

Serial.print("Turn the LED on"); // print out Turn the LED on

// turn the LED ON

}
else if(pushKey == ){ // if pushKey is 2 (button number 2 is pressed),

Serial.print("Turn the LED off"); // print out Turn the LED on

// turn the LED OFF

}
}
(.)
(.)
|uu-2j Switch_Challenge
|uu-2j uetting Staiteu - Aiuu-ez
0niveisity of Tasmania 6
:!D 4+",(5


;<= .#+./". allow you to sense motion, almost always useu to uetect whethei a human
has moveu in oi out of the sensois iange. They aie small, inexpensive, low-powei, easy
to use anu uon't weai out. Foi that ieason they aie commonly founu in appliances anu
gaugets useu in homes oi businesses. They aie often iefeiieu to as PIR, "Passive
Infiaieu", "Pyioelectiic", oi "IR motion" sensois.
:!D 4+",(5 4($57+ 6()+
/* ===== PIR Method =====
void begin(uint8_t pinNum);
int readValue();
======================== */

#include "PIR.h" //include PIR.h to use the library for PIR
#define PIR_PIN 39 //define PIN number for PIR sensor

PIR pir; //call class pir and named as PIR

void setup() {
pir.begin(PIR_PIN); //initialise PIR sensor
Serial.begin(9600); //initialise Serial Monitor
}

void loop() {
Serial.println(pir.readValue()); //print out the value of PIR Sensor
delay(1000); //delay 1000 milliseconds (1 second)
}


veiify anu uploau the coues, then it will give an output in the Seiial Nonitoi like below:

|uu-2j PIR
|uu-2j uetting Staiteu - Aiuu-ez
0niveisity of Tasmania 7
(.)
!"# %& '()*+, %(, '&-,3

void loop() {
Serial.println(pir.readValue()); //print out the value of PIR Sensor
delay(1000); //delay 1000 milliseconds (1 second)
}

%&3

void loop() {
if((pir.readValue()==1){ //if the PIR value is 1, execute the following
Serial.println(Detected); //print out the string Detected
}
delay(1000); //delay 1000 milliseconds (1 second)
}

anu then, veiify anu uploau the coues.
!"# %#&'() *+,'-+& .')) /'%0)(1 !"#$#%$#&' )*#+ $*# !"# %&'%() *(+,-& +&.&/.% 01&' 2
!"#$% !$' #()*+ ,% (- (". (/ .!* '*%'(-' -$%0*!


<6=>??@AB@C
Tiy to combine two piogiams Blink anu PIR. Fill the blanks in the following coue to tuin
the LEB 0N 0FF using switch. Tuin the LEB 0N when the PIR sensoi gets the value 1,
anu tuin the LEB 0FF when the PIR sensoi gets the value u.
4&-,3

#include "PIR.h" //include PIR.h to use the library for PIR
#define PIR_PIN 39 //define PIN number for PIR sensor

PIR pir; //call class pir and named as PIR

void setup() {
pir.begin(PIR_PIN); //initialise PIR sensor
Serial.begin(9600); //initialise Serial Monitor
pinMode(13, OUTPUT); //set the pin number 13 for LED
}
void loop() {
if((pir.readValue()) == ){ // if the PIR value is 1

Serial.print("Turn the LED on"); // print out Turn the LED on

// turn the LED ON

}
else if((pir.readValue()) == ){ // if the PIR value is 0

Serial.print("Turn the LED off"); // print out Turn the LED on

// turn the LED OFF

}
}


(.)
(.)
|uu-2j PIR_Challenge
|uu-2j uetting Staiteu - Aiuu-ez
0niveisity of Tasmania 8
974 #7 +7 :*7.3;"1677#
<

Theie will come a moment in youi expeiimentation when something will stop woiking
anu you will have to figuie out how to fix it. Tioubleshooting anu uebugging aie ancient
aits in which theie aie a few simple iules, but most of the iesults aie obtaineu thiough a
lot of woik.
As eveiy Aiuuino-baseu pioject is maue up of both haiuwaie anu softwaie, theie will be
moie than one place to look if something goes wiong. While looking foi a bug, you
shoulu opeiate along thiee lines:

1) 0nueistanuing
Try to understand as much as possible how the parts that youre using work and how theyre
supposed to contribute to the finished project. This approach will allow you to devise some
ways to test each component separately.
2) Simplification and segmentation
Try to break down (mentally) the project into its components by using the understanding you
have and figure out where the responsibility of each component begins and ends.
3) Exclusion and certainty
While investigating, test each component separately so that you can be absolutely certain that
each one works by itself. You will gradually build up confidence about which parts of project
are doing their job and which ones are dubious.







S
!"# %#&'#"( )*#+ ,'(-*./ 01&22*3/ 4&.5*6

You might also like