arduino - mechatronics engineering...

87
Mechatronics (2) 4 th Year- Mechatronics Major Prof. Dr. Magdy M. Abdelhameed Course Code: MDP 454, Course Name: Mechatronics (2), Second Semester 2014 Prof. Dr. Magdy M. Abdelhameed Arduino

Upload: others

Post on 05-Feb-2020

20 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Course Code: MDP 454, Course Name: Mechatronics (2), Second Semester 2014

• Prof. Dr. Magdy M. Abdelhameed

Arduino

Page 2: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Microcontroller Platform

Okay… but what’s a Microcontroller?

Tiny, self-contained computers in an IC

Often contain peripherals

Different packages available

Vast array of size and power available

What is Arduino?

Page 3: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Students, hobbyist, artists & designers

“open source hardware”Open Source Physical Computing Platform

open sourcefree to inspect & modify

communitywiki, forums, tutorials

Why Arduino?

Page 4: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

InputSensors (buttons, light sensors, ultrasound, …)

Digital Input (Serial, SPI, I2C)

OutputLEDs & Lasers

LCD/OLED/Touch Displays

Speakers

Motors, Servos, Steppers

Control and CommunicationDrive other machinery

Directly or using a communication protocol

What can it do?

Page 5: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Sensors

Page 6: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Arduino models differ in design but all share some basicfunctionality

AVR MicrocontrollerAtmel AVR Mega168 or AVR Mega 328

Powered either directly or via USB power

CommunicationsSerial (older models)

USB (most models)

Connections to a USB interface (smaller models)

Pins for various functions

Arduino hardware

Page 7: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Pins 0-13 (GPIO pins)

Either input our output (default to input)•

Can output 40 mA•

Digital pins

Page 8: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Switching without Pull-up and pull-down resistors

Digital pins

Page 9: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Switching with Pull-up and pull-down resistors

Pull-upPull-down

Digital pins

Page 10: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

What happens if we Digital Read a pin that is not connected?•20K internal pull-up resistor•

Serial pins 0 (RX) and 1 (TX). Connected to FTDI USB-to-TTL•Pin 13 is different (input) (RES + LED)•Analogue pins A0-A5 can be used as digital pins 14-19 (GPIO)•Read up to 5V. Don’t fry your pin!•

Digital pins

Page 11: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

PWM used to create analogue output on digital pins•

Simulate voltage between 5V and 0V using square waves•(signal switching between on and off)

Pins 3, 5, 6, 9, 10, and 11•

Pulse Width Modulation (PWM)

Page 12: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

••(signal switching between on and off)

Pins A0-A5

10 bit resolution (from 0-5V to int 0-1023)

100 microseconds (0.0001 s) to read an analog input,

maximum reading rate is about 10,000 times a second.

Analogue pins

Page 13: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

ICSP (In Circuit Serial Programming) directly•program AVR micro-controllers

I2C / TWI (Two-wire interface) communication•Inter IC control•Arduino analog input A4 = I2C SDA (serial data line)•Arduino analog input A5 = I2C SCL (serial clock line)•

External interrupts•Event-handling without polling•Interrupt 0 (on digital pin 2) and 1 (on digital pin 3)•

Analogue reference• (Set analogue reference for ADC)

Other pins

Page 14: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Arduino

Environment

Page 15: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

The Arduino Environment

Page 16: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Board Type

Page 17: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Serial (COM) port

Page 18: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

The Environment

Page 19: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Parts of the code

Page 20: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

• Comments can be anywhere

Comments

Page 21: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

• Comments can be anywhere

• Comments created with // or /*

and */

Comments

Page 22: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

• Comments can be anywhere

• Comments created with // or /*

and */

• Comments do not affect code

Comments

Page 23: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

• Comments can be anywhere

• Comments created with // or /*

and */

• Comments do not affect code

• You may not need comments,

but think about the community!

Comments

Page 24: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

The equals sign

= is used to assign a value

== is used to compare values

Operators

Page 25: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

And & Or

&& is “and”

|| is “or”

Operators

Page 26: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

–Basic variable types:

–Boolean

–Integer

–Character

–String

Variables

Page 27: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

–Boolean: boolean variableName;

Declaring variables

Page 28: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

–Boolean: boolean variableName;

–Integer: int variableName;

Declaring variables

Page 29: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

–Boolean: boolean variableName;

–Integer: int variableName;

–Character: char variableName;

Declaring variables

Page 30: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

–Boolean: boolean variableName;

–Integer: int variableName;

–Character: char variableName;

–String: stringName [ ];

Declaring variables

Page 31: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

–Boolean: variableName = true;

–or variableName = false;

Assigning variables

Page 32: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

–Boolean: variableName = true;

–or variableName = false;

–Integer: variableName = 32767;

–or variableName = -32768;

Assigning variables

Page 33: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

–Boolean: variableName = true;

–or variableName = false;

–Integer: variableName = 32767;

–or variableName = -32768;

–Character: variableName = „A‟;

–or stringName = “SparkFun”;

Assigning variables

Page 34: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Variable Scope

Page 35: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

void setup ( ) { }

• The setup function comes before

• the loop function and is necessary

• for all Arduino sketches

Setup

Page 36: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

void setup ( ) { }

• The setup header will never change,

• everything else that occurs in setup

• happens inside the curly brackets

Setup

Page 37: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

• Outputs are declare in setup, this is done by using the pinMode function

• This particular example declares digital pin # 13 as an output, remember to use CAPS

Setup

Page 38: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

• Serial communication also begins in setup

• This particular example declares Serial communication at a baud rate of 9600. More on Serial later...

Setup

Page 39: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

• You can also create internal pullup resistors in setup, to do so digitalWrite the pin HIGH

• This takes the place of the pullup resistors currently on your circuit 7 buttons

Setup

Page 40: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Setup, Interruptsvoid setup ( ) {

attachInterrupt (interrupt, function, mode) }

–You can designate an interrupt

function to Arduino pins # 2 and 3

–This is a way around the linear

processing of Arduino

Setup

Page 41: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Setup, Interruptsvoid setup ( ) {

attachInterrupt (interrupt, function, mode) }

–Interrupt: the number of the interrupt, 0 or 1, corresponding to Arduino pins # 2

and 3 respectively

–Function: the function to call when the interrupt occurs

–Mode: defines when the interrupt should be triggered

Setup

Page 42: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Setup, Interruptsvoid setup ( ) {

attachInterrupt (interrupt, function, mode) }

•LOW whenever pin state is low

•CHANGE whenever pin changes value

•RISING whenever pin goes from low to high

•FALLING whenever pin goes from low to high

–Don’t forget to CAPITALIZE

Setup

Page 43: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

if ( this is true ) { do this; }

If statements

Page 44: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

if ( this is true ) { do this; }

If statements

Page 45: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

if ( this is true ) { do this; }

If statements

Page 46: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

if ( this is true ) { do this; }If statements

Page 47: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

else { do this; }

Else

Page 48: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

• loop

• For

• while

Basic Repetition

Page 49: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

void loop ( ) { }Basic Repetition

Page 50: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

void loop ( ) { }Basic Repetition

Page 51: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

void loop ( ) { }

The “void” in the header is what the function will return (or spit out) when it happens, in this case it

returns nothing so it is void

Basic Repetition

Page 52: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

void loop ( ) { }

The “loop” in the header is what the function is called, sometimes you make the name up, sometimes (like loop) the function already

has a name

Basic Repetition

Page 53: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

void loop ( ) { }

The “( )” in the header is where you declare any variables that you are “passing” (or

sending) the function, the loop function is never “passed” any variables

Basic Repetition

Page 54: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

void loop ( ) { }Basic Repetition

Page 55: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

for (int count = 0; count<10; count++) { //for action code goes here//this could be anything

}

Basic Repetition

Page 56: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

for (int count = 0; count<10; count++){//for action code goes here}

Basic Repetition

Page 57: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

for (int count = 0; count<10; count++) {//for action code goes here}

Basic Repetition

Page 58: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

for (int count = 0; count<10; count++) {//for action code goes here}

Basic Repetition

Page 59: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

for (int count = 0; count<10; count++) {//for action code goes here}

Basic Repetition

Page 60: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

for (int count = 0; count<10; count++) {//for action code goes here}

Basic Repetition

Page 61: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

for (int count = 0; count<10; count++) {//for action code goes here}

Basic Repetition

Page 62: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

while ( count<10 ) {//while action code goes here}

Basic Repetition

Page 63: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

while ( count<10 ) {//while action code goes here//should include a way to change count//variable so the computer is not stuck//inside the while loop forever}

Basic Repetition

Page 64: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

while ( count<10 ) {//looks basically like a “for” loop //except the variable is declared before//and incremented inside the while //loop}

Basic Repetition

Page 65: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Or maybe:while ( digitalRead(buttonPin)==1 ) {//instead of changing a variable//you just read a pin so the computer//exits when you press a button//or a sensor is tripped}

Basic Repetition

Page 66: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Arduino

Programming

Structure

Page 67: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Declare variables at top

Initialize

setup() – run once at beginning, set pins

Running

loop() – run repeatedly, after setup()

Arduino “Software Layout”

Page 68: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

int ledPin = 13; – ledconnected to control pin 13

int aSensor = 0; – setupGlobal Variables

sensor 'aSensor' on analog pin 0

int statePin = LOW; – use thisto hold the state of a pin

Arduino “Software Layout”

Page 69: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

pinMode() – set a pin as inputor output

setup()

serial.Begin() – setup to `talk'to the computer

Arduino “Software Layout”

Page 70: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

pinMode(ledPin, Output); –set the pin `ledPin' as an output

setup()

serial.Begin(9600); – talk tothe computer at 9600 baud rate

Arduino “Software Layout”

Page 71: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

digitalWrite() – set a digital pin high/low

digitalRead() – read a digital pin’s state

analogRead() – read an analog pinloop()

analogWrite() – write an “analog” PWM value

delay() – wait an amount of time

millis() – get the current tim

Arduino “Software Layout”

Page 72: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

14 Digital IO (pins 0 - 13)

6 Analog In (pins 0 - 5)

6 Analog Out (pins 3, 5, 6, 9, 10, 11)

Input / Output

Page 73: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Digital – only has two values: on/off

Analog – has many (infinite) values

Computers don’t really do analog - usequantization instead

Digital? Analog?

Page 74: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Many states (Analog) or justtwo (HIGH/LOW - Digital)

Number of states (or “bins”) isresolution

Common computer resolutions:8-bit = 256 states

16-bit = 65,536 states

32-bit = 4,294,967,296 states

Digital? Analog?

Page 75: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Sensors ActuatorsLDR / IR

SolenoidSwitch /

Stepper MotorPotentiometer /Joystick / Piezo

Accelerometer Other CircuitsUltrasonic

Prototype shieldsIndicators

Special ICsLED / Lamps

Buzzers

I/O to/from What?

Page 76: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Install latest Arduino IDE from arduino.ccvoid setup(){ Run Arduino IDE

// start serial port at 9600 bps:Write the code on the left into the editor

Serial.begin(9600);Compile / Verify the code by clicking the play}

button

Before uploading your sketch, check the board andvoid loop()the serial port are correct for your Arduino and for

{ your computer

Serial.print("Hello World!\n\r");Menu -> Tools -> Board

// wait 2sec for next reading:Menu -> Tools -> Serial Portdelay(2000);

} Upload the code from the computer to theArduino using the upload button

Hello World!

Page 77: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Turn Led on and off according to the byte coming from the serial port, if a zero byte then the Led off else the Led on.

LED Blink

Page 78: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

/* Blinking LED ---* turns on and off a light emitting diode(LED) connected to a digital* pin, based on data coming over serial*/

int ledPin = 13; // LED connected to digital pin 13

int inByte = 0;

void setup()

{

pinMode(ledPin, OUTPUT); // sets the digital pin as output

Serial.begin(19200); // initiate serial communication

}

void loop()

{

while (Serial.available()>0) {

inByte = Serial.read();

}

if (inByte>0) {

digitalWrite(ledPin, HIGH); // sets the LED on

} else {

digitalWrite(ledPin, LOW); // sets the LED off

}

}

LED Blink

Page 79: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

/* Blinking LED ---* turns on and off a light emitting diode(LED) connected to a digital* pin, based on data coming over serial*/ Initialiseint ledPin = 13; // LED connected to digital pin 13

some of theint inByte = 0;

void setup() variables{

pinMode(ledPin, OUTPUT); // sets the digital pin as output

Serial.begin(19200); // initiate serial communication

}

void loop()

{

while (Serial.available()>0) {

inByte = Serial.read();

}

if (inByte>0) {

digitalWrite(ledPin, HIGH); // sets the LED on

} else {

digitalWrite(ledPin, LOW); // sets the LED off

}

}

LED Blink

Page 80: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

/* Blinking LED ---* turns on and off a light emitting diode(LED) connected to a digital* pin, based on data coming over serial*/

int ledPin = 13; // LED connected to digital pin 13

int inByte = 0;

void setup()

{

pinMode(ledPin, OUTPUT); // sets the digital pin as output

Serial.begin(19200); // initiate serial communication

}

void loop()

{ Setup LED pin andwhile (Serial.available()>0) {

inByte = Serial.read(); serial connection}

if (inByte>0) {

digitalWrite(ledPin, HIGH); // sets the LED on

} else {

digitalWrite(ledPin, LOW); // sets the LED off

}

}

LED Blink

Page 81: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

/* Blinking LED ---* turns on and off a light emitting diode(LED) connected to a digital* pin, based on data coming over serial*/

int ledPin = 13; // LED connected to digital pin 13

int inByte = 0;

void setup()

{

pinMode(ledPin, OUTPUT); // sets the digital pin as output

Serial.begin(19200); // initiate serial communication

} Loop - Reading thevoid loop() serial for info, when{

while (Serial.available()>0) {

inByte = Serial.read(); something is received}

if (inByte>0) {turn LED on LED onthe

digitalWrite(ledPin, HIGH); // sets the

} else {

digitalWrite(ledPin, LOW); // sets the LED off

}

}

LED Blink

Page 82: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Write Arduino code that uses to turn led on (on pin 13) when the button status is high (on pin 7) and led off when the button status is low; Also send this status via serial port with baud rate 19600.

Digital Input/ Output

Page 83: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

/* Digital reading, turns on and off a light emitting diode (LED) connected to digital* pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of* Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor.*/

int ledPin = 13; // choose the pin for the LED

int inPin = 7; // choose the input pin (button)

int buttonval = 0; // variable for reading the pin status

void setup() {

pinMode(ledPin, OUTPUT); // set LED as output

pinMode(inPin, INPUT); // set pushbutton as input

Serial.begin(19200); // start serial communication to computer

}

void loop() {

buttonval = digitalRead(inPin); // read the pin and get the button's state

if (buttonval == HIGH) { // check if the input is HIGH (button released)

digitalWrite(ledPin, LOW); // turn LED OFF

Serial.write('0'); // Button off (0) sent to computer

} else {

digitalWrite(ledPin, HIGH); // turn LED ON

Serial.write('1'); // Button on (1) sent to computer

}

}

Digital Input/ Output

Page 84: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

/* Digital reading, turns on and off a light emitting diode (LED) connected to digital* pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of* Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor.*/

Initialiseint ledPin = 13; // choose the pin for the LED

int inPin = 7; // choose the input pin (button) some of theint buttonval = 0; // variable for reading the pin status

variablesvoid setup() {

pinMode(ledPin, OUTPUT); // set LED as output

pinMode(inPin, INPUT); // set pushbutton as input

Serial.begin(19200); // start serial communication to computer

}

void loop() {

buttonval = digitalRead(inPin); // read the pin and get the button's state

if (buttonval == HIGH) { // check if the input is HIGH (button released)

digitalWrite(ledPin, LOW); // turn LED OFF

Serial.write('0'); // Button off (0) sent to computer

} else {

digitalWrite(ledPin, HIGH); // turn LED ON

Serial.write('1'); // Button on (1) sent to computer

}

}

Digital Input/ Output

Page 85: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

/* Digital reading, turns on and off a light emitting diode (LED) connected to digital* pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of* Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor.*/

int ledPin = 13; // choose the pin for the LED

Setup LED pin,

int inPin = 7; // choose the input pin (button)

int buttonval = 0; // variable for reading the pin status

switch pin and

void setup() {

serial connection

pinMode(ledPin, OUTPUT); // set LED as output

pinMode(inPin, INPUT); // set pushbutton as input

Serial.begin(19200); // start serial communication to computer

}

void loop() {

buttonval = digitalRead(inPin); // read the pin and get the button's state

if (buttonval == HIGH) { // check if the input is HIGH (button released)

digitalWrite(ledPin, LOW); // turn LED OFF

Serial.write('0'); // Button off (0) sent to computer

} else {

digitalWrite(ledPin, HIGH); // turn LED ON

Serial.write('1'); // Button on (1) sent to computer

}

}

Digital Input/ Output

Page 86: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

/* Digital reading, turns on and off a light emitting diode (LED) connected to digital* pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of* Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor.*/

Loop - Reading theint ledPin = 13; // choose the pin for the LED

int inPin = 7; // choose the input pin (button) button for info, when

int buttonval = 0; // variable for reading the pin status button is press turnvoid setup() {

pinMode(ledPin, OUTPUT); // set LED as output

pinMode(inPin, INPUT); // set pushbutton asthe LED on and input

Serial.begin(19200); // start serial communication to computer

signal the computer

}

void loop() {

Of the button's state

change

buttonval = digitalRead(inPin); // read the pin and get

if (buttonval == HIGH) { // check if the input is HIGH (button released)

digitalWrite(ledPin, LOW); // turn LED OFF

Serial.write('0'); // Button off (0) sent to computer

} else {

digitalWrite(ledPin, HIGH); // turn LED ON

Serial.write('1'); // Button on (1) sent to computer

}

}

Digital Input/ Output

Page 87: Arduino - Mechatronics Engineering Departmentmct.asu.edu.eg/uploads/1/4/0/8/14081679/lect_09-mechatronics_2_arduino.pdf · Vast array of size and power available What is Arduino?

Mechatronics (2)

4th Year- Mechatronics Major

Prof. Dr. Magdy M. Abdelhameed

Thank You For Your Attention!

Questions?