arduino(workshop - hong kong polytechnic university · what(is(arduino?(•...

26
Arduino Workshop 1

Upload: others

Post on 06-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Arduino  Workshop  

1

Page 2: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Part  1  Introduc4on

2

Page 3: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

What  is  Arduino?  •  Open-­‐source  prototyping  pla2orm  

•  Building  digital  devices  and  interac7ve  objects  that  sense,  control  physical  devices  

•  Hardware:  Arduino  I/O  board                    (microcontroller)  

•  SoEware:  Arduino  IDE              (Integrated  Development  Environment)  

•  Able  to  read  inputs  –  sensors,  buIon  etc.  and  turn  it  into  outputs  -­‐  ac7va7ng  a  motor,  turning  on  an  LED  

•  Can  run  independently  or  communicate  with  computer  

Page 4: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

4

Page 5: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Complete  the  Circuit Coding Compile  

&  upload

Create  your  own  

inven4on!

How  it  works?

5

Page 6: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Arduino  Leonardo

6

Page 7: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Structure  of  the  board

7

3  Input    pins

3  Output  pins

Page 8: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Digital  vs  Analog

8

Signal: •  Only  have  2  state  (discrete)            1  (5V)  or  0  (0V)                                    OR                HIGH  or  LOW

•  Varying  number  between  0V  to  5V

Func4onality: •  Digital  input  AND  output  (general  purpose  input/output  (GPIO)  pins)  

•  Mainly  to  read  analog  sensors  •  Analog  Input  AND  output  •  Can  be  used  as  GPIO  pins

Page 9: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Set  up  IDE   hIps://www.arduino.cc/en/Main/SoEware  

9

Page 10: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

10

Page 11: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

11

Page 12: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

12

Page 13: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Choose  the  correct  Arduino  board  seSng  &  Serial  Port

13

Page 14: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Part  2  Programming  Basics

14

Page 15: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Basic  structure

void  setup(){                                                    //  ini7aliza7on  (run  once  only)            some  statement  here…      }    void  loop(){      //  Main  program  (run  repeatedly)            some  statement  here…      }

15

Page 16: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Func4ons Func_type  func_Name  (parameters){        //  declara7on  

 some  statements  here……    }    

16

!!IMPORTANT!! {}  curly  braces                ;  semicolon              Upper  &  Lower  case  commands  

Missing  these  would  cause  compila7on  error!    

/*……..*/  block  comments              //  line  comments  

Page 17: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

17

•   pinMode(pin,  mode);                      //  Ini7alize  the  pin  mode  as  INPUT  or  OUTPUT  

•   digitalRead(pin);            //  Read  input  value  from  digital  pin  

•   digitalWrite(pin,  value);              //  Output  value  from  digital  pin  

•   analogRead(pin);                                //  Read  input  value  from  analog  pin  

•   analogWrite(pin,  value);              //  Output  value  from  analog  pin  

•   delay(value);                                    //  Pauses  the  program  for  7me  in  milliseconds

Basics  commands

Page 18: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Arithme4c  &  Compara4ves

y  =  y  +  3;  x  =  x  -­‐  7;  i  =  j  *  6;  r  =  r  /  5;  z  =  z  %  2;  //mod  x  ++;  //x  =  x  +  1  x  -­‐-­‐;      //x  =  x  -­‐  1    

18

x  ==  y;    //  x  is  equal  to  y  x  !=  y;    //  x  is  not  equal  to  y  x  <  y;      //  x  is  less  than  y  x  >  y;      //  x  is  greater  than  y  x  <=  y;  //  x  is  less  than  or  equal  to  y    x  >=  y;    //  x  is  greater  than  or  equal  to  y  

Page 19: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Logics TRUE  or  FALSE  

Logical  AND:    

               (x  >  0  &&  x  <  5)          //  True  only  if  both  condi7ons  are  true  Logical  OR:  

               (x  >  0  ||  y  >  0)              //  True  if  either  condi7on  is  true  Logical  NOT:  

               (!  x  >  0)                                    //  true  if  condi7on  is  false  

19

Page 20: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Part  3  Let’s  get  started  !

20

Page 21: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

If…..  else if  (Logic){  

 statements;  }else{  

 another  statements;  }    •  else  if

21

Page 22: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Example  1  (Fading  LED)

22

Power  &  Babery  &  Slide  Dimmer  at  a0  pin  Bargraph  at  d5  pin

Slide  Dimmer

Page 23: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

[File]    [Examples]  [03.Analog]    [Fading]

23

Page 24: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Understanding  the  code

24

Int  ledPin  =  5;        //  Declare  that  LED  is  at  Pin  D5  void  setup(){  }                  //  nothing  happens  in  setup  void  loop(){        //  This  will  act  as  the  inhale  for  the  PWM  value  star7ng  from  zero  to  a  max  value  of  255      for(int  fadeValue  =  0;  fadeValue  <=  255;  fadeValue  ++){                  analogWrite(ledPin,  fadeValue);                  delay(  (1034  -­‐  analogRead(A0))  /  10  );      //  wait  for  a  moment  before  moving  forward                  }        //  This  will  act  as  the  exhale  for  the  PWM  value  star7ng  from  255  to  a  min  value  of  0      for(int  fadeValue  =  255;  fadeValue  >=  0;  fadeValue  -­‐-­‐){                  analogWrite(ledPin,  fadeValue);                  delay(  (1034  -­‐  analogRead(A0))  /  10  );      //  wait  for  a  moment  before  moving  forward                  }  }  

Page 25: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Variables •  Naming  &  storing  number  value  for  later  use            e.g.  Int  ledPin  =  5;                        //  declared  a  variable  named  ledPin  and  assigned  the  value  5                              inputVariable  =  analogRead(2);                          //  set  variable  to  value  of  analog  pin  2  

•  Can  test  variables  to  see  if  it  meets  certain  condi7ons  (Logics)  

             e.g.  (inputVariable  <  100)                                                                          //  tests  variable  if  it  is  less  than  100  

•  Global  vs  Local  variables  –  Global:  can  be  used  and  seen  by  all  func7ons  &  statements  

–  Local:  defined  inside  a  func7on  or  loops,  only  used  inside  the  func7on  it  declared  

25

Page 26: Arduino(Workshop - Hong Kong Polytechnic University · What(is(Arduino?(• Open%source+prototyping+plaorm+ • Building+digital+devices+and+interac7ve+ objects+thatsense,+control+physical+devices+

Data  type •  int  (integers)  –   int    someVariable  =  1500;                  //  assigned  1500  integer  value  

•  long  (long  integers)    –   long    someVariable  =  90000;        //  assigned  90000  integer  value  

•  char  (character  value)  –   char  someChar  =  ‘A’;                                    //  assigned  leIer  A  value  

•  boolean  (true  or  false)  –   bool  condi7on  =  false;                              //  assigned  false  value  

26