24
1 Technische Universität München Lect7-Page1 Lecture 7: Programming for the Arduino - The hardware - The programming environment - Binary world, from Assembler to C - Programming C for the Arduino: Basics - Programming C for the Arduino: more … - Programming style Technische Universität München Lect7-Page2 The hardware http://www.arduino.cc/

Lecture07Arduino_2SPP

Embed Size (px)

DESCRIPTION

hi

Citation preview

Page 1: Lecture07Arduino_2SPP

1

Technische Universität München

Lect7-Page1

Lecture 7: Programming for the Arduino

- The hardware- The programming environment- Binary world, from Assembler to C- Programming C for the Arduino: Basics- Programming C for the Arduino: more …- Programming style

Technische Universität München

Lect7-Page2

The hardware

http://www.arduino.cc/

Page 2: Lecture07Arduino_2SPP

2

Technische Universität München

Lect7-Page3

Lecture 7: Programming for the Arduino

- The hardware- The programming environment- Binary world, from Assembler to C- Programming C for the Arduino: Basics- Programming C for the Arduino: more …- Programming style

Technische Universität München

Lect7-Page4

Programming environmentDownload software and driver from:http://arduino.cc/en/Main/Software(MAC OSX FTDI-Driver:http://arduino.cc/en/Guide/MacOSXhttp://www.ftdichip.com/Drivers/VCP.htm)

Page 3: Lecture07Arduino_2SPP

3

Technische Universität München

Lect7-Page5

Lecture 7: Programming for the Arduino

- The hardware- The programming environment- Binary world, from Assembler to C- Programming C for the Arduino: Basics- Programming C for the Arduino: more …- Programming style

Technische Universität München

Lect7-Page6

Binary world, programming from Assembler to C

See: Rembold, Ulrich et. al.: Einführung in die Informatik für Naturwissenschaftler und Ingenieure. Hanser München Wien 1991See: http://privat.swol.de/SvenBandel/index.html, Download 01.09.2007See: http://en.wikipedia.org/wiki/Image:Boulier1.JPG, 01.09.2007

Programmable, mechanical calculation machines (19th/early 20th cent. AD)

Falcon(1728)

-> punchcard loomsfor work steps(program)

Joseph-Marie Jacquard

(1805)

+5V-

1

0

0

1

1

See: Rembold, Ulrich et. al.: Einführung in die Informatik für Naturwissenschaftler und Ingenieure. Hanser München Wien 1991

� Origins at China and also developed by the mathematician Leibniz (17th century AD)� Positional numeral system which represents each number just with 2 symbols, 0 and 1� These values can be represented by voltage levels in electronic circuits� For human use very inefficient but with electronic circuits it is possible to create very efficient arithmetic and logic units (ALUs)

for the basic operations addition, subtraction, multiplication and division

Page 4: Lecture07Arduino_2SPP

4

Technische Universität München

Lect7-Page7

The machine instructions

See: http://ivs.cs.uni-magdeburg.de/bs/lehre/sose99/bs1/seminare/assembler.shtml, Download 30.08.2007

Machine code (80x86): 000001011110100000000011

00000101Operation code for

„add to accumulator“

00000011 11101000 Binary representation of number

1000

low bytehigh byte

Assembler

Assembler mnemonic: add ax, 1000(short „Assembler“)

conversion

Binary world, programming from Assembler to C

Technische Universität München

Lect7-Page8

Programming paradigm of the problem oriented computer language C++/C

See: http://en.wikipedia.org/wiki/Programming_paradigm, Download 08.10.2007

Binary world, programming from Assembler to C

• Procedural programming with structured programming as subset• Code is splitted into several, reusable sections

called procedures or functions with own scopes, which can be called at given code positions

• Logical procedure units are combined to modules

• Jumps (like goto) are not allowed• E.g. C• Case sensitive

Page 5: Lecture07Arduino_2SPP

5

Technische Universität München

Lect7-Page9

From source code to machine instructions

See: http://ivs.cs.uni-magdeburg.de/bs/lehre/sose99/bs1/seminare/assembler.shtml, Download 30.08.2007

Machine code (e.g. 80x86, …)

Assembler/Disassembler

Assembler mnemonic(short „Assembler“)

conversion

Compiler translation andoptimisation

Source codein a specific computer language

The process of compilation is unidirectional. There are several representations of a solution for a specific task in problemoriented languages which results in the same representation in machine code. On the other hand the optimisation is not ideal, so that the knowledge about the results after comiplation can improve the runtime of a program.

Binary world, programming from Assembler to C

Technische Universität München

Lect7-Page10

Binary world, programming from Assembler to C

Compiler

Programming

Creation

Runtime

Preprocessor/Compilation

Linking

Executables

Sourcecodes

Sourcecodes

Objectcodes

Librarycodes

Executables

Load

Executablelibraries

Phase

Upload via USB

Page 6: Lecture07Arduino_2SPP

6

Technische Universität München

Lect7-Page11

Lecture 7: Programming for the Arduino

- The hardware- The programming environment- Binary world, from Assembler to C- Programming C for the Arduino: Basics- Programming C for the Arduino: more …- Programming style

Technische Universität München

Lect7-Page12

Programming C for the Arduino: Basics

Our first program: Blink (LED)

http://www.led-shop.com/images/LED_Anschluss.jpg

Page 7: Lecture07Arduino_2SPP

7

Technische Universität München

Lect7-Page13

Programming C for the Arduino: Basics

Memory int, long,Elements float, double,

char, structures, …

Variables Arrays (e.g. int Ar[5];) Indexes start with 0!!!

Pointers

Technische Universität München

Lect7-Page14

1 0 1 1 0 0 1 0 bin

0 * 20 = 0 * 1 = 0dec

1 * 21 = 1 * 2 = 2dec

0 * 22 = 0 * 4 = 0dec

0 * 23 = 0 * 8 = 0dec

1 * 24 = 0 * 16 = 16dec

1 * 25 = 0 * 32 = 32dec

0 * 26 = 0 * 64 = 0dec

1 * 27 = 0 * 128 = 128dec

= 178dec

= 1 ByteProgramming C for the Arduino: Basics

Page 8: Lecture07Arduino_2SPP

8

Technische Universität München

Lect7-Page15

Programming C for the Arduino: Basics

Function has no return value

“true” or “false”

1 byte character

Same as “byte” (see “byte”)

1 byte integer with a value from 0 to 255

2 byte integer with a value from -32768 to 32767

2 byte unsigned integer with a value from 0 to 65535

Same as “unsigned int” (see “unsigned int”)

4 byte integer with a value from -2147483648 to 2147483647

4 byte integer with a value from 0 to 4294967295

2 byte floating point (6 decimal digits precision)

4 byte floating point (15 decimal digits precision)

e.g. “char cArray[128];” similar to “array” of any type

C++ string object

Array of any type

See: http://arduino.cc/en/Reference/HomePage, Download 2012-01-11

Technische Universität München

Lect7-Page16

Pointer: Memory element:int a = 5;

5

Location of element inMemory (address)

Memory:Pointer:int *a = &a;

<&a>

Address of „a“

Programming C for the Arduino: Basics

Page 9: Lecture07Arduino_2SPP

9

Technische Universität München

Lect7-Page17

Functions

Functiondefinition

int Add (int a, int b){

return a+b;}

Functioncall

X = Add (5, 6);

=> X=11

Programming C for the Arduino: Basics

Technische Universität München

Lect7-Page18

Operators Standard operator:Assign =, Plus +, Minus -,Multiplication *, Division /, Modulo-Div. %, AND && (Bit-AND &)OR || (Bit-OR |)NOT !

…Additional operators:Add one, subtract one:++,--, (e.g. i++; is i=i+1)and a lot of others:+=, -=, …, [, ], ->, *, …

Programming C for the Arduino: Basics

Page 10: Lecture07Arduino_2SPP

10

Technische Universität München

Lect7-Page19

Comments

// This is a comment

/* This is also a comment */

Programming C for the Arduino: Basics

Technische Universität München

Lect7-Page20

Interactionwith users

Serial write

void setup() {// initialize serial communication:Serial.begin(9600);

} void loop(){

Serial.print(“On”); // Write without new lineSerial.println(“Off”); // Wite with new line

}

Programming C for the Arduino: Basics

ABABAB

Page 11: Lecture07Arduino_2SPP

11

Technische Universität München

Lect7-Page21

Applicationworkflow

Conditions

if (iIndex < 10){

Serial.print(“A“);}else{

Serial.print(“B“);}

Conditions

switch (iIndex){

case 1:Serial.print(“A“);break;

case 2:Serial.print(“B“);break;

...}

Programming C for the Arduino: Basics

Technische Universität München

Lect7-Page22

Realizing a state machine using switch condition

int iState;

void setup (){iState = 0;

}

void loop (){switch (iState){case 0:

//calibrationiState = 1;break;

case 1:// move just forward as long // as wall closer than 10 cmiState = 2;break;

case 2:...

}}

Programming C for the Arduino: Basics

0 1 2

calibration move ...

Page 12: Lecture07Arduino_2SPP

12

Technische Universität München

Lect7-Page23

Applicationworkflow

Loopsint i = 0;while (i < 10){

Serial.print(i);i = i +1;

}

Loopsint i;for (i = 1; i < 10; i++){

Serial.print(i);}

Programming C for the Arduino: Basics

Technische Universität München

Lect7-Page24

Digital Pins Output

Input

Programming C for the Arduino: Basics

Page 13: Lecture07Arduino_2SPP

13

Technische Universität München

Lect7-Page25

External modules(Libraries)

Programming C for the Arduino: Basics

Copy librarymodules to here

Use module with#include <…>

Download from:http://www.ladyada.net/make/mshield/

Technische Universität München

Lect7-Page26

MAC OS X:

If you are using Mac OS X, the libraries folder is hidden in the Arduino application:

Right-click on Arduino.app, "Show package content", then put the library folder in "Contents/Resources/Java/hardware/libraries".

Include the library using the menu in the IDE, or type “#include <AFMotor.h>”

You are now ready to use the library.

Page 14: Lecture07Arduino_2SPP

14

Technische Universität München

Lect7-Page27

Lecture 7: Programming for the Arduino

- The hardware- The programming environment- Binary world, from Assembler to C- Programming C for the Arduino: Basics- Programming C for the Arduino: more …- Programming style

Technische Universität München

Lect7-Page28

Programming C for the Arduino: more ...

Motordriver for DC-motors: basic functionality

Motor armature

Commutator

Carbon brush

http://de.wikipedia.org/w/index.php?title=Datei:Gleichstrommaschine.svg&filetimestamp=20070820012802http://www.hpw-modellbahn.de/eisenbahntechnik/motoren.htm

Page 15: Lecture07Arduino_2SPP

15

Technische Universität München

Lect7-Page29

Motordriver for DC-motors: basic functionality

Programming C for the Arduino: more ...

Motor armature

Commutator

Carbon brush

GND

Rotation direction

http://www.rn-wissen.de/index.php/Getriebemotoren_Ansteuerung

Technische Universität München

Lect7-Page30

Motordriver for DC-motors: basic functionality (with driver IC L293 D)

Programming C for the Arduino: more ...

Motor armature

Commutator

Carbon brush

http://www.rn-wissen.de/index.php/Getriebemotoren_Ansteuerung

Page 16: Lecture07Arduino_2SPP

16

Technische Universität München

Lect7-Page31

Programming C for the Arduino: more ...

GND

Rotation directionand On/Off+/- rotate forward-/+ rotate backward+/+ or -/- stop

Enable = speed (PWM)

Motordriver for DC-motors: basic functionality (with driver IC L293 D)

http://www.rn-wissen.de/index.php/Getriebemotoren_Ansteuerung

Technische Universität München

Lect7-Page32

Programming C for the Arduino: more ...

Motordriver for DC-motors: Adafruit Motor/Stepper/Servo Shield for Arduino kit - v1.0

http://www.adafruit.com/index.php?main_page=product_info&cPath=17_21&products_id=81

#1

#2

#3

#4

Page 17: Lecture07Arduino_2SPP

17

Technische Universität München

Lect7-Page33

Programming C for the Arduino: more ...

Motordriver for DC-motors: Adafruit Motor/Stepper/Servo Shield for Arduino kit - v1.0

// Use Adafruit module#include <AFMotor.h>

// create motor #2, 64KHz pwmAF_DCMotor MotorRight( 2, MOTOR12_64KHZ);

// set the speedMotorRight.setSpeed( 50);

// Move forward for (int i=0;i< 500 ;i++){

MotorRight.run(FORWARD);}MotorRight.run(RELEASE); // switch off

// similar BACKWARD// MotorRight.run(BACKWARD);

setup()

loop()

http://www.ladyada.net/make/mshield/

Technische Universität München

Lect7-Page34

Programming C for the Arduino: more ...

Motordriver for step-motors (steppers): basic functionality

Inductor 1 Inductor 2

2x r

esis

tanc

e

http://www.shop.robotikhardware.de/http://www.rn-wissen.de/index.php/Schrittmotorenhttp://www.schule-bw.de/unterricht/faecher/physik/mess/soundkarte/hardware/interfsound/schrittschlitt.htm

Page 18: Lecture07Arduino_2SPP

18

Technische Universität München

Lect7-Page35

Motordriver for step-motors (steppers): RN-Stepp297

Programming C for the Arduino: more ...

Power

5V GND Rotation direction

Clockpulseforsteps

http://www.shop.robotikhardware.de/

Technische Universität München

Lect7-Page36

Motordriver for step-motors (steppers): RN-Stepp297

Programming C for the Arduino: more ...

5VGND

Rotation direction

Clockpulseforsteps(PWM)

http://www.shop.robotikhardware.de/

+5V or 0V

Page 19: Lecture07Arduino_2SPP

19

Technische Universität München

Lect7-Page37

Motordriver for step-motors (steppers): RN-Stepp297 - programming

Programming C for the Arduino: more ...

// Use stepper module#include <Stepper.h>

// Create stepper class variableStepper StepperLeft ( steps , pin_direction , pin_clockpulse );e.g. StepperLeft (200, 8, 9)

StepperRight (200, 7, 10)

// set the speed of the motor to 200 RPMsStepperLeft.setSpeed( 200 );

// Move one step forward StepperLeft.step(1);

// Move one step backwardStepperLeft.step(-1);

setup()

loop()

http://arduino.cc/en/Reference/Stepper?from=Tutorial.Stepper

Technische Universität München

Lect7-Page38

Programming C for the Arduino: more ...

Motordriver for step-motors (steppers): Adafruit Motor/Stepper/Servo Shield for Arduino kit - v1.0

#1#2

Page 20: Lecture07Arduino_2SPP

20

Technische Universität München

Lect7-Page39

Programming C for the Arduino: more ...

Motordriver for step-motors (steppers): Adafruit Motor/Stepper/Servo Shield for Arduino kit - v1.0

// Use Adafruit module#include <AFMotor.h>

// create motor stepper #2 with 48 stepsAF_Stepper StepperLeft( 48, 2);

// set the speedStepperLeft.setSpeed(60); StepperLeft.release();

// Move forward StepperLeft.step(1, FORWARD, SINGLE);

// Move backward StepperLeft.step(1, BACKWARD, SINGLE);

setup()

loop()

http://www.ladyada.net/make/mshield/

Technische Universität München

Lect7-Page40

Low Cost Ultra Sonic Range Finder

Programming C for the Arduino: more ...

5V

GND

Trigger pulse and signal

Page 21: Lecture07Arduino_2SPP

21

Technische Universität München

Lect7-Page41

Low Cost Ultra Sonic Range Finder

Programming C for the Arduino: more ...

Range = uSec/58=cm or uSec/148=inches.

Technische Universität München

Lect7-Page42

Low Cost Ultra Sonic Range Finder

Programming C for the Arduino: more ...

Range = uSec/58=cm or uSec/148=inches.

e.g. int iPingPin = 13;

Page 22: Lecture07Arduino_2SPP

22

Technische Universität München

Lect7-Page43

Robot Compass Modul

Programming C for the Arduino: more ...

PWM-signal

0.1 msec == 1 degree

5V

GND

Signal (PWM)

e.g. int iCompassPin = 11;

Technische Universität München

Lect7-Page44

Lecture 7: Programming for the Arduino

- The hardware- The programming environment- Binary world, from Assembler to C- Programming C for the Arduino: Basics- Programming C for the Arduino: more …- Programming style

Page 23: Lecture07Arduino_2SPP

23

Technische Universität München

Lect7-Page45

Programming style

See: http://casablanca.informatik.hu-berlin.de/database/MDA-UML/VL/V1-MODSOFT-I.1.Ueberblick.pdf, Download 07.11.2006

Technische Universität München

Lect7-Page46

Programming style

Try to write readable and efficient code:

- Add comments to explain the code- Use understandable (variable, function) names- Structure the code into logical units- Initialize variables- Search for existing code first- …

Page 24: Lecture07Arduino_2SPP

24

Technische Universität München

Lect7-Page47

Programming style

And for help look at:

Technische Universität München

Lect7-Page48Thank you

X30cm

30cm

Stop the robot:while (1==1) {}

The goal!