Handouts - DAY1

  • Published on
    27-Dec-2015

  • View
    6

  • Download
    0

Embed Size (px)

DESCRIPTION

arduino programming

Transcript

  • THINKLAB WORKSHOP

    DAY 1 Arduino Introduction and Fundamentals 1

  • Outline

    INTRODUCTION

    GETTING STARTED WITH ARDUINO

    INTERFACING FUNDAMENTALS

    SERIAL COMMUNICATIONS

    2

  • INTRODUCTION

    3

  • Microcontroller

    One-chip solution

    Highly integrated chip that includes all or most of

    the parts needed for a controller:

    CPU

    RAM

    ROM

    I/O Ports

    Timer

    Interrupt Control

    4

  • What is Arduino?

    Open-source physical computing platform based on

    a simple microcontroller board and a development

    environment for writing software for the board

    The Gizduino board is an Arduino clone based on

    Arduino Diecimila, which is based on the

    ATmega168 microprocessor

    Arduino comes with its own open-source Integrated

    Development Environment (IDE)

    5

  • Specifications

    Microprocessor: ATmega168

    Operating voltage: 5V

    Input voltage (recommended): 7-12V

    Input voltage (limits): 6-20V

    Digital I/O Pins: 14 (6 provides PWM)

    Analog Input Pins: 6

    DC Current per pin: 40mA

    Flash Memory: 16kB (2kB used by bootloader)

    6

  • Why Arduino?

    Inexpensive

    Cross-platform

    Simple, clear programming environment

    Open-source and extensible software

    Open-source and extensible hardware

    7

  • GETTING STARTED

    8

  • Handling the Arduino

    Never plug in the USB cable and the DC adapter at

    the same time!

    Prevent the male pins from touching each other, as

    well as the leads, on the bottom of the board.

    Best practice is to hold the boards on its

    sides/edges.

    Another best practice is to unpower the board when

    adding or removing components or connecting or

    disconnecting modules.

    9

  • Connect Arduino to PC

    Connect the Gizduino board to your computer using

    the USB cable. The green power LED should turn on.

    10

  • Launching Arduino IDE

    Double click the Arduino application

    Open the HelloWorld example found on your

    Thinklab examples folder

    11

  • Setting-up Arduino IDE

    Tools > Board menu and select Gizduino (mini)

    12

  • Setting-up Arduino IDE

    Select the serial device of the Arduino board from

    the Tools > Serial Port menu

    Disconnect-reconnect the USB of the Arduino to find

    out which port to pick, or use the Device Manager

    13

  • Uploading the Program

    Click Upload to check the code and subsequently

    upload the sketch to your board

    If upload is successful, the message Done

    Uploading will appear in the status bar

    14

  • Uploading the Program

    Click the Serial Monitor button

    15

  • Uploading the Program

    Select 9600

    You should see

    Hello World! Printed

    If it does,

    congratulations!

    Youve gotten

    Arduino up and

    running! c:

    16

  • What is a Sketch?

    It is the unit of code that is uploaded to and run on

    an Arduino board

    Example: HelloWorld code uploaded earlier

    17

  • INTERFACING FUNDAMENTALS

    18

  • Bare Minimum

    There are two special functions that are part of

    every Arduino sketch:

    setup()

    A function that is called once, when the sketch starts

    Setting pin modes or initializing libraries are placed here

    loop()

    A function that is called over and over and is the heart of

    most sketches

    19

  • Grouping symbols

    ( )

    { }

    Arithmetic symbols

    +

    -

    *

    /

    %

    Punctuation symbols

    ;

    ,

    .

    Comparators

    =

    <

    >

    &&

    ||

    Syntax and Symbols

    20

  • Comments

    Statements ignored by the Arduino when it runs the

    sketch

    Used to give information about the sketch, and for

    people reading the code

    Multi-line comment: /* */

    Single-line comment: //

    21

  • Variables

    Place for storing a piece of data

    Has a name, value, and type

    int, char, float

    Variable names may not start with a numerical

    character

    Example:

    int iamVariable = 9;

    char storeChars;

    float saveDecimal;

    22

  • Variables

    You may now refer to this variable by its name, at

    which point its value will be looked up and used

    Example:

    In a statement: Serial.println(105);

    Declaring: int storeNum = 105;

    We can instead write: Serial.println(storeNum);

    23

  • Variables

    Variable Scope

    Global recognized anywhere in the sketch

    Local recognized in a certain function only

    Variable Qualifiers

    CONST make assigned variable value unchangeable

    STATIC ensure variable will only and always be

    manipulated by a certain function call

    VOLATILE load variable directly from RAM to prevent

    inaccuracy due to access beyond the control of the main

    code

    24

  • Variables

    Types

    INT

    UNSIGNED INT

    LONG

    UNSIGNED LONG

    FLOAT

    CHAR

    BYTE

    BOOLEAN

    25

  • Variables

    Type-cast Conversion on-the-fly conversion of

    variables from its default type to another whilst not

    changing its original declared type

    Syntax:

    variable_type(value)

    Example

    int(N)

    float(100)

    26

  • Variables

    Array a collection of variables of the same type

    accessed via a numerical index

    It is a series of variable instances placed adjacent

    in memory, and labeled sequentially with the index

    myArray[0]

    myArray[1]

    myArray[2]

    myArray[3]

    myArray[4]

    int myArray[5]

    27

  • Control Structures

    Series of program instructions that handle data and

    execute commands, exhibiting control

    DECISIVE exhibits control by decision making

    RECURSIVE exhibits control by continuous

    execution of commands

    Control element manifests in the form of

    CONDITIONS

    28

  • Control Structures

    Syntax:

    if (condition) {

    // do something here

    }

    else {

    // do something here

    }

    If-Else Statement

    It allows you to

    make something

    happen or not

    depending on

    whether a given

    condition is true or

    not.

    29

  • Control Structures

    void setup() {

    Serial.begin(9600);

    int test = 0;

    if (test == 1) {

    Serial.print(Success);

    }

    else {

    Serial.print(Fail);

    }

    }

    void loop() { } 30

  • Control Structures

    Replace setup() code with this to show branching

    Serial.print(Score = );

    Serial.println(test);

    if (test == 100) Serial.print(Perfect! Magaling!);

    else if (test >= 90) Serial.print(Congrats! So close~);

    else if (test >= 85) Serial.print(Pwedeeee);

    else if (test >= 80) Serial.print(More effort);

    else if (test >= 75) Serial.print(Study harder!);

    else Serial.print(Nako summer na yan tsk tsk); 31

  • Control Structures

    Syntax:

    switch (var) {

    case val01:

    // do something when var = val01

    break;

    case val02:

    // do something when var = val02

    break;

    default:

    // if no match, do default

    }

    Switch-Case Statement

    Depends on whether

    the value of a

    specified variable

    matches the values

    specified in case

    statements.

    32

  • Control Structures

    void setup() {

    Serial.begin(9600);

    int test = 0;

    switch (test) {

    case 1:

    Serial.print(Success);

    break;

    case 0:

    Serial.print(Fail);

    break;

    }

    }

    void loop() { }

    33

  • Control Structures

    While Loop

    Continue program until a given condition is true

    Syntax:

    while (condition) {

    // do something here until condition

    becomes false

    }

    34

  • Control Structures

    void setup() {

    Serial.begin(9600);

    int count = 0;

    while (count

  • Control Structures

    Do-While Loop

    Same as While loop however this structure allows

    the loop to run once before checking the condition

    Syntax:

    do {

    // do something here until condition

    becomes false

    } while (condition) ;

    36

  • Control Structures

    void setup() {

    Serial.begin(9600);

    int count = 0;

    do {

    Serial.println(Hello);

    count++;

    } while (count

  • Control Structures

    For Loop

    Distinguished by a increment/decrement counter

    for termination

    Syntax: