• No results found

Programming the Arduino

N/A
N/A
Protected

Academic year: 2021

Share "Programming the Arduino"

Copied!
25
0
0

Loading.... (view fulltext now)

Full text

(1)

SU-Arduino-Prog-Page1

Summer University 2015:

Programming the Arduino

Alexander Neidhardt (FESG)

neidhardt@fs.wettzell.de

Technische Universität München

Programming the Arduino

- The hardware

- The programming environment

- Binary world, from Assembler to C

- Programming the Arduino in C: Basics

- Programming the Arduino in C: more …

- Programming style

(2)

SU-Arduino-Prog-Page3

The hardware

http://www.arduino.cc/

Technische Universität München

SU-Arduino-Prog-Page4

Programming the Arduino

- The hardware

- The programming environment

- Binary world, from Assembler to C

- Programming the Arduino in C: Basics

- Programming the Arduino in C: more …

- Programming style

(3)

SU-Arduino-Prog-Page5

Download from:

http://arduino.cc/en/Main/Software

Technische Universität München

Programming the Arduino

- The hardware

- The programming environment

- Binary world, from Assembler to C

- Programming the Arduino in C: Basics

- Programming the Arduino in C: more …

- Programming style

(4)

SU-Arduino-Prog-Page7

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 1991 See: http://privat.swol.de/SvenBandel/index.html, Download 01.09.2007

See: http://en.wikipedia.org/wiki/Image:Boulier1.JPG, 01.09.2007

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

Falcon

(1728)

-> punchcard looms for 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

Technische Universität München

SU-Arduino-Prog-Page8

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

00000101

Operation code for „add to accumulator“

00000011 11101000

Binary representation of number

1000

low byte high byte

Assembler

Assembler mnemonic: add ax, 1000

(short „Assembler“)

conversion

(5)

SU-Arduino-Prog-Page9

Programming paradigm of the

problem oriented computer

language C

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

• 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

Technische Universität München

From source code to machine instructions

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

Assembler/Disassembler

Assembler mnemonic

(short „Assembler“)

conversion

Compiler

translation and

optimisation

Source code

in 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

Binary world, programming from Assembler to C

(6)

SU-Arduino-Prog-Page11

Binary world, programming from Assembler to C

Compiler

Programming

Creation

Runtime

Preprocessor/ Compilation Linking Executables Source codes Source codes Object codes Library codes Executables Load Executable libraries

Phase

Upload

via USB

Technische Universität München

SU-Arduino-Prog-Page12

Programming the Arduino

- The hardware

- The programming environment

- Binary world, from Assembler to C

- Programming the Arduino in C: Basics

- Programming the Arduino in C: more …

- Programming style

(7)

SU-Arduino-Prog-Page13

Our first program: Blink (LED)

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

Technische Universität München

Programming the Arduino in C: Basics

Memory short, int, long,

Elements float, double,

char,

structures,

Variables

Arrays (e.g. int Ar[5];)

Indexes start with 0!!!

(8)

SU-Arduino-Prog-Page15

1 0 1 1 0 0 1 0

bin

0 * 2

0

= 0 * 1 = 0

dec

1 * 2

1

= 1 * 2 = 2

dec

0 * 2

2

= 0 * 4 = 0

dec

0 * 2

3

= 0 * 8 = 0

dec

1 * 2

4

= 0 * 16 = 16

dec

1 * 2

5

= 0 * 32 = 32

dec

0 * 2

6

= 0 * 64 = 0

dec

1 * 2

7

= 0 * 128 = 128

dec

= 178

dec

= 1 Byte

Programming the Arduino in C: Basics

Technische Universität München

SU-Arduino-Prog-Page16

Programming the Arduino in C: 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

(9)

SU-Arduino-Prog-Page17

Pointer:

Memory element:

int a = 5;

5

Location of element in

Memory (address)

Memory:

Pointer:

int *a = &a;

<&a>

Address of „a“

Technische Universität München

Functions

Function definition

int Add (int a, int b)

{

return a+b;

}

Function call

X = Add (5, 6);

=> X=11

(10)

SU-Arduino-Prog-Page19

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 the Arduino in C: Basics

Technische Universität München

SU-Arduino-Prog-Page20

Comments

// This is a comment

/* This is also a comment */

(11)

SU-Arduino-Prog-Page21

Interaction

with users

Serial write

void setup() {

// initialize serial communication:

Serial.begin(9600);

}

void loop()

{

Serial.print(“On”); // Write without new line

Serial.println(“Off”); // Write with new line

}

AB AB AB

Technische Universität München

Application

workflow

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;

...

}

(12)

SU-Arduino-Prog-Page23

Application

workflow

Loops

int i = 0;

while (i < 10)

{

Serial.print(i);

}

Loops

int i;

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

{

Serial.print(i);

}

Programming the Arduino in C: Basics

Technische Universität München

SU-Arduino-Prog-Page24

Digital Pins

Output

Input

(13)

SU-Arduino-Prog-Page25

External modules

(Libraries)

Copy modules

to here

Use module with

#include <…>

Technische Universität München

Programming the Arduino in C: Basics

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>”

(14)

SU-Arduino-Prog-Page27

Programming the Arduino

- The hardware

- The programming environment

- Binary world, from Assembler to C

- Programming the Arduino in C: Basics

- Programming the Arduino in C: more …

- Programming style

Technische Universität München

SU-Arduino-Prog-Page28

Motordriver for DC-motors: basic functionality

Programming the Arduino in C: more ...

Motor armature

Commutator

Carbon brush

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

(15)

SU-Arduino-Prog-Page29

Motordriver for DC-motors: basic functionality

Motor armature Commutator Carbon brush

GND

Rotation direction

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

Technische Universität München

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

Programming the Arduino in C: more ...

Motor armature

Commutator Carbon brush

(16)

SU-Arduino-Prog-Page31

Programming the Arduino in C: more ...

GND

Rotation direction

and 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

SU-Arduino-Prog-Page32

Programming the Arduino in C: 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

(17)

SU-Arduino-Prog-Page33

Motordriver for DC-motors:

Adafruit Motor/Stepper/Servo Shield for Arduino kit - v1.0

// Use Adafruit module

#include <AFMotor.h>

// create motor #2, 64KHz pwm

AF_DCMotor MotorRight(

2

,

MOTOR12_64KHZ

);

// set the speed

MotorRight.setSpeed(

50

);

MotorLeft.run(

RELEASE

);

// Move forward

for (int i=0;i<

500

;i++)

{

MotorRight.run(FORWARD);

MotorRight.run(RELEASE);

}

// Move forward

for (int i=0;i<

500

;i++)

{

MotorRight.run(BACKWARD);

MotorRight.run(RELEASE);

}

setup()

loop()

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

Technische Universität München

Motordriver for step-motors (steppers): basic functionality

Programming the Arduino in C: more ...

Inductor 1

Inductor 2

2

x

r

e

s

is

ta

n

c

e

(18)

SU-Arduino-Prog-Page35

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

Programming the Arduino in C: more ...

Power

5V

GND

Rotation direction

Clock

pulse

for

steps

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

Technische Universität München

SU-Arduino-Prog-Page36

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

Programming the Arduino in C: more ...

5V

GND

Rotation

direction

Clock

pulse

for

steps

(PWM)

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

+5V or 0V

(19)

SU-Arduino-Prog-Page37

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

// Use stepper module

#include <Stepper.h>

// set the speed of the motor to 200 RPMs

StepperLeft.setSpeed(

200

);

// Move one step forward

StepperLeft.step(1);

// Move one step backward

StepperLeft.step(-1);

setup()

loop()

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

// Create stepper class variable

Stepper StepperLeft (

steps

,

pin_direction

,

pin_clockpulse

);

e.g. StepperLeft (200, 8, 9)

StepperRight (200, 7, 10)

Technische Universität München

Programming the Arduino in C: more ...

Motordriver for step-motors (steppers):

Adafruit Motor/Stepper/Servo Shield for Arduino kit - v1.0

#1

#2

(20)

SU-Arduino-Prog-Page39

Programming the Arduino in C: 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 steps

AF_Stepper StepperLeft(

60

,

2

);

// set the speed

StepperLeft.setSpeed(100);

StepperLeft.release();

// Move forward

StepperLeft.step(1, FORWARD, SINGLE);

StepperLeft.release();

// Move backward

StepperLeft.step(1, BACKWARD, SINGLE);

StepperLeft.release();

setup()

loop()

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

Technische Universität München

SU-Arduino-Prog-Page40

Low Cost Ultra Sonic Range Finder

Programming the Arduino in C: more ...

5V

GND

(21)

SU-Arduino-Prog-Page41

Low Cost Ultra Sonic Range Finder

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

Technische Universität München

Programming the Arduino in C: more ...

Low Cost Ultra Sonic Range Finder

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

(22)

SU-Arduino-Prog-Page43

Robot Compass Modul

Programming the Arduino in C: more ...

PWM-signal

0.1 msec == 1 degree

5V

GND

Signal (PWM)

Technische Universität München

SU-Arduino-Prog-Page44

Programming the Arduino

- The hardware

- The programming environment

- Binary world, from Assembler to C

- Programming the Arduino in C: Basics

- Programming the Arduino in C: more …

- Programming style

(23)

SU-Arduino-Prog-Page45

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

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

- …

(24)

SU-Arduino-Prog-Page47

Programming style

And for help look at:

Technische Universität München

SU-Arduino-Prog-Page48

(25)

SU-Arduino-Prog-Page49

X

30cm

3

0

c

m

Stop the robot:

while (1==1) {}

Technische Universität München

References

Related documents

”Libcloud is a Python library which abstracts differences between cloud provider APIs and allows users to manage their cloud resources (servers, storage, load-balancers) using a

Brydges, Reed, Fox and Anderson ) 33 ( fund out the same results; They examined the role of executive functions in predicting intelligence and showed that working

under PCT Scheme (for those who are availing PCT. Designation &amp; Employee No. a) Half-Yearly OP claim for the Period: Apr_to Sept/ Oct to Mar. b) Quarterly claim for

The relationship between the immune repertoire and the physiopathological status of individuals is essential to apprehend the genesis and the evolution of numerous

Manhattan School of Music Wednesday February 6, 2013 9:30 AM The Mischa Elman Distance Learning Center David Geber, cello. The Royal Danish Academy of Music Wednesday February

13-Feb-08 Introduction to CAESAR II and Pipe Stress Analysis. el bw S S i

9 Perubahan absorbansi maksimum daerah Q band pada ekstrak pigmen klorofil, dan Zn-klorofil terhadap inkubasi variasi suhu .....

In conclusion, the RCSA believes a Framework for Collaboration will provide the basis for effective and outcomes focused collaboration between private recruitment agencies