61
THINKLAB WORKSHOP DAY 1 – Arduino Introduction and Fundamentals 1

Handouts - DAY1

Embed Size (px)

DESCRIPTION

arduino programming

Citation preview

Page 1: Handouts - DAY1

THINKLAB WORKSHOP

DAY 1 – Arduino Introduction and Fundamentals 1

Page 2: Handouts - DAY1

Outline

INTRODUCTION

GETTING STARTED WITH ARDUINO

INTERFACING FUNDAMENTALS

SERIAL COMMUNICATIONS

2

Page 3: Handouts - DAY1

INTRODUCTION

3

Page 4: Handouts - DAY1

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

Page 5: Handouts - DAY1

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

Page 6: Handouts - DAY1

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

Page 7: Handouts - DAY1

Why Arduino?

Inexpensive

Cross-platform

Simple, clear programming environment

Open-source and extensible software

Open-source and extensible hardware

7

Page 8: Handouts - DAY1

GETTING STARTED

8

Page 9: Handouts - DAY1

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

Page 10: Handouts - DAY1

Connect Arduino to PC

Connect the Gizduino board to your computer using

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

10

Page 11: Handouts - DAY1

Launching Arduino IDE

Double click the Arduino application

Open the HelloWorld example found on your

Thinklab examples folder

11

Page 12: Handouts - DAY1

Setting-up Arduino IDE

Tools > Board menu and select Gizduino (mini)

12

Page 13: Handouts - DAY1

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

Page 14: Handouts - DAY1

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

Page 15: Handouts - DAY1

Uploading the Program

Click the Serial Monitor button

15

Page 16: Handouts - DAY1

Uploading the Program

Select 9600

You should see

Hello World! Printed

If it does,

congratulations!

You’ve gotten

Arduino up and

running! c:

16

Page 17: Handouts - DAY1

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

Page 18: Handouts - DAY1

INTERFACING FUNDAMENTALS

18

Page 19: Handouts - DAY1

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

Page 20: Handouts - DAY1

Grouping symbols

( )

{ }

“ “

Arithmetic symbols

+

-

*

/

%

Punctuation symbols

;

,

.

Comparators

=

<

>

&&

||

Syntax and Symbols

20

Page 21: Handouts - DAY1

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: /* <statement> */

Single-line comment: //

21

Page 22: Handouts - DAY1

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

Page 23: Handouts - DAY1

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

Page 24: Handouts - DAY1

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

Page 25: Handouts - DAY1

Variables

Types

INT

UNSIGNED INT

LONG

UNSIGNED LONG

FLOAT

CHAR

BYTE

BOOLEAN

25

Page 26: Handouts - DAY1

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

Page 27: Handouts - DAY1

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

Page 28: Handouts - DAY1

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

Page 29: Handouts - DAY1

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

Page 30: Handouts - DAY1

Control Structures

void setup() {

Serial.begin(9600);

int test = 0;

if (test == 1) {

Serial.print(“Success”);

}

else {

Serial.print(“Fail”);

}

}

void loop() { } 30

Page 31: Handouts - DAY1

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

Page 32: Handouts - DAY1

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

Page 33: Handouts - DAY1

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

Page 34: Handouts - DAY1

Control Structures

While Loop

Continue program until a given condition is true

Syntax:

while (condition) {

// do something here until condition

becomes false

}

34

Page 35: Handouts - DAY1

Control Structures

void setup() {

Serial.begin(9600);

int count = 0;

while (count <= 2) {

Serial.println(“Hello”);

count++;

}

}

void loop() { } 35

Page 36: Handouts - DAY1

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

Page 37: Handouts - DAY1

Control Structures

void setup() {

Serial.begin(9600);

int count = 0;

do {

Serial.println(“Hello”);

count++;

} while (count <= 2);

}

void loop() { } 37

Page 38: Handouts - DAY1

Control Structures

For Loop

Distinguished by a increment/decrement counter

for termination

Syntax:

for (start value; condition; operation) {

// do something until condition becomes

false

}

38

Page 39: Handouts - DAY1

Control Structures

void setup() {

Serial.begin(9600);

int count = 0;

for (count = 0; count <=5; count++) {

Serial.println(“Hello”);

}

}

void loop() { }

39

Page 40: Handouts - DAY1

Control Structures

break;

Used to terminate a loop regardless of the state

of the condition

Required in Switch-Case statements as an exit

command

40

Page 41: Handouts - DAY1

Control Structures

continue;

Used to skip loop iterations, bypassing the

command(s) set

Note that the condition handle of the loop is still

checked when using this command

41

Page 42: Handouts - DAY1

Logic

Conditional AND

Symbolized by &&

Used when all conditions need to be satisfied.

Conditional OR

Symbolized by ||

Used when either of the conditions need to be

satisfied.

42

Page 43: Handouts - DAY1

Logic

Conditional NOT

Symbolized by !

Appends to other statements to invert its state

43

Page 44: Handouts - DAY1

Logic

Serial.begin(9600);

int gupit = 0;

int ahit = 0;

if ((gupit == 1) && (ahit == 1))

Serial.println("Gwapong-gwapo! Pwede nang artista!");

else if ((gupit == 1) || (ahit == 1))

Serial.println("May kulang, pero pwede nang model");

else

Serial.println("Eeeew mukhang gusgusin!");

44

Page 45: Handouts - DAY1

Math Commands

min(x, y) – returns the SMALLER of two numbers x

and y

max(x, y) – returns the LARGER of two numbers x

and y

constrain(x, A, B) – constrain a number x between a

lower value A and a higher value B

map(x, A, B, C, D) – re-map a value x, whose

ORIGINAL RANGE runs from A to B, to a NEW

RANGE running from C to D 45

Page 46: Handouts - DAY1

Math Commands

abs(x) – returns the ABSOLUTE VALUE of a

number x

pow(x, y) – exponential function, returns x^y

sqrt(x) – returns the SQUARE ROOT of a

number x

46

Page 47: Handouts - DAY1

Timing Controls

delay()

Halts the sequence of execution during the time

specified

Syntax:

delay(milliseconds);

milliseconds = the time value in milliseconds

47

Page 48: Handouts - DAY1

Timing Controls

millis()

Gives the number of milliseconds since the

Arduino board began running the uploaded

sketch

Syntax:

variable = millis();

48

Page 49: Handouts - DAY1

Random Numbers

random()

Generates pseudo-random numbers

Syntax:

random(min, max);

min = lower bound of random value, inclusive

(optional)

max = upper bound of random value, exclusive

49

Page 50: Handouts - DAY1

Random Numbers

randomSeed()

Varies the sequence of numbers at every start-up

Syntax:

randomSeed(source);

50

Page 51: Handouts - DAY1

Functions

Modular pieces of code that perform a defined

task outside of the main program

Very useful to reduce

repetitive lines of code

due to multiple execution

of commands

Has a name, type, and

value – same as variables?

51

Page 52: Handouts - DAY1

Functions

52

Page 53: Handouts - DAY1

SERIAL COMMUNICATIONS

53

Page 54: Handouts - DAY1

Initializing Serial

Serial.begin()

Initialize serial communication

Comonly placed at the setup function of your sketch

Syntax:

Serial.begin(baud)

baud: serial baud rate value to be used

e.g. 300, 1200, 2400, 4800, 9600, 14400, 28800,

38400, 57600, 115200)

Ex. Serial.begin(9600);

54

Page 55: Handouts - DAY1

Printing Lines using Serial

Serial.println()

Prints data to the Serial port as ASCII text

Syntax:

Serial.println(val)

val: the value to print, which can be any data type

Ex. Serial.println(“Hello World!”);

Displays Hello World! In Serial Monitor

55

Page 56: Handouts - DAY1

void setup() {

Serial.begin(9600);

}

void loop() {

Serial.println(“Hello World!”);

delay(1000);

}

Sample Code

56

Page 57: Handouts - DAY1

Available Data from Serial

Serial.available()

Checks if there is data present on the line and returns

the number of bytes to be read

Syntax:

Serial.available()

Ex. if (Serial.available() > 0) { …

57

Page 58: Handouts - DAY1

Reading from Serial

Serial.read()

Returns the first byte of incoming serial data

Syntax:

variable = Serial.read();

variable can be either an int or a char

58

Page 59: Handouts - DAY1

void setup() {

Serial.begin(9600);

}

void loop() {

if (Serial.available() > 0) {

ReceivedByte = Serial.read();

Serial.print(ReceivedByte);

delay(10);

}

}

Sample Code

59

Page 60: Handouts - DAY1

SEE YOU IN DAY 2!

60

Page 61: Handouts - DAY1

CONTACT DETAILS

Landline: (02) 861-1531

Email: [email protected]

FB account: facebook.com/thinklab.secretariat

FB page: facebook.com/thinklab.ph

THANK YOU FOR

COMING!

61