Arduino Programming Part 2

Similar documents
Microcontroller Programming Beginning with Arduino. Charlie Mooney

Lab 6 Introduction to Serial and Wireless Communication

Electronic Brick of Current Sensor

Lecture 7: Programming for the Arduino

Programming the Arduino

arrays C Programming Language - Arrays

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

Arduino Lesson 5. The Serial Monitor

CanSat Program. Stensat Group LLC

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

Informatica e Sistemi in Tempo Reale

The Answer to the 14 Most Frequently Asked Modbus Questions

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

Example of a Java program

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

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

Arduino Lesson 14. Servo Motors

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

Conditionals (with solutions)

1 Coffee cooling : Part B : automated data acquisition

Decimal Number (base 10) Binary Number (base 2)

Arduino Lesson 13. DC Motors. Created by Simon Monk

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

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

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

Stacks. Linear data structures

EARTH PEOPLE TECHNOLOGY SERIAL GRAPH TOOL FOR THE ARDUINO UNO USER MANUAL

DS1307 Real Time Clock Breakout Board Kit

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Arduino Lesson 16. Stepper Motors

AP Computer Science Java Subset

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

GM862 Arduino Shield

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

Your Multimeter. The Arduino Uno 10/1/2012. Using Your Arduino, Breadboard and Multimeter. EAS 199A Fall Work in teams of two!

ZX-NUNCHUK (# )

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Arduino DUE + DAC MCP4922 (SPI)

Introduction to Java. CS 3: Computer Programming in Java

Computer Architectures

Ultrasonic Distance Measurement Module

Introduction to Arduino

Arduino project. Arduino board. Serial transmission

AirCasting Particle Monitor Bill of Materials

Java Interview Questions and Answers

Arduino provides a standard form factor that breaks the functions of the micro-controller into a more accessible package.

RGB LED Strips. Created by lady ada. Last updated on :00:18 PM EST

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Servo Motor API nxt_motor_get_count nxt_motor_set_count nxt_motor_set_speed

Connecting Arduino to Processing a

Microcontroller Code Example Explanation and Words of Wisdom For Senior Design

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

1 Abstract Data Types Information Hiding

CSI 333 Lecture 1 Number Systems

Wireless Communication With Arduino

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Temperature Measurement with a Thermistor and an Arduino

Chapter 2: Elements of Java

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

1 of 5 12/31/ :51 AM

Integer Operations. Overview. Grade 7 Mathematics, Quarter 1, Unit 1.1. Number of Instructional Days: 15 (1 day = 45 minutes) Essential Questions

COSC282 BIG DATA ANALYTICS FALL 2015 LECTURE 2 - SEP 9

Temperature Measurement with a Thermistor and an Arduino

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

Introduction to Java

VB.NET Programming Fundamentals

MXwendler Javascript Interface Description Version 2.3

Introduction to Python

Arduino Shield Manual

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

To Evaluate an Algebraic Expression

Chapter 4 -- Decimals

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

AP Computer Science Java Mr. Clausen Program 9A, 9B

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

Arduino Lab 1 - The Voltage Divider

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Preamble. Kirchoff Voltage Law (KVL) Series Resistors. In this section of my lectures we will be. resistor arrangements; series and

Basic Pulse Width Modulation

Chapter 2 Introduction to Java programming

Pemrograman Dasar. Basic Elements Of Java

Exercise 4 Learning Python language fundamentals

Lecture 22: C Programming 4 Embedded Systems

The string of digits in the binary number system represents the quantity

How To Write An Array In A Microsoft Zil

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

5 Arrays and Pointers

Arduino Lesson 9. Sensing Light

Arduino Lesson 1. Blink

Addition Methods. Methods Jottings Expanded Compact Examples = 15

Yun Shield Quick Start Guide VERSION: 1.0 Version Description Date 1.0 Release 2014-Jul-08 Yun Shield Quick Start Guide 1 / 14

Java Basics: Data Types, Variables, and Loops

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

Binary Adders: Half Adders and Full Adders

How To Control A Car With A Thermostat

Object Oriented Software Design

Answers to Review Questions Chapter 7

Transcription:

Arduino Programming Part 2 EAS 199A Lecture 6 Fall 2011 Overview Variable types int float Loops for loops while loops (another day) 2 Arduino web site Assigning and Using Variables http://arduino.cc/en/reference/homepage http://www.arduino.cc/en/tutorial/variables http://arduino.cc/en/tutorial/foundations The more common variable types are integers: int, long, unsigned int, unsigned long floating point values: (numbers with fractional parts float, double characters and character strings char, string, String arrays 3

Integers are used for counting int integers in the range 32,768 to 32,767 unsigned int integers in the range 0 to 65,535 long integers in the range 2,147,483,648 to 2,147,483,647 unsigned long integers in the range 0 to 4,294,967,295 4 Practical usage of int and long Use an int for most common tasks requiring integers Use an int for most loop counters: int i, n=16; for ( i=0; i<n; i++) { // loop body An int is returned by a built-in functions, e.g. analogread int val, photo_pin=4; val = analogread(photo_pin); 5 Practical usage of int and long Use a long when the range of values is very large, e.g. measuring the system time in milliseconds long start_time, current_time; long wait_time = 86400000; // one day void setup() { start_time = millis(); Serial.begin(9600); void loop() { current_time = millis(); if ( (current_time - start_time) > wait_time ) { Serial.println( 24 hours has passed ); start_time = current_time; 6

float Floating point numbers are used for computing with fractional values numbers with fractional part values in the range 3.4028235 10 38 to 3.4028235 10 38 Practical advice Use a float in formulas when fractional values are needed A float can be very large or small floating point math involves small rounding errors 7 Integer and floating point variables use different arithmetic rules Integer math: Division rounds to nearest int int a, b, c; a = 4; b = 3; c = a/b; // Value of 1 is stored in c Floating point math float x, y, z; x = 4.0; // Include point zero to reinforce y = 3.0; // that x and y are floats z = x/z; // Value of 1.3333333 is stored in z 8 Use conversion functions to change type Convert to an integer: a = int(x); Convert to a floating point value: x = float(i); Practical Advice Use explicit type conversion functions to convey your intent 9

Defining and Using Variables All variables must be declared before use Declaration consists of a type specification and the variable name A declaration may also include an assignment Use meaningful variable names Add comments to further clarify meaning Examples int red_pin; // declaration only int blue_pin = 5; // declaration and assignment int greenpin = 0; float voltage; // Voltage of the input signal float maxvoltage = 5.0; // Maximum range of analog input sensorval = analogread(sensorpin); // get reading // convert to floating point voltage voltage = float(sensorval)*maxvoltage/float(range); 10 Case study: Use floats to store sensor values Use photo-resistor circuit to create sensor input Convert input reading to a voltage using floating point variables Use loops to compute the average of sensor readings 5V photoresistor Analog pin 3 10 kω 11 Try it! Measure photoresistor output Build the photo-resistor circuit and run this program 5V int sensorval; int sensorpin = 3; float voltage; float input2volts = 5.0/1023.0; void setup () { Serial.begin(9600); 10 kω photoresistor Analog pin 3 void loop () { sensorval = analogread(sensorpin); voltage = float(sensorval)*input2volts; Serial.print( sensorval, voltage = ); Serial.print(sensorVal); Serial.print( ); Serial.println(voltage); 12

Loops Loops Loops allow code to be repeated Repeated code goes in a block, surrounded by { for loops need a counter while loops need an escape int i; // declare counter for ( i=0; i<=12; i++ ) { // standard structure Serial.println(i); // send value of i to serial monitor 14 Loops Initial value of counter i=0 only on first pass through the loop Stopping test: Continue while this condition is true int i; // declare counter for ( i=0; i<=12; i++ ) { // standard structure Serial.println(i); // send value of i to serial monitor Increment: How to change i on each pass through the loop 15

Loops Common loop: increment by one for ( i=0; i<=12; i++ ) { // increment by one... code block goes here Common loop: increment by two for ( i=0; i<=12; i+=2 ) { // increment by two... code block goes here Decrement by one for ( i=12; i>=0; i-- ) { // decrement by one... code block goes here 16 Try it! Modify the photoresistor program Change the loop function (modify your previous code) void loop () { float sensorave; int sensorsum; int nave=5; 5V 10 kω photoresistor Analog pin 3 sensor_sum = 0.0; for ( i=1; i<=nave; i++ ) { sensorval = analogread(sensorpin); sensorsum = sensorsum + sensorval; sensorave = float(sensorsum)/float(nave); voltage = sensorave*input2volts; Serial.print( Average voltage = ); Serial.println(voltage); This code contains errors that you will need to fix before it runs! 17 Test it! Break your code to learn how it works Change nave Increase nave from 5 to 10, 50, 100, 500 Why is the reading negative for large nave? How can you fix this by changing the variable type for sensorsum? Add print statements inside the averaging loop Serial.print( \t Reading = ); Serial.println(sensorVal); 18