Arduino Part 1 - University of Minnesota · Arduino Part 1 Prof. Steven S. Saliterman, ... E. Time...

Post on 14-May-2018

213 views 1 download

transcript

Introductory Medical Device Prototyping

Arduino Part 1 Prof. Steven S. Saliterman, http://saliterman.umn.edu/ Department of Biomedical Engineering, University of Minnesota

Prof. Steven S. Saliterman

Arduino Uno

Prof. Steven S. Saliterman

Power & Interface…

USB Interface

Digital I/O Pins Reset Button

ATMega Microcontroller

ICSP Connector

Power & Auxiliary Pins Analog –to - Digital Converter Pins

Debug LED

7 to 12 VDC Input

Prof. Steven S. Saliterman

USB Connection to Computer…

Prof. Steven S. Saliterman

Example Modules – Sensors…

Ultrasonic Sensor - HC-SR04 IR Sensor 2Y0A21 Sharp Digital Distance Sensor

MAX6675 Module + K Type Thermocouple Temperature Sensor

Diymall Bme280 Pressure Temperature Sensor Module with IIC I2c

SunFounder Accelerometer ADXL335

Prof. Steven S. Saliterman

9-Axis Absolute Orientation Sensor…

Absolute Orientation (Euler Vector, 100Hz) Three axis orientation data based on a 360° sphere.

Absolute Orientation (Quaterion, 100Hz) Four point quaternion output for more accurate data manipulation.

Angular Velocity Vector (100Hz) Three axis of 'rotation speed' in rad/s.

Acceleration Vector (100Hz) Three axis of acceleration (gravity + linear motion) in m/s^2.

Magnetic Field Strength Vector (20Hz) Three axis of magnetic field sensing in micro Tesla (uT).

Linear Acceleration Vector (100Hz) Three axis of linear acceleration data (acceleration minus gravity) in m/s^2.

Gravity Vector (100Hz) Three axis of gravitational acceleration (minus any movement) in m/s^2.

Temperature (1Hz) Ambient temperature in degrees celsius.

Courtesy of Adafruit

Sensor “fusion” combines the following measurements…

Prof. Steven S. Saliterman

Actuators…

2-Channel 5 V Solid State Relay Module

Standard Size - High Torque - Metal Gear Servo Stepper Motor

DC Motor with Gear

Prof. Steven S. Saliterman

User Interface…

Adafruit 1.8” Color TFT LCD Display with MicroSD Card Breakout – ST77 35R1.8" Color TFT LCD display with MicroSD Card Breakout - ST7735R

7-Segment Serial Display - Red

Diymall 0.96" Inch I2c IIC Serial 128x64 Oled LCD LED White Display Module

Mini ISD2548 Digital Voice Recorder

Prof. Steven S. Saliterman

Consider this Device Concept Task: Make a digital thermometer consisting of an enclosure, microcontroller board, thermocouple sensor, digital display, sound alert, slide switch, pushbutton and battery….

http://store-usa.arduino http://www.globalsources.com http://www.thermometricscorp.com

Prof. Steven S. Saliterman

Formulate an Algorithm…

1. When the pushbutton is pressed… Measure the temperature, Beep when the reading is good, Display the value for ten seconds, and finally Save the value to memory.

2. Start all over again. 3. Now flowchart this…

Prof. Steven S. Saliterman

Flowchart the Algorithm…

Display Temperature

& Beep

Start

Delay 10 Sec.

Monitor for Button Press

Button Pressed

?

Read Temperature

Valid Reading

?

Clear Display

Store Value

Stop

No

Yes

Yes

No

Prof. Steven S. Saliterman

Software & Programming

1. Software is the smart in your “smart device.” 2. An algorithm displayed as a flowchart, transforms your problem

into various input, processing, decision and output steps 3. Lines of code are written to implement your algorithm. 4. Code may be written in assembly language and/or higher level

languages such as C, C++, and C#. 5. A compiler converts your code into machine language that the

microcontroller understands. 6. The compiled code is then uploaded into a board containing the

microcontroller, memory and various interface circuits. 7. Errors are then fixed by debugging. 8. You may write your own code and/or incorporate code that has

been written by others (“sketches”).

Prof. Steven S. Saliterman

Integrated Development Environment (IDE)

Editor – To write your code in C (.c) and/or assembly (.a) language. A finished program is called a “sketch.”

Compiler – Turns your code into machine readable instructions or object files (.o). A Linker combines this code with the standard Arduino Libraries, producing a single hex file (.h).

Means to Upload – Transferring the hex file to the Arduino board program memory. This is done via the USB or serial connection with the aid of the bootloader.

Means to Run – Executing the Program Means to Debug – Finding & Correcting Errors

Prof. Steven S. Saliterman

Programs are Written in C and/or Assembly Language

Prof. Steven S. Saliterman

Arduino Bootloader & Firmware

Bootloader is resident code that runs when the board is powered on or reset. It is programmed via the ICSP with a programmer.

This code configures the board and USB port for your subsequent use and programming directly from your computer.

You can instead remove the bootloader to save memory and program directly through the ICSP header.

Prof. Steven S. Saliterman

1. Structures A. Setup & loop B. Control statements C. Syntax D. Arithmetic operators E. Comparison operators F. Bitwise operators G. Compound operators

Arduino Programming Components

2. Variables A. Constants B. Data Types C. Variable Scope D. Qualifiers E. Conversion F. Utilities

3. Functions A. Digital I/O B. Analog I/O C. Due & Zero only D. Advanced I/O E. Time F. Math G. Trigonometry H. Characters I. Random numbers J. Bits and bytes K. External interrupts L. Interrupts M. Communication N. USB

Items in blue will be covered in this lecture, items in red in Arduino Part 2, and green in Arduino Part 3.

Prof. Steven S. Saliterman

1. Structures: Setup() Example

1) Initialize variables

2) Assign pins 3) Runs once, after

powerup or reset.

int buttonPin = 3; void setup() { Serial.begin(9600) ; //serial baud rate pinMode(buttonPin), INPUT); //assign pin 3 to be an input } void loop() { … }

Prof. Steven S. Saliterman

Loop() Example

1. Occurs after setup.

2. Loops consecutively

3. Initialize variables 4. Assign pins 5. Runs once, after

powerup or reset. 6. Variations: if, if-

else; if-else-if

int buttonPin = 3; void setup() { Serial.begin(9600) ; //serial baud rate pinMode(buttonPin), INPUT); //assign 3 to be an input } void loop() { if(digitalRead(buttonPin) == HIGH) Serial.write(‘H’); else serial.write(‘L’); delay(1000); }

Prof. Steven S. Saliterman

Control Statements

Loop Statements For While Do-While

Decision Statements Break and Continue If If-Else, if-else-if Switch-Case

Directional Goto Return

Prof. Steven S. Saliterman

Arrays…

1. The first element is indexed with zero, e.g. a[3] has 3 elements, a[0], a[1], and a[2].

2. Declare as usual, e.g. int a[3], float a[3], and char a[3]. 3. Initialize: int a[3] = {2, 6, 1}. 4. Ok to initialize using a “for” loop. 5. If number of elements is not stated, the initialization will

determine it, e.g. int a[ ] = {2, 6, 1} – elements will be three. 6. Arrays may be multidimensional, e.g. a[3, 5]. 7. Two dimensional (rows and columns) can also be written, e.g.

int M[4] [5] (remember there is a zero row and column). 8. Number of elements may be determined by a variable – in

which case range check first.

Prof. Steven S. Saliterman

“For” Statement (a Loop)…

for (initialization; condition; increment) {program statement(s);} Example – What is the value of the a[49] element? … int a[100]; for (int n = 0; n < 100; n = n + 1) { a[n] = n * 2; } …

Statement Format

Example Code

Prof. Steven S. Saliterman

“While” Statement (a Loop)…

while (expression – a boolean that is true or false) {program statement(s);} Example – What is the value of a[30] element? … int a[100]; int n = 0; while (n < 100) { a[n] = n * 3; n = n + 1; // Could also use “++n” } …

Prof. Steven S. Saliterman

“Do-while” Statement (a Loop)…

do {program statement(s)} while (test condition); Example – What is the value of a[75] element? … int a[100]; int n = 0; do { a[n] = n * 4; n = n + 1; } while n < 100; …

Prof. Steven S. Saliterman

“If” Statement (a Decision)…

if (expression) {program statement(s);} Example – What is the value of n? … int a = 4, n = 0; if a <= 5 { n = n + 50; } …

Prof. Steven S. Saliterman

“If-Else” Statement (a Decision)…

if (expression) {program statement(s);} else {program statement(s)}; Example – What is the value of n? … int a = 10, n = 0; if a <= 5 { n = n + 50; } else { n = n + 25; } …

Prof. Steven S. Saliterman

“Switch – Case” Statement (a Decision…)

switch (expression) { case label1: program statements; break; case label2: program statements; break; default: program statements; break; }

For example: int a; Bool buy; … a = 2; switch (a) { case 1: // if a =1 buy = true; break; case 2: // if a =2 buy = false; break; } …

Prof. Steven S. Saliterman

Syntax

; Used to end a statement { } Enclose statements, keep balanced // Start comment until end of line /* … */ Multi-line comment

#define Assigning a value to a constant name Follows C rules and no semicolon afterwards Use const type variable = value (e.g. const float pi = 3.14) when able instead.

#include To include outside libraries

Prof. Steven S. Saliterman

Arithmetic & Boolean Operators

= assignment operator + addition - subtraction * multiplication / division % modulo

&& and || or ! not

Prof. Steven S. Saliterman

Comparison & Pointer Operators

== equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to

* dereference & reference

Prof. Steven S. Saliterman

Bitwise & Compound Operators

& bitwise and | bitwise or ^ bitwise xor ~ bitwise not << bit shift left >> bit shift right

++ increment -- decrement += compound addition -= compound subtraction *= compound multiplication /= compound division %= compound modulo &= compound bitwise and |= compound bitwise or

Prof. Steven S. Saliterman

2. Variables: Constants

1. true | false (typed in lower case) 1. false is defined as zero 2. true is defined as one, or any boolean test of an

integer that is non-zero. 2. Integer constants:

Decimal 123 Binary B11110000 (leading B) Octal 0173 (leading zero) Hexadecimal 0x7B (leading 0x)

Prof. Steven S. Saliterman

4. By default, an integer constant is treated as an int

with the attendant limitations in values. To specify an integer constant with another data type, follow it with a: 'u' or 'U' to force the constant into an unsigned data

format. Example: 33u 'l' or 'L' to force the constant into a long data format.

Example: 100000L ‘ul’ or 'UL' to force the constant into an unsigned long

constant. Example: 32767ul

Prof. Steven S. Saliterman

5. Floating point constants: Constant Evaluates to Also 10.0 10 2.34E5 2.34 * 10^5 234000 67e-5 67.0 * 10^-5 .00067

Prof. Steven S. Saliterman

Data Types

1. boolean (8 bit) - simple logical true/false (1 byte = 8 bits) 2. byte (8 bit) - unsigned number from 0-255 3. char (8 bit) - signed number from -128 to 127. The compiler

will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results

https://learn.sparkfun.com/tutorials/data-types-in-arduino

4. unsigned char (8 bit) - same as ‘byte’; if this is what you’re after, you should use ‘byte’ instead, for reasons of clarity

5. word (16 bit) - unsigned number from 0-65535 (1 word = 2 bytes)

6. unsigned int (16 bit)- the same as ‘word’. Use ‘word’ instead for clarity and brevity.

Prof. Steven S. Saliterman

9. long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647

10. float (32 bit) or double- signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. If you can avoid it, you should.

https://learn.sparkfun.com/tutorials/data-types-in-arduino

7. int (16 bit) - signed number from -32768 to 32767. This is most commonly what you see used for general purpose variables in Arduino example code provided with the IDE.

8. unsigned long (32 bit) - unsigned number from 0-4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running.

Prof. Steven S. Saliterman

Conversion

char() - Converts a value to the char data type. byte() - Converts a value to the byte data type. int() - Converts a value to the int data type. word() - Converts a value to the word data type

or creates a word from two bytes. long() - Converts a value to the long data type. float() - Converts a value to the float data type.

Prof. Steven S. Saliterman

Variable Scope & Qualifiers

1. A global variable is one that can be seen by every function in a program. Local variables are only visible to the function in

which they are declared. In the Arduino environment, any variable declared

outside of a function (e.g. setup(), loop(), etc. ), is a global variable.

“For “ loop variables are local. 2. Static - the static keyword is used to create

variables that are visible to only one function.

Prof. Steven S. Saliterman

3. Volatile - a keyword known as a variable qualifier, it is usually used before the datatype of a variable, to modify the way in which the compiler and subsequent program treats the variable. e.g. volatile int state = LOW; Specifically, it directs the compiler to load the variable from

RAM and not from a storage register. A variable should be declared volatile whenever its value

can be changed by something beyond the control of the code section in which it appears, such as a concurrently executing thread.

In the Arduino, the only place that this is likely to occur is in sections of code associated with interrupts, called an interrupt service routine.

Prof. Steven S. Saliterman

Summary

Arduino Uno, sensors and actuator examples.

Using an IDE, programs are typically written in C and assembly language, compiled, linked with libraries and uploaded onto the Arduino board memory as hexadecimal code.

Structures, variables and functions comprise an embedded program.