Programming the Arduino



Similar documents
Lecture 7: Programming for the Arduino

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Arduino Lesson 5. The Serial Monitor

Arduino Lesson 16. Stepper Motors

The programming language C. sws1 1

Arduino Motor Shield (L298) Manual

Lab 6 Introduction to Serial and Wireless Communication

Microcontroller Programming Beginning with Arduino. Charlie Mooney

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Basic Pulse Width Modulation

2011, The McGraw-Hill Companies, Inc. Chapter 3

DS1307 Real Time Clock Breakout Board Kit

cs281: Introduction to Computer Systems Lab08 Interrupt Handling and Stepper Motor Controller

Arduino Lesson 13. DC Motors. Created by Simon Monk

Theory and Practice of Tangible User Interfaces. Thursday Week 2: Digital Input and Output. week. Digital Input and Output. RGB LEDs fade with PWM

Sources: On the Web: Slides will be available on:

How To Control A Car With A Thermostat

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

The Answer to the 14 Most Frequently Asked Modbus Questions

Eric Mitchell April 2, 2012 Application Note: Control of a 180 Servo Motor with Arduino UNO Development Board

Arduino Internet Connectivity: Maintenance Manual Julian Ryan Draft No. 7 April 24, 2015

Installing Java (Windows) and Writing your First Program

Arduino Lesson 14. Servo Motors

Informatica e Sistemi in Tempo Reale

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

Electronic Brick of Current Sensor

GM862 Arduino Shield

IR Communication a learn.sparkfun.com tutorial

ZX-NUNCHUK (# )

Arduino Microcontroller Guide W. Durfee, University of Minnesota ver. oct-2011 Available on-line at

Arduino Lesson 1. Blink

MICROPROCESSOR AND MICROCOMPUTER BASICS

S and STEP 7 Basic V10.5

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

AN601 I2C 2.8 Communication Protocol. SM130 SM130 - Mini APPLICATION NOTE

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

RB-See-211. Seeedstudio Grove 6-15V, 2A Dual I2C Motor Driver. Grove - I2C Motor Driver. Introduction

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Data Acquisition Module with I2C interface «I2C-FLEXEL» User s Guide

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

PowerBox PBX 180. Installation and Users Guide

Bluetooth + USB 16 Servo Controller [RKI-1005 & RKI-1205]

How To Write Portable Programs In C

Introduction to Java

Servo Info and Centering

Microprocessor & Assembly Language

Work with Arduino Hardware

CPU Organization and Assembly Language

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Arduino Lesson 0. Getting Started

Ultrasonic Distance Measurement Module

MeshBee Open Source ZigBee RF Module CookBook

Pololu DRV8835 Dual Motor Driver Shield for Arduino

Chapter 7D The Java Virtual Machine

Application Note: AN00141 xcore-xa - Application Development

1 Classical Universal Computer 3

8051 MICROCONTROLLER COURSE

How to program a Zumo Robot with Simulink

Transmitter Interface Program

Number Representation

RS-485 Protocol Manual

An Introduction to Computer Science and Computer Organization Comp 150 Fall 2008

E-Blocks Easy Internet Bundle

Programmable Logic Controllers Definition. Programmable Logic Controllers History

Freescale Semiconductor, I

1 Abstract Data Types Information Hiding

Software Manual RS232 Laser Merge Module. Document # SU Rev A

Object Oriented Software Design

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

3 SOFTWARE AND PROGRAMMING LANGUAGES

CanSat Program. Stensat Group LLC

Arduino-Based Dataloggers: Hardware and Software David R. Brooks Institute for Earth Science Research and Education V 1.2, June, , 2015

Pemrograman Dasar. Basic Elements Of Java

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Sending Data from a computer to a microcontroller using a UART (Universal Asynchronous Receiver/Transmitter)

(Refer Slide Time: 00:01:16 min)

Modern Robotics, Inc Core Device Discovery Utility. Modern Robotics Inc, 2015

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

DC Motor Driver 24V 20A [RKI-1340]

Crazy Alarm Clock L A K S H M I M E Y Y A P P A N J A M E S K A Y E W I L L I A M D I E H L C O N G C H E N

C++ Programming Language

MODULE BOUSSOLE ÉLECTRONIQUE CMPS03 Référence :

Set up and Blink - Simulink with Arduino

Candle Plant process automation based on ABB 800xA Distributed Control Systems

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

DELLORTO. Instructions Manual. Deuss Service Tool For ECS System ECU. Dell Orto Deuss Service Tool instruction manual Page 1 of 11.

MACHINE ARCHITECTURE & LANGUAGE

Stacks. Linear data structures

1602 LCD adopts standard 14 pins(no backlight) or 16pins(with backlight) interface, Instruction of each pin interface is as follows:

The C Programming Language course syllabus associate level

Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.

Let s put together a Manual Processor

SYSTEM 45. C R H Electronics Design

FTC Android Based Control System

Lab Experience 17. Programming Language Translation

Event counters in NOVA

Data Sheet. Adaptive Design ltd. Arduino Dual L6470 Stepper Motor Shield V th November L6470 Stepper Motor Shield

1. Learn about the 555 timer integrated circuit and applications 2. Apply the 555 timer to build an infrared (IR) transmitter and receiver

Chapter 4 Register Transfer and Microoperations. Section 4.1 Register Transfer Language

Transcription:

Summer University 2015: Programming the Arduino Alexander Neidhardt (FESG) neidhardt@fs.wettzell.de SU-Arduino-Prog-Page1 Programming the Arduino - The hardware - The programming environment - Binary world, from Assembler to C - - Programming the Arduino in C: more - Programming style SU-Arduino-Prog-Page2 1

The hardware http://www.arduino.cc/ SU-Arduino-Prog-Page3 Programming the Arduino - The hardware - The programming environment - Binary world, from Assembler to C - - Programming the Arduino in C: more - Programming style SU-Arduino-Prog-Page4 2

Programming environment Download from: http://arduino.cc/en/main/software SU-Arduino-Prog-Page5 Programming the Arduino - The hardware - The programming environment - Binary world, from Assembler to C - - Programming the Arduino in C: more - Programming style SU-Arduino-Prog-Page6 3

Binary world, programming from Assembler to C Programmable, mechanical calculation machines (19th/early 20th cent. AD) Falcon (1728) Joseph-Marie Jacquard (1805) -> punchcard looms for work steps (program) 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 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 See: Rembold, Ulrich et. al.: Einführung in die Informatik für Naturwissenschaftler und Ingenieure. Hanser München Wien 1991 +5V- 1 0 0 1 1 SU-Arduino-Prog-Page7 Binary world, programming from Assembler to C The machine instructions Assembler mnemonic: add ax, 1000 (short Assembler ) Assembler conversion Machine code (80x86): 000001011110100000000011 00000101 Operation code for add to accumulator high byte low byte 00000011 11101000 Binary representation of number 1000 See: http://ivs.cs.uni-magdeburg.de/bs/lehre/sose99/bs1/seminare/assembler.shtml, Download 30.08.2007 SU-Arduino-Prog-Page8 4

Binary world, programming from Assembler to C Programming paradigm of the problem oriented computer language 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 See: http://en.wikipedia.org/wiki/programming_paradigm, Download 08.10.2007 SU-Arduino-Prog-Page9 Binary world, programming from Assembler to C From source code to machine instructions Source code in a specific computer language Assembler mnemonic (short Assembler ) Compiler translation and optimisation conversion Assembler/Disassembler Machine code (e.g. 80x86, ) 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. See: http://ivs.cs.uni-magdeburg.de/bs/lehre/sose99/bs1/seminare/assembler.shtml, Download 30.08.2007 SU-Arduino-Prog-Page10 5

Binary world, programming from Assembler to C Phase Programming Creation Compiler Source codes Source codes Preprocessor/ Compilation Object codes Library codes Linking Executables Runtime Load Executables Executable libraries Upload via USB SU-Arduino-Prog-Page11 Programming the Arduino - The hardware - The programming environment - Binary world, from Assembler to C - - Programming the Arduino in C: more - Programming style SU-Arduino-Prog-Page12 6

Our first program: Blink (LED) http://www.led-shop.com/images/led_anschluss.jpg SU-Arduino-Prog-Page13 Memory Elements short, int, long, float, double, char, structures, Variables Arrays (e.g. int Ar[5];) Indexes start with 0!!! Pointers SU-Arduino-Prog-Page14 7

= 1 Byte 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 SU-Arduino-Prog-Page15 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 SU-Arduino-Prog-Page16 8

Pointer: Pointer: int *a = &a; <&a> Memory element: int a = 5; 5 Memory: Address of a Location of element in Memory (address) SU-Arduino-Prog-Page17 Functions Function definition int Add (int a, int b) { return a+b; } Function call X = Add (5, 6); => X=11 SU-Arduino-Prog-Page18 9

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: +=, -=,, [, ], ->, *, SU-Arduino-Prog-Page19 Comments // This is a comment /* This is also a comment */ SU-Arduino-Prog-Page20 10

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 SU-Arduino-Prog-Page21 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;... } SU-Arduino-Prog-Page22 11

Application workflow Loops int i = 0; while (i < 10) { Serial.print(i); } Loops int i; for (i = 1; i < 10; i++) { Serial.print(i); } SU-Arduino-Prog-Page23 Digital Pins Output Input SU-Arduino-Prog-Page24 12

External modules (Libraries) Copy modules to here Use module with #include < > SU-Arduino-Prog-Page25 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. SU-Arduino-Prog-Page26 13

Programming the Arduino - The hardware - The programming environment - Binary world, from Assembler to C - - Programming the Arduino in C: more - Programming style SU-Arduino-Prog-Page27 Motordriver for DC-motors: basic functionality 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 SU-Arduino-Prog-Page28 14

Motordriver for DC-motors: basic functionality Motor armature Commutator Carbon brush Rotation direction GND http://www.rn-wissen.de/index.php/getriebemotoren_ansteuerung SU-Arduino-Prog-Page29 Motordriver for DC-motors: basic functionality (with driver IC L293 D) Motor armature Commutator Carbon brush http://www.rn-wissen.de/index.php/getriebemotoren_ansteuerung SU-Arduino-Prog-Page30 15

Motordriver for DC-motors: basic functionality (with driver IC L293 D) Rotation direction and On/Off +/- rotate forward -/+ rotate backward +/+ or -/- stop Enable = speed (PWM) GND http://www.rn-wissen.de/index.php/getriebemotoren_ansteuerung SU-Arduino-Prog-Page31 Motordriver for DC-motors: Adafruit Motor/Stepper/Servo Shield for Arduino kit - v1.0 #3 #1 #4 #2 http://www.adafruit.com/index.php?main_page=product_info&cpath=17_21&products_id=81 SU-Arduino-Prog-Page32 16

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/ SU-Arduino-Prog-Page33 Motordriver for step-motors (steppers): basic functionality Inductor 1 Inductor 2 2x resistance http://www.shop.robotikhardware.de/ http://www.rn-wissen.de/index.php/schrittmotoren http://www.schulebw.de/unterricht/faecher/physik/mess/soundkarte/hardware/interfsound/schrittschlitt.htm SU-Arduino-Prog-Page34 17

Motordriver for step-motors (steppers): RN-Stepp297 Power Clock pulse for steps http://www.shop.robotikhardware.de/ 5V GND Rotation direction SU-Arduino-Prog-Page35 Motordriver for step-motors (steppers): RN-Stepp297 +5V or 0V Rotation direction Clock pulse for steps (PWM) 5V GND http://www.shop.robotikhardware.de/ SU-Arduino-Prog-Page36 18

Motordriver for step-motors (steppers): RN-Stepp297 - programming // Use stepper module #include <Stepper.h> // Create stepper class variable Stepper StepperLeft (steps, pin_direction, pin_clockpulse); e.g. StepperLeft (200, 8, 9) StepperRight (200, 7, 10) // set the speed of the motor to 200 RPMs StepperLeft.setSpeed(200); setup() // Move one step forward StepperLeft.step(1); // Move one step backward StepperLeft.step(-1); http://arduino.cc/en/reference/stepper?from=tutorial.stepper loop() SU-Arduino-Prog-Page37 Motordriver for step-motors (steppers): Adafruit Motor/Stepper/Servo Shield for Arduino kit - v1.0 #2 #1 SU-Arduino-Prog-Page38 19

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(); setup() // Move forward StepperLeft.step(1, FORWARD, SINGLE); StepperLeft.release(); // Move backward StepperLeft.step(1, BACKWARD, SINGLE); StepperLeft.release(); loop() http://www.ladyada.net/make/mshield/ SU-Arduino-Prog-Page39 Low Cost Ultra Sonic Range Finder 5V GND Trigger pulse and signal SU-Arduino-Prog-Page40 20

Low Cost Ultra Sonic Range Finder Range = usec/58=cm or usec/148=inches. SU-Arduino-Prog-Page41 Low Cost Ultra Sonic Range Finder e.g. int ipingpin = 13; Range = usec/58=cm or usec/148=inches. SU-Arduino-Prog-Page42 21

Robot Compass Modul GND Signal (PWM) 5V PWM-signal 0.1 msec == 1 degree SU-Arduino-Prog-Page43 Programming the Arduino - The hardware - The programming environment - Binary world, from Assembler to C - - Programming the Arduino in C: more - Programming style SU-Arduino-Prog-Page44 22

Programming style See: http://casablanca.informatik.hu-berlin.de/database/mda-uml/vl/v1-modsoft-i.1.ueberblick.pdf, Download 07.11.2006 SU-Arduino-Prog-Page45 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 - SU-Arduino-Prog-Page46 23

And for help look at: Programming style SU-Arduino-Prog-Page47 Data Analysis Course SU-Arduino-Prog-Page48 24

The competition... 30cm X 30cm Stop the robot: while (1==1) {} SU-Arduino-Prog-Page49 Thank you SU-Arduino-Prog-Page50 25