Technische Universität München

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

Lect7-Page1

Technische Universität München

The hardware

http://www.arduino.cc/

Lect7-Page2

1

Technische Universität München

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

Lect7-Page3

Technische Universität München

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

Lect7-Page4

2

The hardware . 01. subtraction.Programming C for the Arduino: more … . multiplication and division 0 0 1 1 +5VSee: See: See: See: Rembold.The programming environment . Hanser München Wien 1991 Lect7-Page6 3 .Technische Universität München Lecture 7: Programming for the Arduino .wikipedia.swol.html.2007 Rembold.09. al.09. programming from Assembler to C Programmable.de/SvenBandel/index.Binary world. mechanical calculation machines (19th/early 20th cent.Programming style Lect7-Page5 Technische Universität München Binary world. 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. Hanser München Wien 1991 http://privat.: Einführung in die Informatik für Naturwissenschaftler und Ingenieure.org/wiki/Image:Boulier1. Ulrich et. Download 01.JPG.Programming C for the Arduino: Basics . Ulrich et. from Assembler to C .: Einführung in die Informatik für Naturwissenschaftler und Ingenieure. al.2007 http://en. AD) Falcon (1728) Joseph-Marie Jacquard (1805) -> punchcard looms for work steps (program) 1 Origins at China and also developed by the mathematician Leibniz (17th century AD) Positional numeral system which represents each number just with 2 symbols.

wikipedia.g.08.10. programming from Assembler to C The machine instructions Assembler mnemonic: (short „Assembler“) add ax.uni-magdeburg.org/wiki/Programming_paradigm.2007 Lect7-Page7 Technische Universität München Binary world. Download 30. reusable sections called procedures or functions with own scopes. C Case sensitive See: http://en.2007 Lect7-Page8 4 . Download 08. 1000 Assembler conversion Machine code (80x86): 000001011110100000000011 high byte low byte 00000101 Operation code for „add to accumulator“ 00000011 11101000 Binary representation of number 1000 See: http://ivs.de/bs/lehre/sose99/bs1/seminare/assembler.shtml.Technische Universität München Binary world. programming from Assembler to C Programming paradigm of the problem oriented computer language C++/C • Procedural programming with structured programming as subset • • • • • Code is splitted into several.cs. which can be called at given code positions Logical procedure units are combined to modules Jumps (like goto) are not allowed E.

g. so that the knowledge about the results after comiplation can improve the runtime of a program. programming from Assembler to C From source code to machine instructions Source code in a specific computer language Compiler Assembler mnemonic (short „Assembler“) translation and optimisation conversion Assembler/Disassembler The process of compilation is unidirectional. Lect7-Page9 Machine code (e. On the other hand the optimisation is not ideal.08. There are several representations of a solution for a specific task in problemoriented languages which results in the same representation in machine code.Technische Universität München Binary world. programming from Assembler to C Phase Programming Creation Compiler Source codes Source codes Preprocessor/ Compilation Object codes Linking Executables Library codes Runtime Load Executables Executable libraries Upload via USB Lect7-Page10 5 . …) See: http://ivs.2007 Technische Universität München Binary world.de/bs/lehre/sose99/bs1/seminare/assembler.uni-magdeburg.cs. Download 30.shtml. 80x86.

jpg Lect7-Page12 6 .Binary world.Programming C for the Arduino: more … . from Assembler to C .Programming style Lect7-Page11 Technische Universität München Programming C for the Arduino: Basics Our first program: Blink (LED) http://www.Technische Universität München Lecture 7: Programming for the Arduino .Programming C for the Arduino: Basics .led-shop.The programming environment .The hardware .com/images/LED_Anschluss.

double.g. int Ar[5]. … Variables Arrays (e.Technische Universität München Programming C for the Arduino: Basics Memory Elements int.) Indexes start with 0!!! Pointers Lect7-Page13 Technische Universität München Programming C for the Arduino: Basics = 1 Byte 1 0 1 1 0 0 1 0 bin 0 * 20 = 0 * 1 = 0dec 1 * 21 = 1 * 2 = 2dec 0 * 22 = 0 * 4 = 0dec 3 0*2 = 0*8 = 0dec 4 1 * 2 = 0 * 16 = 16dec 5 1 * 2 = 0 * 32 = 32dec 0 * 26 = 0 * 64 = 0dec 1 * 27 = 0 * 128 = 128dec = 178dec Lect7-Page14 7 . char. structures. long. float.

g.cc/en/Reference/HomePage. <&a> Address of „a“ Memory element: int a = 5. 5 Location of element in Memory (address) Memory: Lect7-Page16 8 . Download 2012-01-11 Lect7-Page15 Technische Universität München Programming C for the Arduino: Basics Pointer: Pointer: int *a = &a. “char cArray[128].Technische Universität München 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.” similar to “array” of any type C++ string object Array of any type See: http://arduino.

g. subtract one: ++. -. ]. %. …. *. (e. i++. -=.Technische Universität München Programming C for the Arduino: Basics Functions Functiondefinition int Add (int a. int b) { return a+b. *. => X=11 Lect7-Page17 Technische Universität München Programming C for the Arduino: Basics Operators Assign Plus Minus Multiplication Division Modulo-Div. +. … Lect7-Page18 9 . AND OR NOT Standard operator: =. is i=i+1) and a lot of others: +=. } Functioncall X = Add (5. && (Bit-AND &) || (Bit-OR |) ! … Additional operators: Add one. /. ->. [. 6).--.

// Write without new line Serial.println(“Off”).Technische Universität München Programming C for the Arduino: Basics Comments // This is a comment /* This is also a comment */ Lect7-Page19 Technische Universität München Programming C for the Arduino: Basics Interaction with users Serial write void setup() { // initialize serial communication: Serial. // Wite with new line } AB AB AB Lect7-Page20 10 .print(“On”).begin(9600). } void loop() { Serial.

print(“B“). 0 1 2 void loop () { switch (iState) { case 0: //calibration iState = 1.Technische Universität München Programming C for the Arduino: Basics Application workflow Conditions if (iIndex < 10) { Serial... } Lect7-Page21 Technische Universität München Programming C for the Arduino: Basics Realizing a state machine using switch condition int iState. case 2: Serial.. } calibration move .. break. case 2: . } else { Serial. break.print(“A“).print(“A“).. void setup () { iState = 0. break.. } } Lect7-Page22 11 .print(“B“). } Conditions switch (iIndex) { case 1: Serial. break. case 1: // move just forward as long // as wall closer than 10 cm iState = 2. .

print(i). i < 10.print(i).Technische Universität München Programming C for the Arduino: Basics Application workflow Loops int i = 0. } Lect7-Page23 Technische Universität München Programming C for the Arduino: Basics Digital Pins Output Input Lect7-Page24 12 . i = i +1. } Loops int i. i++) { Serial. for (i = 1. while (i < 10) { Serial.

net/make/mshield/ Lect7-Page25 Technische Universität München MAC OS X: If you are using Mac OS X.h>” You are now ready to use the library. then put the library folder in "Contents/Resources/Java/hardware/libraries".app. Lect7-Page26 13 . the libraries folder is hidden in the Arduino application: Right-click on Arduino. "Show package content".ladyada.Technische Universität München Programming C for the Arduino: Basics External modules (Libraries) Copy library modules to here Use module with #include <…> Download from: http://www. or type “#include <AFMotor. Include the library using the menu in the IDE.

htm Lect7-Page28 14 .svg&filetimestamp=20070820012802 http://www.The programming environment .Programming style Lect7-Page27 Technische Universität München Programming C for the Arduino: more .The hardware .php?title=Datei:Gleichstrommaschine. Motordriver for DC-motors: basic functionality Motor armature Commutator Carbon brush http://de.Programming C for the Arduino: more … .de/eisenbahntechnik/motoren.Programming C for the Arduino: Basics ..hpw-modellbahn.Binary world.wikipedia.org/w/index..Technische Universität München Lecture 7: Programming for the Arduino . from Assembler to C .

.php/Getriebemotoren_Ansteuerung Lect7-Page30 15 .de/index..rn-wissen. Motordriver for DC-motors: basic functionality Motor armature Commutator Carbon brush Rotation direction GND http://www..php/Getriebemotoren_Ansteuerung Lect7-Page29 Technische Universität München Programming C for the Arduino: more .Technische Universität München Programming C for the Arduino: more .rn-wissen.de/index. Motordriver for DC-motors: basic functionality (with driver IC L293 D) Motor armature Commutator Carbon brush http://www..

.de/index. Motordriver for DC-motors: basic functionality (with driver IC L293 D) Enable = speed (PWM) Rotation direction and On/Off +/.v1.rotate forward -/+ rotate backward +/+ or -/..Technische Universität München Programming C for the Arduino: more .com/index..stop GND http://www.php?main_page=product_info&cPath=17_21&products_id=81 Lect7-Page32 16 .adafruit.php/Getriebemotoren_Ansteuerung Lect7-Page31 Technische Universität München Programming C for the Arduino: more .rn-wissen..0 #3 #1 #4 #2 http://www. Motordriver for DC-motors: Adafruit Motor/Stepper/Servo Shield for Arduino kit .

64KHz pwm AF_DCMotor MotorRight(2.htm 2x resistance Lect7-Page34 17 .de/ http://www.net/make/mshield/ Lect7-Page33 Technische Universität München Programming C for the Arduino: more .h> // create motor #2.run(RELEASE).run(FORWARD).i<500.shop. } MotorRight. // set the speed MotorRight.v1.Technische Universität München Programming C for the Arduino: more . Motordriver for step-motors (steppers): basic functionality Inductor 1 Inductor 2 http://www.. setup() loop() http://www..de/index..schulebw.robotikhardware.php/Schrittmotoren http://www.i++) { MotorRight. Motordriver for DC-motors: Adafruit Motor/Stepper/Servo Shield for Arduino kit .0 // Use Adafruit module #include <AFMotor.de/unterricht/faecher/physik/mess/soundkarte/hardware/interfsound/schrittschlitt.rn-wissen.setSpeed(50).ladyada. MOTOR12_64KHZ). // Move forward for (int i=0. // switch off // similar BACKWARD // MotorRight..run(BACKWARD).

de/ 18 . Motordriver for step-motors (steppers): RN-Stepp297 Power Clock pulse for steps 5V GND Rotation direction http://www.Technische Universität München Programming C for the Arduino: more .shop..robotikhardware.. Motordriver for step-motors (steppers): RN-Stepp297 +5V or 0V Rotation direction Clock pulse for steps (PWM) 5V GND Lect7-Page36 http://www...robotikhardware.shop.de/ Lect7-Page35 Technische Universität München Programming C for the Arduino: more .

10) // set the speed of the motor to 200 RPMs StepperLeft.cc/en/Reference/Stepper?from=Tutorial. e. 8. StepperLeft (200..step(1). pin_direction.setSpeed(200).. setup() // Move one step forward StepperLeft.Technische Universität München Programming C for the Arduino: more .programming // Use stepper module #include <Stepper. Motordriver for step-motors (steppers): RN-Stepp297 . pin_clockpulse). 9) StepperRight (200... http://arduino. 7. loop() // Move one step backward StepperLeft.0 #2 #1 Lect7-Page38 19 .h> // Create stepper class variable Stepper StepperLeft (steps.v1.Stepper Lect7-Page37 Technische Universität München Programming C for the Arduino: more .g. Motordriver for step-motors (steppers): Adafruit Motor/Stepper/Servo Shield for Arduino kit .step(-1).

BACKWARD. StepperLeft. SINGLE). // set the speed StepperLeft.release().. setup() // Move forward StepperLeft.setSpeed(60).step(1.v1.h> // create motor stepper #2 with 48 steps AF_Stepper StepperLeft(48.Technische Universität München Programming C for the Arduino: more .0 // Use Adafruit module #include <AFMotor. http://www.ladyada. FORWARD. Low Cost Ultra Sonic Range Finder 5V GND Trigger pulse and signal Lect7-Page40 20 .. Motordriver for step-motors (steppers): Adafruit Motor/Stepper/Servo Shield for Arduino kit .step(1. 2).. SINGLE). loop() // Move backward StepperLeft..net/make/mshield/ Lect7-Page39 Technische Universität München Programming C for the Arduino: more .

Lect7-Page42 21 .g. Lect7-Page41 Technische Universität München Programming C for the Arduino: more .. Low Cost Ultra Sonic Range Finder Range = uSec/58=cm or uSec/148=inches. int iPingPin = 13..Technische Universität München Programming C for the Arduino: more ... Low Cost Ultra Sonic Range Finder e. Range = uSec/58=cm or uSec/148=inches.

.g. Lect7-Page43 Technische Universität München Lecture 7: Programming for the Arduino . Robot Compass Modul GND Signal (PWM) 5V PWM-signal 0.The programming environment . from Assembler to C .The hardware .Programming C for the Arduino: more … .Programming C for the Arduino: Basics .Programming style Lect7-Page44 22 .1 msec == 1 degree e.Technische Universität München Programming C for the Arduino: more .Binary world.. int iCompassPin = 11.

Search for existing code first -… Lect7-Page46 23 .Technische Universität München Programming style See: http://casablanca.Add comments to explain the code .Use understandable (variable.1.11.informatik.Structure the code into logical units . function) names .Initialize variables .pdf. Download 07.de/database/MDA-UML/VL/V1-MODSOFT-I.hu-berlin.2006 Lect7-Page45 Technische Universität München Programming style Try to write readable and efficient code: .Ueberblick.

Technische Universität München Programming style And for help look at: Lect7-Page47 Technische Universität München The goal! Stop the robot: while (1==1) {} 30cm X 30cm Thank you Lect7-Page48 24 .