Hackatronics Using an Arduino Multi-function Multi-function Shield
By Kashif Baig © 2015 cohesivecomputing.co.uk
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
%ntroucing Hackatronics – &oing for 'un..................................... 'un............................................................ .............................................. .........................( ..( %nsta""ing the mu"ti#function shie" "i)rary........................... "i)rary.................................................. .............................................. .................................( ..........( !ore a)out this series................................................ series....................................................................... .............................................. ............................................( .....................( *art 1 Basic %nput + ,utput......................................... ,utput................................................................ .............................................. ...............................................5 ........................5 Using the shie"-s )eeper............................................... )eeper...................................................................... ...................................................... ............................... ........5 etecting )utton presses on the shie".................................................... shie"......................................................................................... ..................................... 5 /riting to the shie"-s igit isp"ay................................................. isp"ay............................................................................................. ............................................ &ontro""ing the shie"-s "ights.................................................................. "ights......................................................................................... ............................3 .....3 4eaing the va"ue of the shie"-s potentiometer..........................................................................3 *art 2 4eaing $ensors............................................ $ensors................................................................... .............................................. .................................................. ........................... &ounting pu"ses............................................. pu"ses.................................................................... .............................................. ........................................................ ................................. 4eaing the temperature using an !(5 sensor...................................................... sensor........................................................................ .................. 10 Using an a n H& $406 sonar mou"e...................................... mou"e............................................................. .............................................. ..................................10 ...........10 7etting ata from an !*U050 motion sensor..................... s ensor............................................ .............................................. ..............................11 .......11 *art ( 4ea" /or" App"ications.................. App"ications......................................... .............................................. .............................................. ......................................16 ...............16 &ounto8n timer................................................ timer....................................................................... .............................................. .................................................16 ..........................16 26 Hour A"arm &"ock......................................... &"ock................................................................ .............................................. .................................................15 ..........................15 Heart monitor.......................................... monitor................................................................. .............................................. ................................................... ............................ .........1 ......... 1 $urface inc"ine inicator........................................... inicator.................................................................. .............................................. ...........................................21 ....................21 $onar ranger............................................ ranger................................................................... .............................................. ................................................... ............................ .........2( ......... 2( $peeometer........................................... $peeometer................... ............................................... .............................................. .............................................................26 ......................................26 Appenices......................................... Appenices.................. .............................................. .............................................. .............................................. ............................................ .......................23 ..23 !u"ti#function $hie" i)rary He"p.......................................................... He"p............................................................................... ................................. ............23 23 !*U050 He"p.............................................. He"p..................................................................... .............................................. .............................................. ...............................(0 ........(0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
%ntroucing Hackatronics – &oing for 'un..................................... 'un............................................................ .............................................. .........................( ..( %nsta""ing the mu"ti#function shie" "i)rary........................... "i)rary.................................................. .............................................. .................................( ..........( !ore a)out this series................................................ series....................................................................... .............................................. ............................................( .....................( *art 1 Basic %nput + ,utput......................................... ,utput................................................................ .............................................. ...............................................5 ........................5 Using the shie"-s )eeper............................................... )eeper...................................................................... ...................................................... ............................... ........5 etecting )utton presses on the shie".................................................... shie"......................................................................................... ..................................... 5 /riting to the shie"-s igit isp"ay................................................. isp"ay............................................................................................. ............................................ &ontro""ing the shie"-s "ights.................................................................. "ights......................................................................................... ............................3 .....3 4eaing the va"ue of the shie"-s potentiometer..........................................................................3 *art 2 4eaing $ensors............................................ $ensors................................................................... .............................................. .................................................. ........................... &ounting pu"ses............................................. pu"ses.................................................................... .............................................. ........................................................ ................................. 4eaing the temperature using an !(5 sensor...................................................... sensor........................................................................ .................. 10 Using an a n H& $406 sonar mou"e...................................... mou"e............................................................. .............................................. ..................................10 ...........10 7etting ata from an !*U050 motion sensor..................... s ensor............................................ .............................................. ..............................11 .......11 *art ( 4ea" /or" App"ications.................. App"ications......................................... .............................................. .............................................. ......................................16 ...............16 &ounto8n timer................................................ timer....................................................................... .............................................. .................................................16 ..........................16 26 Hour A"arm &"ock......................................... &"ock................................................................ .............................................. .................................................15 ..........................15 Heart monitor.......................................... monitor................................................................. .............................................. ................................................... ............................ .........1 ......... 1 $urface inc"ine inicator........................................... inicator.................................................................. .............................................. ...........................................21 ....................21 $onar ranger............................................ ranger................................................................... .............................................. ................................................... ............................ .........2( ......... 2( $peeometer........................................... $peeometer................... ............................................... .............................................. .............................................................26 ......................................26 Appenices......................................... Appenices.................. .............................................. .............................................. .............................................. ............................................ .......................23 ..23 !u"ti#function $hie" i)rary He"p.......................................................... He"p............................................................................... ................................. ............23 23 !*U050 He"p.............................................. He"p..................................................................... .............................................. .............................................. ...............................(0 ........(0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
Introducing Hackatronics – Coding for Fun /hen % 8as a)out 10 years o" my fo"ks )ought me a &6 computer9 an very soon % 8as "earning ho8 to connect some )asic components an sensors to its :oystick an para""e" ports to try an o some interesting things. ;his 8as 8ay )ack in 1<(. ,n one occasion9 % actua""y manage to fry this =(50 computer 8hi"e so"ering a 8ire that 8as connecte to a port 8hen it 8as s8itche on. 'ortunate"y9 % got the &6 repaire at minima" cost9 )ut on-t try something "ike that yourse"f. ;oay9 the 4asp)erry *i an Aruino range of microcontro""ers is a great 8ay to start to "earn ho8 to 8rite coe that connects 8ith the outsie 8or". %n aition9 there are numerous e"ectronic a# ons 8ith components a"reay )ui"t on to them9 them9 reay to )e uti"i>e 8ith a )it of coe. ,ne such a#on is a mu"ti#function Aruino shie" avai"a)"e at "o8 cost from internet supp"iers9 one such )eing Ho))y &omponents? &omponents ? $imp"e %+, that is usua""y taken for grante on *&s9 "ike reaing key presses9 outputting to a isp"ay9 an souning an a"arm9 often get in the 8ay of the focus of the main task 8hen eve"oping for microcontro""ers. %t is for this reason % have eve"ope a "i)rary for this mu"ti#function shie" that simp"ifies )asic an munane %+, operations. % a"so provie a set of rea" 8or" app"ications that make use of this "i)rary as part of a coing series9 so those ne8 to coing on the Aruino can e@periment 8ith an enhance them. /e""9 that-s ho8 % "earnt ho8 to coe a"" those years ago. $ome fami"iarity 8ith the Aruino p"atform is assume9 as is the insta""ation of the Aruino eve"opment environment. environment . A emo vieo of some of the Aruino app"ications is avai"a)"e from my 8e)site.
Installing the multi-function shield library %f you haven-t a"reay one so9 you 8i"" nee to first insta"" the ;imer,ne an $oft8are %2& "i)raries using instructions for insta""ing Aruino "i)raries. "i)raries . ou can o8n"oa the mu"ti#function shie" "i)rary from the "ink )e"o8 an insta"" as a .>ip "i)rary9 referring to the instructions in the "ink a)ove?
!u"ti#function $hie" i)rary A"" source coe use in series
%f for any reason you haven-t )een successfu" insta""ing any of the "i)raries9 then o8n"oa this "i)rary )un"e instea9 )un"e instea9 un>ip an copy to your ocumentsAruino"i)raries fo"er Cif using /ino8sD or ,$ eEuiva"ent. A"though 8e o everything to ensure our o8n"oas are free from viruses an ma"8are9 p"ease check that your virus an ma"8are scanning soft8are is up to ate )efore han. % must point out that )y fo""o8ing the Hackatronics series9 you agree to o so at your o8n risk9 an agree to take fu"" responsi)i"ity for any "oss or amages you may incur upon yourse"f or others. %f you-re a ki starting out9 )e sure to have supervision of a responsi)"e au"t.
More about this series ;his series is ivie in to three main parts? 1. Basi Basicc %npu %nputt + ,ut ,utpu putt
*age ( of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
2. 4eaing $ensors (. 4ea" /or" App"ications *art 1 emonstrates the ease 8ith 8hich the mu"ti#function shie" )uttons9 )eeper an isp"ay can uti"i>e )y using the shie" "i)rary9 conseEuent"y making it easier to concentrate on the "ogic of the app"ication. *art 2 emonstrates ho8 the shie" "i)rary can )e use to rea va"ues from e@terna" sensors9 such as temperature9 sonar an motion sensors9 an ho8 to process e"ectronic pu"ses from an e@terna" source. *art ( e@p"ores 8orking app"ications using the "i)rary an the mu"ti#function shie"?
26 hour a"arm c"ock Heart monitor – CreEuires heart pu"se sensorD &ount o8n timer $urface inc"ine "eve" inicator– CreEuires !*U050 motion sensorD $onar ranger – CreEuires H& $406 sonar mou"eD $peeometer – CreEuires magnet an ree s8itchD
ach of these has scope to )e )ui"t upon an e@pane9 )ut % "eave that to you. 7et coing an have funF
*age 6 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
Part 1 Basic Input / Output ;his is part one of the App"ie Hackatronics $eries for the Aruino !u"ti#function shie"9 8hich sho8s ho8 to use the shie" "i)rary to access the mu"ti#function shie" )uttons9 )u>>er an isp"ay. %f you haven-t a"reay one so9 you-"" nee to o8n"oa the source coe an insta"" the "i)raries using the "inks in the introuction. By fo""o8ing the Hackatronics series9 you agree to o so at your o8n risk9 an agree to take fu"" responsi)i"ity for any "oss or amages you may incur upon yourse"f or others.
Using the shield’s beeper ;he mu"ti#function shie" "i)rary provies a f"e@i)"e 8ay to soun ifferent types of a"arms using the )eeper. ;he actua" timing an souning of the )eeper is contro""e in the )ackgroun using interrupts9 8hich means your app"ication can continue to focus on performing its main task. #include #include #include void setup() { // put your setup code here to run once! Timer".initili$e()% MFS.initili$e(&Timer")%
// initili$e multi'unction shield lirry
// *OT+ eep control is perormed in the c,-round i.e. eep() is non loc,in-. // short eep or milliseconds MFS.eep()% dely(")% // 0 short eeps MFS.eep(2 // 2 // 0 // 1 // 2 // )%
repeted 1 times. eep or 2 milliseconds silent or 2 milliseconds repet ove cycle 0 times loop 1 times 3it 2 milliseconds et3een loop
4 void loop() { // put your min code here to run repetedly! 4
Detecting button presses on the shield /ith the mu"ti#function shie" "i)rary9 ifferent types of )utton presses can )e etecte? short press9 "ong press9 )utton re"ease after short press9 )utton re"ease after "ong press. ;he sketch )e"o8 isp"ays the type of )utton press in the seria" monitor 8ino8. &heck 8hat happens you press an or ho" mu"tip"e )uttons together9 an for ifferent urations. #include #include #include void setup() {
*age 5 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
// put your setup code here to run once! Seril.e-in(56)% Timer".initili$e()% MFS.initili$e(&Timer")% // initili$e multi'unction shield lirry 4
void loop() { // put your min code here to run repetedly! yte tn 7 MFS.-et8utton()% // *ormlly it is suicient to compre the return // vlue to predeined mcros e.-. 89TTO*:":;+SS+= // 89TTO*:":O*?:;+SS+= etc. i (tn) { yte utton*umer 7 tn & 8""""""% yte utton@ction 7 tn & 8""% Seril.print(A89TTO*:A)% Seril.3rite(utton*umer B CC)% Seril.print(A:A)%
i (utton@ction 77 89TTO*:;+SS+=:D*=) { Seril.println(A;+SS+=A)% 4 else i (utton@ction 77 89TTO*:SEOT:++@S+:D*=) { Seril.println(ASEOT:++@S+A)% 4 else i (utton@ction 77 89TTO*:O*?:;+SS+=:D*=) { Seril.println(AO*?:;+SS+=A)% 4 else i (utton@ction 77 89TTO*:O*?:++@S+:D*=) { Seril.println(AO*?:++@S+A)% 4
4 4
Writing to the shield’s digit display ;he management of the mu"ti#function shie"-s igit isp"ay is performe in the )ackgroun using interrupts9 8hich means your app"ication can continue to focus on performing its main task. $tring9 integer an f"oat va"ues are 8ritten to the isp"ay as emonstrate in the sketch )e"o8? #include #include #include void setup() { // put your setup code here to run once! Timer".initili$e()% MFS.initili$e(&Timer")% MFS.3rite(AEiA)% dely()% MFS.3rite('1)% dely()% MFS.3rite(1."0" )% dely()% 4
// initili$e multi'unction shield lirry
// disply to deciml plces.
int counter7% yte ended 7 lse%
*age of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
void loop() { // put your min code here to run repetedly!
4
i (counter < ) { MFS.3rite((int)counter)% counterBB% 4 else i (Gended) { ended 7 true% MFS.3rite(A+ndA)% MFS.lin,=isply(=D?DT:@ O*)% 4 dely(2)%
Controlling the shield’s LED lights A"though it isn-t strict"y necessary to use the mu"ti#function shie" "i)rary to contro" the "ights of the shie"9 support is provie in cases 8here your app"ication nees the s to perform )asic )"ink operations. B"inking is manage in the )ackgroun using interrupts. #include #include #include void setup() { // put your setup code here to run once! Timer".initili$e()% MFS.initili$e(&Timer")%
// initili$e multi'unction shield lirry
MFS.3riteeds(+=:@ O*)% dely()% MFS.lin,eds(+=:" H +=: O*)% dely()% MFS.lin,eds(+=:" H +=: OFF)% MFS.lin,eds(+=:1 H +=:0 O*)% dely()% MFS.lin,eds(+=:@ O*)% dely()% MFS.lin,eds(+=:@ OFF)% MFS.3riteeds(+=:@ OFF)% 4 void loop() { // put your min code here to run repetedly! 4
Reading the alue of the shield’s potentiometer ;his sketch emonstrates ho8 the va"ue of the preset pot is rea an isp"aye on the mu"ti#function shie". After up"oaing this sketch9 turn the scre8 of the potentiometer to see the reaing change on the igit isp"ay. #include #include #include void setup() { // put your setup code here to run once! Timer".initili$e()%
*age 3 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
MFS.initili$e(&Timer")%
cohesivecomputing.co.uk
// initili$e multi'unction shield lirry
4 void loop() { // put your min code here to run repetedly! MFS.3rite(nlo-ed(;OT:;D*))% dely(")% 4
A"" the coe samp"es an app"ications have )een teste an 8ork. %f you e@perience any ifficu"ties9 p"ease "eave a comment9 an %-"" get )ack to you as soon as % can.
*age < of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
Part 2 Reading Sensors ;his is part t8o of the App"ie Hackatronics $eries for the Aruino !u"ti#function shie"9 an sho8s ho8 the mu"ti#function shie" "i)rary can )e use to rea va"ues from e@terna" sensors9 such as temperature9 sonar an motion sensors9 an ho8 to process e"ectronic pu"ses from an e@terna" source. %f you haven-t a"reay one so9 you-"" nee to o8n"oa the source coe an insta"" the "i)raries using the "inks in the introuction. By fo""o8ing the Hackatronics series9 you agree to o so at your o8n risk9 an agree to take fu"" responsi)i"ity for any "oss or amages you may incur upon yourse"f or others.
Counting pulses ;he mu"ti#function shie" "i)rary has support for counting pu"ses Cup to 500H>D app"ie to an input pin of the Aruino. ;he counting of pu"ses is manage in the )ackgroun using interrupts9 8hich a""o8s your app"ication to focus on performing its main task. After up"oaing this sketch9 repeate"y press )utton 1 to generate the pu"ses an see a reaing of the press rate on the igit isp"ay. #include #include #include void setup() { // put your setup code here to run once! Timer".initili$e()% MFS.initili$e(&Timer")% // initili$e multi'unction shield lirry MFS.init;ulseDnIounter( 89TTO*:":;D* // use utton " s mens o -enertin- pulses. "2 // the numer o milliseconds to 3it or pulse eore resettin- pulse in period to . OW // tri--er pulse on OW input. )% 4
void loop() { // put your min code here to run repetedly! // ?et the period o the most recent pulse (in milliseconds). // *OT+! pulse mesurements re ctully perormed usin- interrupts. unsi-ned int pulse;eriodMs 7 MFS.-et;ulseDn;eriod()% i (pulse;eriodMs 77 ) { MFS.3rite(. ")% 4 else { MFS.3rite(". / pulse;eriodMs ")% to " deciml plce. 4 4
*age of (0
// clculte pulses per second. =isply
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
Reading the temperature using an LM!" sensor ;he mu"ti#function shie" has a socket for accepting an !(5 temperature sensor9 8hich must )e inserte correct"y9 other8ise you may irrepara)"y amage your Aruino9 or computer connecte to it. ou 8i"" kno8 8hen the sensor is incorrect"y connecte9 )ecause it 8i"" )ecome very hot. ;he mu"ti#function shie" "i)rary provies three "eve"s of fi"tering for smoothing the reaings from this sensor?
#include #include #include // *OT+! m,e sure Jumper K" is removed rom shield nd tht M12 is inserted correctly. void setup() { // put your setup code here to run once! Timer".initili$e()% MFS.initili$e(&Timer")% // initili$e multi'unction shield lirry // Dnitili$e usin- lo3 pss ilter. // Ihoose either! SMOOTED*?:*O*+ SMOOTED*?:MO=+@T+ or SMOOTED*?:STO*? MFS.initM12(SMOOTED*?:MO=+@T+)% 4 void loop() { // put your min code here to run repetedly! int tempIenti-rde 7 MFS.-etM12=t()% // -et centi-rde in "/" o de-ree. MFS.3rite((lot)tempIenti-rde / " ")%
// disply temp to " deciml plce.
dely(")% 4
Using an #C $R%& sonar module /hen using an H& $406 sonar mou"e9 it is avisa)"e to use the mu"ti#function shie" "i)rary to rea an ca"cu"ate istance va"ues if interrupts are turne on. ;he reason for this is that the "i)rary-s interrupt service routine affects the timing reEuirements of this sonar mou"e9 an as such the "i)rary compensates for it. ;he "i)rary a"so offers three "eve"s of fi"tering for smoothing the reaings from the sonar mou"e. ;he trigger an echo pins of the sonar mou"e are connecte to Aruino pins 5 an respective"y.
#include #include #include const int Tri-;in 7 2%
*age 10 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
const int +cho;in 7 6%
void setup() { // put your setup code here to run once! pinMode(Tri-;in O9T;9T)% pinMode(+cho;in D*;9T)% Timer".initili$e()% MFS.initili$e(&Timer")%
// initili$e multi'unction shield lirry
// initili$e 3ith lo3 pss ilter! SMOOTED*?:*O*+ SMOOTED*?:MO=+@T+ or SMOOTED*?:STO*? MFS.initSonr(SMOOTED*?:MO=+@T+)% 4
void loop() { // put your min code here to run repetedly! MFS.3rite((int)MFS.-etSonr=tIm(Tri-;in +cho;in))% dely(")% 4
'etting data from an M(U)%"% motion sensor ;he mu"ti#function shie" oes not e@pose the har8are %2& pins of some Aruino )oars9 such as the eonaro. ;he fo""o8ing sketch uses the shie" "i)rary to rea ra8 va"ues from the !*U050 sensor9 using either the /ire "i)rary or a soft8are imp"ementation of %2&. o8n"oa the fu"" source coe in orer to up"oa the sketch to an Aruino9 after 8hich press )utton 1 on the shie" to togg"e reporting of sensor ata9 an press )utton 2 to cyc"e through acce"eration9 gyro an temperature va"ues.
#deine :SOFTDI:E #include #include #include #include #include ASot3reDI.hA #include ADI.hA #include AM;962.hA #deine SOFTW@+:DI M;962 M;9% void displyEedin-(yte mode)%
void setup() { Seril.e-in(56)% // put your setup code here to run once! Timer".initili$e()% #i deined (SOFTW@+:DI) // 9se sot3re DI
*age 11 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
// 9no nd eonrdo use pin 2 or SI nd 6 or S=@. Me-26 use pin @2 or S=@. SotDI".initili$e()% M;9.initili$e(&SotDI" M;9:=+F@9T:@==+SS << ")% #else // 9se hrd3re DI Wire.e-in()% DI".initili$e(&Wire)% M;9.initili$e(&DI" M;9:=+F@9T:@==+SS)% #endi MFS.initili$e(&Timer")% MFS.3rite(A@ccA)% 4
yte displyLlues 7 true% yte displyMode 7 %
void loop() { // put your min code here to run repetedly! yte tn 7 MFS.-et8utton()% // 9se utton " to to--le reportin- o sensor vlues. i (tn 77 89TTO*:":;+SS+=) { displyLlues 7 GdisplyLlues% i (displyLlues) { displyEedin-(displyMode)% 4 else { MFS.3rite(AOA)% 4
4
i (displyLlues) { // 9se utton to cycle thou-h the disply modes. i (tn 77 89TTO*::;+SS+=) { displyModeBB% i (displyMode 77 1) { displyMode 7 % 4 displyEedin-(displyMode)% 4
i (displyMode 77 ) { // disply r3 ccelertion vlues. M;9.-et@ccel3()% Seril.print((lot)M;9.ccel::3 / M;9.ccelScleLlue)% Seril.print(ANtA)% Seril.print((lot)M;9.ccel::3 / M;9.ccelScleLlue)% Seril.print(ANtA)% Seril.print((lot)M;9.ccel:P:3 / M;9.ccelScleLlue)% Seril.print(ANtNnA)% 4 else i (displyMode 77 ") { // disply r3 -yrovlues M;9.-et?yro3()% Seril.print((lot)M;9.-yro::3 / M;9.-yroScleLlue)%
*age 12 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
Seril.print(ANtA)% Seril.print((lot)M;9.-yro::3 / M;9.-yroScleLlue)% Seril.print(ANtA)% Seril.print((lot)M;9.-yro:P:3 / M;9.-yroScleLlue)% Seril.print(ANtNnA)%
4 else i (displyMode 77 ) { // disply temperture vlue. Seril.println((lot)M;9.-etTemp"th() / ")% 4 4 dely(2)% 4
void displyEedin-(yte mode) { i (mode 77 ) { Seril.println(A@ccelertion in - ("- 7 5.Q m/s/s)A)% Seril.println(ANtNtPA)% MFS.3rite(A@ccA)% 4 else i (mode 77 ") { Seril.println(A?yro n-ulr velocity in de-rees / secondA)% Seril.println(ANtNtPA)% MFS.3rite(A?yroA)% 4 else i (mode 77 ) { Seril.println(ATemperture in de-rees celsius.A)% MFS.3rite(ATeA)% 4 4
A"" the coe samp"es an app"ications have )een teste an 8ork. %f you e@perience any ifficu"ties9 p"ease "eave a comment9 an %-"" get )ack to you as soon as % can.
*age 1( of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
Part 3 Real orld !pplications ;his is part three of the App"ie Hackatronics $eries for the Aruino !u"ti#function shie"9 an e@p"ores 8orking app"ications using the "i)rary an the mu"ti#function shie". %f you haven-t a"reay one so9 you-"" nee to o8n"oa the source coe an insta"" the "i)raries using the "inks in the introuction. 'or each of the app"ications )e"o8 there is an accompanying on"ine vieo9 as 8e"" as a summary vieo. By fo""o8ing the Hackatronics series9 you agree to o so at your o8n risk9 an agree to take fu"" responsi)i"ity for any "oss or amages you may incur upon yourse"f or others.
Countdo*n timer ;his counto8n timer is simi"ar to a counto8n timer you might fin in a micro8ave oven. ou set the time9 start the counto8n timer9 an 8hen it reaches >ero9 the a"arm souns. ou can pause+continue the timer9 an reset to >ero. Use the mu"ti#function shie" )uttons 2 an ( to set the minutes an secons. A short press of )utton 1 starts or stops the timer9 an a "ong press resets it. *ossi)"e enhancements for this app"ication are to have a evice s8itche on on"y 8hi"st the timer is counting o8n. #include #include #include
enum Iount=o3nModeLlues { IO9*TD*?:STO;;+= IO9*TD*? 4% yte count=o3nMode 7 IO9*TD*?:STO;;+=% yte tenths 7 % chr seconds 7 % chr minutes 7 % void setup() { // put your setup code here to run once! Timer".initili$e()% MFS.initili$e(&Timer")% // initili$e multi'unction shield lirry MFS.3rite()% Seril.e-in(56)% 4
void loop() { // put your min code here to run repetedly! yte tn 7 MFS.-et8utton()% s3itch (count=o3nMode) { cse IO9*TD*?:STO;;+=! i (tn 77 89TTO*:":SEOT:++@S+ && (minutes B seconds) > ) { // strt the timer count=o3nMode 7 IO9*TD*?%
*age 16 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
4 else i (tn 77 89TTO*:":O*?:;+SS+=) { // reset the timer tenths 7 % seconds 7 % minutes 7 % MFS.3rite(minutesR" B seconds)% 4 else i (tn 77 89TTO*::;+SS+= HH tn 77 89TTO*::O*?:;+SS+=) { minutesBB% i (minutes > 6) { minutes 7 % 4 MFS.3rite(minutesR" B seconds)% 4 else i (tn 77 89TTO*:1:;+SS+= HH tn 77 89TTO*:1:O*?:;+SS+=) { seconds B7 "% i (seconds >7 6) { seconds 7 % 4 MFS.3rite(minutesR" B seconds)% 4 re,%
cse IO9*TD*?! i (tn 77 89TTO*:":SEOT:++@S+ HH tn 77 89TTO*:":O*?:++@S+) { // stop the timer count=o3nMode 7 IO9*TD*?:STO;;+=% 4 else { // continue countin- do3n tenthsBB%
i (tenths 77 ") { tenths 7 % seconds''%
i (seconds < && minutes > ) { seconds 7 25% minutes''% 4
i (minutes 77 && seconds 77 ) { // timer hs reched so sound the lrm MFS.eep(2 2 1)% // eep 1 times 2 milliseconds on / 2 o count=o3nMode 7 IO9*TD*?:STO;;+=% 4 MFS.3rite(minutesR" B seconds)% 4 dely(")%
4 re,%
4 4
+& #our ,larm Cloc
*age 15 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
;his app"ication emonstrates a igita" c"ock 8ith an a"arm capa)i"ity. /hen the Aruino is po8ere on9 the mu"ti#function shie" isp"ay f"ashes unti" the user sets the time. Ho" )utton 1 to set the time or a"arm. /hen setting the time use )utton ( to set the hour or minutes. *ress )utton 2 to vie8 a"arm time or cance" the a"arm if in progress. Ho"ing )utton ( ena)"es or isa)"es the a"arm C1 inicates a"arm is ena)"eD. *ossi)"e enhancements to this app"ication are to have a snoo>e feature9 or to have mu"tip"e on+off perios uring the ay for a evice. #include #include #include /R utton " ! hold to set time or lrm utton ! press to vie3 lrm time or cncel lrm i in pro-ress utton 1 ! increment hour / minute 3hen settin- (lrm) time. Eold to to--le lrm settin-. +="
! on 7 lrm enled
R/ voltile voltile voltile voltile voltile
unsi-ned int cloc,MilliSeconds 7 % yte cloc,Seconds 7 % yte cloc,Minutes 7 % yte cloc,Eours 7 "% yte cloc,+nled 7 "%
yte lrmMinutes 7 1% yte lrmEours 7 6% voltile yte lrm+nled 7 lse% yte lrmTo--le;ressed 7 lse%
enum displyModeLlues { MO=+:IOI:TDM+ MO=+:IOI:TDM+:S+T:EO9 MO=+:IOI:TDM+:S+T:MD*9T+ MO=+:@@M:TDM+ MO=+:@@M:TDM+:S+T:EO9 MO=+:@@M:TDM+:S+T:MD*9T+ 4% yte displyMode 7 MO=+:IOI:TDM+% //''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' void setup() { Timer".initili$e()% MFS.userDnterrupt 7 cloc,DS% MFS.initili$e(&Timer")% MFS.lin,=isply(=D?DT:@)% //MFS.eep(2)% 4
void loop() { // put your min code here to run repetedly! yte tn 7 MFS.-et8utton()% s3itch (displyMode) { cse MO=+:IOI:TDM+! displyTime(cloc,Eours cloc,Minutes)%
*age 1 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
i (tn 77 89TTO*::;+SS+=) { MFS.eep()% // cncel the lrm. displyMode 7 MO=+:@@M:TDM+% 4 else i (tn 77 89TTO*:":O*?:;+SS+=) { MFS.lin,=isply(=D?DT:@ OFF)% MFS.lin,=isply(=D?DT:" H =D?DT:)% displyMode 7 MO=+:IOI:TDM+:S+T:EO9% cloc,+nled 7 lse% cloc,MilliSeconds 7 % cloc,Seconds 7 % 4 else i (tn 77 89TTO*:1:O*?:;+SS+= && GlrmTo--le;ressed) { lrmTo--le;ressed 7 true% lrm+nled 7 Glrm+nled% MFS.3riteeds(+=:" lrm+nled)% 4 else i (tn 77 89TTO*:1:O*?:++@S+) { lrmTo--le;ressed 7 lse% 4 re,% cse MO=+:IOI:TDM+:S+T:EO9! i (tn 77 89TTO*:":;+SS+=) { MFS.lin,=isply(=D?DT:" H =D?DT: OFF)% MFS.lin,=isply(=D?DT:1 H =D?DT:0)% displyMode 7 MO=+:IOI:TDM+:S+T:MD*9T+% 4 else i (tn 77 89TTO*:1:;+SS+= HH tn 77 89TTO*:1:O*?:;+SS+=) { cloc,EoursBB% i (cloc,Eours >7 0) { cloc,Eours 7 % 4 displyTime(cloc,Eours cloc,Minutes)% 4 re,% cse MO=+:IOI:TDM+:S+T:MD*9T+! i (tn 77 89TTO*:":;+SS+=) { MFS.lin,=isply(=D?DT:1 H =D?DT:0 OFF)% displyMode 7 MO=+:IOI:TDM+% cloc,+nled 7 true% 4 else i (tn 77 89TTO*:1:;+SS+= HH tn 77 89TTO*:1:O*?:;+SS+=) { cloc,MinutesBB% i (cloc,Minutes >7 6) { cloc,Minutes 7 % 4 displyTime(cloc,Eours cloc,Minutes)% 4 re,% cse MO=+:@@M:TDM+! displyTime(lrmEours lrmMinutes)% i (tn 77 89TTO*::SEOT:++@S+ HH tn 77 89TTO*::O*?:++@S+) { displyMode 7 MO=+:IOI:TDM+% 4 else i (tn 77 89TTO*:":O*?:;+SS+=) {
*age 13 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
MFS.lin,=isply(=D?DT:@ OFF)% MFS.lin,=isply(=D?DT:" H =D?DT:)% displyMode 7 MO=+:@@M:TDM+:S+T:EO9% lrm+nled 7 lse% 4 re,%
cse MO=+:@@M:TDM+:S+T:EO9! i (tn 77 89TTO*:":;+SS+=) { MFS.lin,=isply(=D?DT:" H =D?DT: OFF)% MFS.lin,=isply(=D?DT:1 H =D?DT:0)% displyMode 7 MO=+:@@M:TDM+:S+T:MD*9T+% 4 else i (tn 77 89TTO*:1:;+SS+= HH tn 77 89TTO*:1:O*?:;+SS+=) { lrmEoursBB% i (lrmEours >7 0) { lrmEours 7 % 4 displyTime(lrmEours lrmMinutes)% 4 re,%
cse MO=+:@@M:TDM+:S+T:MD*9T+! i (tn 77 89TTO*:":;+SS+=) { MFS.lin,=isply(=D?DT:1 H =D?DT:0 OFF)% displyMode 7 MO=+:IOI:TDM+% lrm+nled 7 true% MFS.3riteeds(+=:" O*)% 4 else i (tn 77 89TTO*:1:;+SS+= HH tn 77 89TTO*:1:O*?:;+SS+=) { lrmMinutesBB% i (lrmMinutes >7 6) { lrmMinutes 7 % 4 displyTime(lrmEours lrmMinutes)% 4 re,%
4 4
void displyTime (yte hours yte minutes) { chr time2U% sprint(time AV1dA (hours R ") B minutes)% MFS.3rite(time ")% 4 //'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' void cloc,DS () { // ;erorm ripple count or ll time components. i (cloc,+nled) { cloc,MilliSecondsBB% i (cloc,MilliSeconds >7 ") { cloc,MilliSeconds 7 % cloc,SecondsBB% i (cloc,Seconds >7 6) { cloc,Seconds 7 %
*age 1< of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
cloc,MinutesBB% i (cloc,Minutes >7 6) { cloc,Minutes 7 % cloc,EoursBB% i (cloc,Eours >7 0) { cloc,Eours 7 % 4 4
// D current time coincides 3ith lrm time nd lrm is enled en--e the lrm. i (lrm+nled && (cloc,Minutes 77 lrmMinutes) && (cloc,Eours 77 lrmEours)) { MFS.eep( " // on period 2 // o period 0 // numer o cycles " // numer o loop cycles 2 // dely et3een loop cycles )% 4 4 4 4 4
#eart monitor ;his app"ication reEuires a heart )eat sensor simi"ar to the one sho8n in the image Cthat can )e purchase from various internet supp"iersD9 8hich uses infrare "ight to etect pu"ses in a finger or ear "o)e. /hen norma"i>e9 the sensor outputs an ana"ogue signa" of 2.5 vo"ts9 8hich rises an rops every time the )"oo f"o8 changes ue to a heart )eat. ;he app"ication 8orks )y counting every time the signa" rises a)ove ( vo"ts9 then rops )e"o8 2.5 vo"ts9 an measures time )et8een pu"ses to ca"cu"ate the )eats per minute. ;he )eeper is soune every time a heart )eat is etecte. ;he sensor output 8i"" nee to )e connecte to Aruino pin A5 e@pose on the mu"ti#function shie". /A4G%G7? ;his app"ication is not intene for meica" purposes. After po8ering on the Aruino9 gent"y )ut firm"y p"ace the ine@ finger on the sensor an 8ait for the isp"ay to start )"inking. ;his inicates the sensor has norma"i>e9 after 8hich the shie" isp"ay 8i"" sho8 the )eats per minute9 an the )eeper 8i"" soun. %f the isp"ay remains at 0 an oesn-t )"ink after severa" secons9 remove the finger9 8ait for a 8hi"e an try again. nsure the finger is p"ace 8ith consistent pressure. *ossi)"e moification for this app"ication is to have an "ight synchronise 8ith each heart )eat. #include #include #include void initili$eSensoredin-()% int dt0U% yte dtDd7%
*age 1 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
yte pulse=etected 7 lse% int lst;ulseTime 7 '"%
void setup() { // put your setup code here to run once! Timer".initili$e()% MFS.initili$e(&Timer")% initili$eSensoredin-()% //Seril.e-in(56)% 4
void loop() { i (MFS.-etTimer() 77 ) { MFS.setTimer(")% // reset millisecond countdo3n timer. i (lst;ulseTime G7 '") { lst;ulseTime 7 " B lst;ulseTime% 4 4 int sensorLlue 7 nlo-ed(@2)%
// red the sensor.
i (sensorLlue < HH sensorLlue > 5) { // Sensor hsnCt normli$ed chec, ho3 lon- or in milliseconds. i (lst;ulseTime G7 '" && lst;ulseTime ' MFS.-etTimer() > ) { initili$eSensoredin-()% 4 4 else i (sensorLlue > (1 R "0) / 2) // vlue is risin- so must e strt o pulse. { i (Gpulse=etected) { pulse=etected 7 true% i (lst;ulseTime 77 '") { lst;ulseTime 7 MFS.-etTimer()% 4 else { int pulse;eriod 7 lst;ulseTime ' MFS.-etTimer()% et3een pulses in millseconds. lst;ulseTime 7 MFS.-etTimer()% int pm 7 6 / pulse;eriod%
// clculte time
// clculte ets per minute.
i (pm < 02 HH pm > 1) // pm is outside cceptile rn-e so cler the dt uer. { initili$eSensoredin-()% 4 else { // pm is 3ithin rn-e ut still need to clculte ver-e. dtdtDdBBU 7 pm% i (dtDd >7 0) { dtDd 7 % 4
*age 20 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
i (dtU && dt"U && dtU && dt1U) // chec, i dt uer is ull eore clcultin- v- pm. { int v-8pm 7 (dtU B dt"U B dtU B dt1U) / 0% MFS.lin,=isply(=D?DT:@ OFF)% MFS.3rite(v-8pm)% MFS.eep()%
4 else { // uer not ull so lin, the disply. MFS.lin,=isply(=D?DT:@ O*)% 4
4 4 4 4 else i (sensorLlue < ("0 / )) pulse. { pulse=etected 7 lse% 4
// vlue is llin- so must e end o
//Seril.println(sensorLlue)% //dely(")% 4
// Dnitili$e the red uer nd disply. void initili$eSensoredin-() { lst;ulseTime 7 % dtDd 7 % or (int i7% i<0% iBB) { dtiU 7 % 4 MFS.3rite()% MFS.lin,=isply(=D?DT:@ OFF)% 4
$urface incline indicator ;he surface inc"ine inicator app"ication uses the !*U050 motion sensor to etermine the ang"e of inc"ination of a f"at surface. ou 8i"" nee to o8n"oa the fu"" source coe )efore up"oaing to the Aruino. Because the app"ication uses soft8are %2&9 8hen using 4( )oars Uno an eonaro9 use pin 5 for $& an for $A. 'or !ega2509 use pin 5 for $& an pin A5 for $A. After po8ering on the Aruino9 p"ace the motion sensor on a surface that is as "eve" as possi)"e9 then press an ho" )utton 1 on the mu"ti#function shie". 1 )"inks 8hi"e the sensor is ca"i)rate. ;hereafter9 )y p"acing the motion sensor on inc"ine surfaces the shie" 8i"" isp"ay the ang"e of inc"ination. *resent"y the inc"ine is isp"aye on"y for a sing"e a@is9 )ut the app"ication cou" moifie to the sho8 the inc"ine for an aitiona" a@is. #deine :SOFTDI:E #include #include #include #include
*age 21 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
#include ASot3reDI.hA #include ADI.hA #include AM;962.hA #deine SOFTW@+:DI void clirte()% M;962 M;9% const lot rdTo=e- 7 "Q. / 1."0"256212Q5511Q066011Q152% int Oset7 yOset7% lot $ScleOset 7 "% // multiply P is 3ith this vlue to -et s close to "s possile.
void setup() { // put your setup code here to run once! Timer".initili$e()% #i deined (SOFTW@+:DI) // 9se sot3re DI // 9no nd eonrdo use pin 2 or SI nd 6 or S=@. Me-26 use pin 2 or SI nd pin @2 or S=@. SotDI".initili$e()% M;9.initili$e(&SotDI" M;9:=+F@9T:@==+SS << " @II+:FS: ?O:FS:2 =;F:8W:2)% #else // 9se hrd3re DI Wire.e-in()% DI".initili$e(&Wire)% M;9.initili$e(&DI" M;9:=+F@9T:@==+SS @II+:FS: ?O:FS:2 =;F:8W:2)% #endi MFS.initili$e(&Timer")% 4 void loop() { // put your min code here to run repetedly! yte tn 7 MFS.-et8utton()% i (tn 77 89TTO*:":O*?:;+SS+=) { clirte()% 4 M;9.-et@ccel3()% M;9.ccel::3 '7 Oset% M;9.ccel::3 '7 yOset% lot n-le% i (M;9.ccel:P:3 77 ) { n-le 7 5% 4 else { n-le 7 tn((lot)M;9.ccel::3 / (M;9.ccel:P:3 R $ScleOset)) R rdTo=e-% // clculte or y is //n-le 7 tn((lot)M;9.ccel::3 / (M;9.ccel:P:3 R $ScleOset)) R rdTo=e-% // clculte or is 4 MFS.3rite(n-le ")% dely()% 4
*age 22 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
void clirte() { MFS.3rite(A A)% MFS.3riteeds(+=:" O*)% MFS.lin,eds(+=:" O*)% // discrd irst e3 sensor redin-s. or (int i7% i<"% iBB) { M;9.-et@ccel3()% dely(")% 4 int Llues2U yLlues2U $Llues2U% or (int i7% i<2% iBB) { M;9.-et@ccel3()% LluesiU 7 M;9.ccel::3% yLluesiU 7 M;9.ccel::3% $LluesiU 7 M;9.ccel:P:3% dely(1)% 4 Oset 7 MedinO2(LluesU Llues"U LluesU Llues1U Llues0U)% yOset 7 MedinO2(yLluesU yLlues"U yLluesU yLlues1U yLlues0U)% $ScleOset 7 (lot)M;9.ccelScleLlue / MedinO2($LluesU $Llues"U $LluesU $Llues1U $Llues0U)% MFS.lin,eds(+=:" OFF)% // cler the input utton uer y redin- it nd discrdin- vlue. or (int i7% i<"% iBB) { MFS.-et8utton()% 4 4
$onar ranger ;he sonar ranger app"ication uses the H& $406 sonar mou"e to measure istance )et8een the mou"e an a so"i o):ect up to 5 meters a8ay. ;his app"ication 8orks in a 8ay simi"ar to the o)stac"e sensor of some vehic"es the assist the river in parking manoeuvres. As an o)stac"e nears the sonar mou"e9 the )eeper is soune at shorter an shorter interva"s. ;he shie"-s )utton 1 is use for engaging or isengaging the sonar mou"e. ;he trigger an echo pins of the sonar mou"e are connecte to Aruino pins 5 an respective"y9 8hich are e@pose on the mu"ti#function shie". After po8ering on the Aruino9 p"ace a so"i o):ect at ifferent istances a8ay from the sonar mou"e. #include #include #include const int Tri-;in 7 2% const int +cho;in 7 6% enum sonrModeLlues { MO=+:SO*@:OFF MO=+:SO*@:O* 4% yte sonrMode 7 MO=+:SO*@:OFF% void setup()
*age 2( of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
{ //Seril.e-in(56)% pinMode(Tri-;in O9T;9T)% pinMode(+cho;in D*;9T)% Timer".initili$e()% MFS.initili$e(&Timer")% MFS.3rite(AoA)% 4 void loop() { yte tn 7 MFS.-et8utton()% s3itch (sonrMode) { cse MO=+:SO*@:OFF! i (tn 77 89TTO*:":;+SS+=) { sonrMode 7 MO=+:SO*@:O*% MFS.eep(2 52 ")% MFS.3rite(AonA)% 4 re,% cse MO=+:SO*@:O*! i (tn 77 89TTO*:":;+SS+=) { sonrMode 7 MO=+:SO*@:OFF% MFS.eep()% MFS.3rite(AoA)% MFS.lin,=isply(=D?DT:@ OFF)% MFS.initSonr()% 4 else { int distnce 7 MFS.-etSonr=tIm(Tri-;in +cho;in)%
i (distnce G7 && distnce < ) { int o;eriod 7 distnce ' 6% i (o;eriod < ) { o;eriod 7 % 4 MFS.3rite(distnce)% MFS.set8eepO;eriod(o;eriod)%
MFS.lin,=isply(=D?DT:@ distnce < "")% 4 dely(")%
4 re,%
4 4
$peedometer ;he speeometer app"ication ca"cu"ates the spee of a 8hee" Cin ki"ometres+hourD )y using a magnet an a ree s8itch9 8hich is connecte to Aruino pin 5. %t shou" a"so )e possi)"e to fa)ricate your o8n 8hee" encoer using a "ine or mark sensor. After po8ering on the Aruino9 press an ho" )utton 1 of the mu"ti#function shie" unti" the isp"ay )"inks9 then use )uttons 2 an ( to set the 8hee" iameter in centimetres. *ress )utton 1 again 8hen finishe. ;urn the 8hee" to see the spee inicate on the shie" isp"ay.
*age 26 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
A possi)"e enhancement for this app"ication is to keep a recor of trip istance in ki"ometres. #include #include #include
enum SpeedoModeLlues { S+T9;:WE++ I@I9@T+:S;++= 4% yte speedoMode 7 I@I9@T+:S;++=% yte 3heel=imeterIm 7 6% unsi-ned int 3heelIirmcumerenceIm 7 (3heel=imeterIm R 1"0) / "% lot Speedmh (unsi-ned int 3heelIircumerenceIm unsi-ned int periodMs)%
void setup() { // put your setup code here to run once! pinMode(2 D*;9T:;99;)% Timer".initili$e()% MFS.initili$e(&Timer")% MFS.init;ulseDnIounter( 2 // use di-itl pin 2 or pulse input. // the numer o milliseconds to 3it or pulse eore resettin- pulse in period to . OW // tri--er pulse on OW input. )% 4
void loop() { // put your min code here to run repetedly! yte tn 7 MFS.-et8utton()% s3itch (speedoMode) { cse S+T9;:WE++! i (tn 77 89TTO*:":;+SS+=) { speedoMode 7 I@I9@T+:S;++=% MFS.lin,=isply(=D?DT:@ OFF)% 3heelIirmcumerenceIm 7 (3heel=imeterIm R 1"0) / "% 4 else i (tn 77 89TTO*::;+SS+= HH tn 77 89TTO*::O*?:;+SS+=) { 3heel=imeterIm''%
i (3heel=imeterIm < 1) { 3heel=imeterIm 7 1% 4 MFS.3rite(3heel=imeterIm)% 4 else i (tn 77 89TTO*:1:;+SS+= HH tn 77 89TTO*:1:O*?:;+SS+=) { 3heel=imeterImBB% i (3heel=imeterIm > ") { 3heel=imeterIm 7 "% 4
*age 25 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
MFS.3rite(3heel=imeterIm)% 4 re,% cse I@I9@T+:S;++=! i (tn 77 89TTO*:":O*?:;+SS+=) { speedoMode 7 S+T9;:WE++% MFS.3rite(3heel=imeterIm)% MFS.lin,=isply(=D?DT:@ O*)% 4 else { unsi-ned int pulse;eriodMs 7 MFS.-et;ulseDn;eriod()% i (pulse;eriodMs 77 ) { MFS.3rite(. ")% 4 else { MFS.3rite(Speedmh(3heelIirmcumerenceIm pulse;eriodMs) ")% 4 4 re,%
4 dely(")% 4
lot Speedmh (unsi-ned int 3heelIircumerenceIm unsi-ned int periodMs) { return (lot)(3heelIircumerenceIm R 16) / periodMs% 4
A"" the coe samp"es an app"ications have )een teste an 8ork. %f you e@perience any ifficu"ties9 p"ease post a comment9 an %-"" get )ack to you as soon as % can.
*age 2 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
!ppendices Multi-function $hield Library #elp #deine O* " #deine OFF #deine #deine #deine #deine #deine #deine #deine #deine #deine #deine #deine #deine #deine
+=:":;D* +=::;D* +=:1:;D* +=:0:;D* ;OT:;D* 8++;+:;D* 89TTO*:":;D* 89TTO*::;D* 89TTO*:1:;D* @TIE:;D* I:;D* =@T@:;D* M12:;D*
#deine #deine #deine #deine #deine
=D?DT:" " =D?DT: =D?DT:1 0 =D?DT:0 Q =D?DT:@ "2
#deine #deine #deine #deine #deine
+=:" " +=: +=:1 0 +=:0 Q +=:@ "2
"1 " "" " 1 @" @ @1 0 Q @0
// utton stte indictors #deine 89TTO*:;+SS+=:D*= #deine 89TTO*:SEOT:++@S+:D*= #deine 89TTO*:O*?:;+SS+=:D*= #deine 89TTO*:O*?:++@S+:D*=
( (" ( (1
<< << << <<
6) 6) 6) 6)
#deine #deine #deine #deine
89TTO*:":;+SS+= 89TTO*:":SEOT:++@S+ 89TTO*:":O*?:;+SS+= 89TTO*:":O*?:++@S+
(" (" (" ("
H H H H
89TTO*:;+SS+=:D*=) 89TTO*:SEOT:++@S+:D*=) 89TTO*:O*?:;+SS+=:D*=) 89TTO*:O*?:++@S+:D*=)
#deine #deine #deine #deine
89TTO*::;+SS+= 89TTO*::SEOT:++@S+ 89TTO*::O*?:;+SS+= 89TTO*::O*?:++@S+
( ( ( (
H H H H
89TTO*:;+SS+=:D*=) 89TTO*:SEOT:++@S+:D*=) 89TTO*:O*?:;+SS+=:D*=) 89TTO*:O*?:++@S+:D*=)
#deine #deine #deine #deine
89TTO*:1:;+SS+= 89TTO*:1:SEOT:++@S+ 89TTO*:1:O*?:;+SS+= 89TTO*:1:O*?:++@S+
(1 (1 (1 (1
H H H H
89TTO*:;+SS+=:D*=) 89TTO*:SEOT:++@S+:D*=) 89TTO*:O*?:;+SS+=:D*=) 89TTO*:O*?:++@S+:D*=)
#deine SMOOTED*?:*O*+ #deine SMOOTED*?:MO=+@T+ #deine SMOOTED*?:STO*?
"
clss MultiFuncShield { pulic! // ;ointer to user interrupt 3ith reXuency o ",h$. void (RuserDnterrupt)() 7 *9% // Dnitili$es this instnce usin- TimerOne instnce. @ ",h$ interrupt is ttched. void initili$e(TimerOne Rtimer")%
*age 23 of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
// Dnitili$es this instnce ut interrupt sed etures re not ville. void initili$e()% // For internl use only. void isrIll8c,()% // Dnitites millisecond countdo3n timer. void setTimer (unsi-ned lon- thousndths)% // ?ets the current vlue o the countdo3n timer. unsi-ned lon- -etTimer()% // Dnitites nd 3its or millisecond countdo3n timer to rech . void 3it(unsi-ned lon- thousndths)% // Writes to the += di-it disply. void 3rite(const chr Rtetstrin- yte ri-htKustiy 7)% void 3rite(int inte-er)% void 3rite(lot numer yte deciml;lces 7 ")% // Mnully rereshes the ed di-it disply. // *ot to e used 3hilst interrupt sed etures re ville. void mnul=isplyeresh()% // 8lin,s the di-its on the += di-it disply. void lin,=isply(yte di-its // use it3ise O e.-. =D?DT:" H =D?DT: yte enled 7 O* // turns on/o the lin,in)% // Turns +=s on or o. void 3riteeds(yte leds yte lit )%
// use it3ise O e.-. +=:" H +=: // O* or OFF
// 8lin,s the +=s. void lin,eds(yte leds yte enled 7 O* )%
// use it3ise O e.-. +=:" H +=: // O* or OFF
// +n--e the eeper 3hich is mn-ed in the c,-round. ;eriod timin- is in "th o second void eep(unsi-ned int on;eriod 7 unsi-ned int o;eriod 7 yte cycles 7 " unsi-ned int loopIycles 7 " /R 7indeinitely R/ unsi-ned int loop=ely;eriod 7)% // 9se this to set the o period 3hilst the eeper is en--ed void set8eepO;eriod(unsi-ned int o;eriod)% // Yueues utton ction to the utton Xueue e.- 89TTO*:":;+SS+= void Xueue8utton (yte utton)% // ;ulls utton ction rom the utton Xueue. yte -et8utton()% // Yueues utton short press nd relese ctions. on- utton presses re not supported nd lon- releses re reported s short releses. // Should not e used 3hilst interrupt sed etures re ville. void mnul8uttonEndler()% // Dnitili$es the pulse counter. 9sed or countin- pulses M pulse reXuency 2h$. void init;ulseDnIounter(yte pin 7 89TTO*:":;D* // unsi-ned int timeOut 7 1 // to 3it or pulse eore resettin- pulse in period to . yte tri--er 7 OW // risin- or llin- ed-e )%
pplied to n input pin. input pin the numer o milliseconds tri--er counter on either
// ?ets the period o the most recent pulse (in milliseconds). unsi-ned int -et;ulseDn;eriod()%
*age 2< of (0
Hackatronics – Using an Aruino !u"ti#function $hie"
cohesivecomputing.co.uk
// ?ets the totl numer pulses counted. unsi-ned lon- -et;ulseDnTotlIount()% // esets the pulse counter to . void reset;ulseDnTotlIount()% // Sets the pulse in timeout 3hich is the numer o milliseconds to 3it or pulse eore resettin- pulse in period to . void set;ulseDnTimeOut(unsi-ned int timeOut)% // Dnitili$es the sonr redin- eture. *eeds EI'S0 sonr module. void initSonr(yte level 7 SMOOTED*?:MO=+@T+)% // level 7none "7moderte 7stron-. // ?ets the distnce mesured in centimeters usin- EI'S0 sonr module. unsi-ned int -etSonr=tIm(yte tri--er;in yte echo;in)% // Dnitili$es temperture redin- eture. *eeds M12 sensor. Must remove Jumper K" rom shield. void initM12(yte level 7 SMOOTED*?:MO=+@T+)% // level 7none "7moderte 7stron// ?ets the temperture redin- in " tenths o centi-rde. int -etM12=t()% 4% etern MultiFuncShield MFS%
*age 2 of (0