ME 120: User-defined functions: average analog input reading Arduino Programming – Part 5: User-defined functions ME 120 Mechanical and Materials Engineering.

Slides:



Advertisements
Similar presentations
Intermediate Electronics and Lilypad Where Electronics Meet Textiles Workshop with Lynne Bruning and Troy Robert Nachtigall Sponsored by Spark Fun and.
Advertisements

Anurag Dwivedi & Rudra Pratap Suman.  Open Source electronic prototyping platform based on flexible easy to use hardware and software.
What is Arduino?  Arduino is a ATMEL 168 micro-controller kit designed specially for small projects  User friendly IDE(Integrated Development Environment)
temperature system wiring
Digital & Analog Inputs. Review Fundamental parts of an Arduino program are … Setting output types using pinMode. Declaring variables Can write a digital.
Khaled A. Al-Utaibi  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the.
Intro to Arduino with LilyPad Make a MakerSpace, Artisan’s Asylum Linz Craig, Chris Taylor, Mike Hord & Joel Bartlett.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
CS150 Introduction to Computer Science 1
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
Basic Circuits – Lab 2 Arduino and Sensors Xmedia Spring 2011.
Basic Circuits – Lab 2 Arduino and Sensors
Image of Arduino. Arduino discussion Address issues with circuit walk-through – Electricity, Programming, Arduino Concepts Work on BeatTable (next week)
Objectives: Lead Switching Circuitry/Control Analog to Digital Converter Write to Computer.
Pointers (Continuation) 1. Data Pointer A pointer is a programming language data type whose value refers directly to ("points to") another value stored.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
ProtoSnap Introduction to Arduino Casey Haskell, Pete Lewis, David Stillman, Jim Lindblom, Pete Dokter, Lindsay Levkoff, Trevor Zylstra.
Intro to Arduino Programming. Draw your circuits before you build them From Arduino 330 Ohm From Arduino 330 Ohm From Arduino 330 Ohm.
Arduino Week 2 Lab ECE 1020 Prof. Ahmadi. Objectives 1. Control the rotation of standard servo motor  A standard servo motor is limited in its rotation.
Pulse Width Modulation (PWM). 100% Pulse Width Modulation (PWM) 0% On the chipKIT there are 490 periods per second. Use analogWrite(pin, value) to control.
CPS120: Introduction to Computer Science Lecture 14 Functions.
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
ARDUINO 1. Basics  Comments  /* * Blink * * The basic Arduino example. Turns on an LED on for one second, * then off for one second, and so on... We.
1 - Remove LED from 13 and GND - Bring out your breadboard from HW#4 Arduino Overview:
Basic Circuits – Lab 4 Serial and OSC (maybe some theory too) Xmedia Spring 2011.
Servo Demonstration In MPIDE, select File > Examples > Servo > Sweep.
Line following tips photoresistors positioned near floor photoresistor circuits © 2011 LWTL faculty team living with the lab.
INTERNET OF EVERYTHING SDU 2016 Week 4. Simple Digital and Analog Inputs  The Arduino’s ability to sense digital and analog inputs allows it to respond.
Photoresistor resistance changes dramatically with light level living with the lab Using Photoresistors with an Arduino © 2011 LWTL faculty team.
Youn-Hee Han, In-Seok Kang {yhhan, Laboratory of Intelligent Networks Advanced Technology Research Center Korea University of Technology.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
Serial Communication RS-232. In order to make two devices communicate, whether they are desktop computers, microcontrollers, or any other form of integrated.
ME 120: Arduino PWM For Loops in Arduino ME 120 Mechanical and Materials Engineering Portland State University
Microcontroller basics Embedded systems for mortals.
ME 120: Arduino Programming Arduino Programming Part II ME 120 Mechanical and Materials Engineering Portland State University
Electronic instrumentation Digitization of Analog Signal in TD
Pulse-Width Modulation: Simulating variable DC output
ME 120: Arduino Programming Arduino Programming Part 1 ME 120 Mechanical and Materials Engineering Portland State University
ME 120: Arduino PWM Breathing LED Code: Implementation on Arduino ME 120 Mechanical and Materials Engineering Portland State University
Pulse Width Modulation Instructor Dr Matthew Khi Yi Kyaw.
ME 120: Photoresistors and Arduino Programming Arduino Programming Case Study: Photoresistor Measurements ME 120 Mechanical and Materials Engineering Portland.
Arduino Programming. THE ARDUINO IS A MICROCONTROLLER – A LOW COST, LOW PERFORMANCE COMPUTER.
Val Manes Department of Math & Computer Science
Arduino Programming Part II
Line Following Tips photoresistor circuits
INC 161 , CPE 100 Computer Programming
Control the color and brightness of an RGB LED with a Potentiometer
Arduino Uno and sensors
Control a motors angular position with a flex sensor
Analog Input through POT
Roller Coaster Design Project
IoT Programming the Particle Photon.
Functions Declarations CSCI 230
Digital Acquisition of Analog Signals – A Practical Guide
Arduino Week 2 Lab ECE 1020 Prof. Ahmadi.
Using Photoresistors with an Arduino
Line Following Tips photoresistor circuit
Topics: Programming Constructs: loops & conditionals Digital Input
Arduino : Introduction & Programming
Programming 2: The Arduino IDE & First Sketches
Arduino Practice: Photoresistors, PWM, Potentiometers, Motors
CTY SAR FCPS Shawn Lupoli, Elliot Tan
Arduino 7 Segment Display Lab
Interrupts.
Pulse-Width Modulation: Simulating variable DC output
Arduino Programming: “if” statements
CTY SAR FCPS Alexander Velikanov
Presentation transcript:

ME 120: User-defined functions: average analog input reading Arduino Programming – Part 5: User-defined functions ME 120 Mechanical and Materials Engineering Portland State University

ME 120: User-defined functions: average analog input reading Overview Continue case study of analog input with a photoresistor Use loop to compute the average of multiple readings Create a user-defined function ❖ Perform a generic task ❖ Reuse in other programs ❖ Encapsulate code to separate the details See on-line reference: ❖ ❖ 2

ME 120: User-defined functions: average analog input reading Voltage divider circuit for photoresistor 3 Why is the fixed resistor on the bottom of the voltage divider?

ME 120: User-defined functions: average analog input reading Display voltage divider output on the serial monitor Connect the voltage divider output to analog pin 0 4 See void setup() { Serial.begin(9600); // Initialize serial port object } void loop() { int reading; float voltage; reading = analogRead(A0); // Read analog input channel 0 voltage = reading*(5.0/1023.0); // and convert to voltage Serial.print(reading); // Print the raw reading Serial.print(” ”); // Make a horizontal space Serial.println(voltage); // Print voltage value }

ME 120: User-defined functions: average analog input reading Average multiple readings 5

ME 120: User-defined functions: average analog input reading Computing the average Basic procedure ❖ Make several readings with analogRead ❖ Add the readings and divide by the number of readings Let n be the number of readings Express with summation notation 6

ME 120: User-defined functions: average analog input reading Evaluation summation with a loop Basic loop structure for n readings Add this code to the basic reading code presented earlier 7 int i,n=10,sensor_pin=0; float ave,sum; sum = 0.0; // initial value of sum for ( i=1; i<=n; i++ ) { sum = sum + analogRead(sensor_pin); } ave = sum/float(n);

ME 120: User-defined functions: average analog input reading Writing a function to compute the average of n readings 8

ME 120: User-defined functions: average analog input reading User-defined functions Functions are reusable code modules ❖ Functions encapsulate details of a task into larger building blocks ❖ Well-written functions can be reused ❖ Functions can accept input (or not) and return output (or not) Reference on the Arduino web site ❖ 9

ME 120: User-defined functions: average analog input reading The average_reading function Write a function to average n readings 10 float average_reading(int sensor_pin) { int i,n=10; float ave,sum; sum = 0.0; // initial value of sum for ( i=1; i<=n; i++ ) { sum = sum + analogRead(sensor_pin); } ave = sum/float(n); return(ave); } Return a float Input is an int Name of the function is average_reading

ME 120: User-defined functions: average analog input reading Use the average_reading function 11 void setup() { Serial.begin(9600); } void loop() { int pot_pin=1; float reading, voltage; reading = average_reading(pot_pin); voltage = reading*(5.0/1023.0); Serial.print(reading); Serial.print(" "); Serial.println(voltage); } A float is returned Input can be any variable name or a constant int

ME 120: User-defined functions: average analog input reading Update the function to allow the number of readings to be a variable 12

ME 120: User-defined functions: average analog input reading Update the average_reading function 13 float average_reading(int sensor_pin, int nave) { int i; float ave,sum; sum = 0.0; // initial value of sum for ( i=1; i<=nave; i++ ) { sum = sum + analogRead(sensor_pin); } ave = sum/float(n); return(ave); } Return a float Both inputs are ints (in this case)

ME 120: User-defined functions: average analog input reading Use the updated average_reading function 14 void setup() { Serial.begin(9600); } void loop() { int n=15, pot_pin=1; float reading, voltage; reading = average_reading(pot_pin,n); voltage = reading*(5.0/1023.0); Serial.print(reading); Serial.print(" "); Serial.println(voltage); } Inputs can be any variable name or a constant int

ME 120: User-defined functions: average analog input reading Summary of user-defined functions You chose the name ❖ Make sure the name is not already used You chose the type of return value You choose the number and type of inputs ❖ Input types are declared in the function definition ‣ float average_reading( int sensor_pin, int nave ) { … } ❖ Input variables are used in the body of the function ❖ When function is called in another section of code, any variable that matches the type of the declared input can be used ‣ reading = average_reading( pot_pin, 15 ); Variables in the function are local ❖ Calling function is not affected by local variables and logic in the function Add this code to the basic reading code presented earlier 15