ME 120: Arduino PWM Breathing LED Code: Implementation on Arduino ME 120 Mechanical and Materials Engineering Portland State University

Slides:



Advertisements
Similar presentations
Khaled A. Al-Utaibi Interfacing an LED The Light Emitting Diode (LED) Applications DC Characteristics & Operation Interfacing to.
Advertisements

EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
EMS1EP Lecture 8 Pulse Width Modulation (PWM)
Lab7: Introduction to Arduino
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)
Embedded Sumo 1T4 – 1T5 UTRA.
Digital & Analog Inputs. Review Fundamental parts of an Arduino program are … Setting output types using pinMode. Declaring variables Can write a digital.
Introduction to Arduino Programming January MER-421:Mechatronic System Design.
Intro to Programming and Microcontrollers. Activity Group into pairs and sit back-to-back. Pick one person who is going to draw. The other person will.
Living with the Lab Using servos with an Arduino EAS 199A Fall 2011.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
Introduction to Arduino Prepared by R. Lamond.  “Arduino is an open-source electronics prototyping platform based on flexible, easy- to-use hardware.
Basic Circuits – Lab 2 Arduino and Sensors Xmedia Spring 2011.
ARDUINO PROGRAMMING Working with the Arduino microcontroller.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
Arduino John Marcoux Christopher Lesch Thomas Dodge Unless otherwise noted, all information and pictures came from:
Basic Circuits – Lab 2 Arduino and Sensors
chipKit Sense Switch & Control LED
Khaled A. Al-Utaibi  The Push Button  Interfacing Push Buttons to Arduino  Programming Digital Inputs  Working with “Bouncy”
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.
Introduction to Sensor Technology Week Five Adam Taylor
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.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Good LED Circuit 5V0 GND. What Voltage Does Meter See? Answer: 5 V.
Periods of Coefficients of Ehrhart Quasipolynomials of 2- Dimensional Polytopes By Christopher O’Neill and Anastasia Chavez.
Sec How Arithmetic Sequences Work?
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.
Arduino Circuits and Code. int ledPin = 9; void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, LOW); delay(1000); digitalWrite(ledPin,
Water Flow GROUP A. Analogue input voltage results: Motor Input voltage( V) pin 12 Analogue input voltage (V) Display number
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.
ME 120: Arduino PWM For Loops in Arduino ME 120 Mechanical and Materials Engineering Portland State University
ME 120: User-defined functions: average analog input reading Arduino Programming – Part 5: User-defined functions ME 120 Mechanical and Materials Engineering.
Microcontroller basics Embedded systems for mortals.
1 Introduction to Haptics Introduction to the Hapkit board Allison M. Okamura Stanford University.
ME 120: Arduino Programming Arduino Programming Part II ME 120 Mechanical and Materials Engineering Portland State University
Programming in Arduino Materials:Arduino Board Casperelectronics Pre Pres. Notes Photos from workshop?
INTERNET OF EVERYTHING SDU 2016 Week 12. Remotely Controlling Devices  interact with almost any device that uses some form of remote control  TVs, audio.
Arduino + Bluetooth TYWu. Connection Arduino + Bluetooth Module.
:Blink Blink: Er. Sahil Khanna
Istituto Tecnico Industriale A.Monaco EURLAB Control a Servo Motor If you want to swing an robot arm or … steer a robot you need a special motor (Servo).
Introduction to Programming the Arduino Dr Gaw 3/21/14.
Pulse-Width Modulation: Simulating variable DC output
Session 6 John Wolf (WolfDenElectronics.com). Objectives – We’ll Learn  Code to Voice  PIR sensors – motion detection  Quadrature Encoders  Audio.
ME 120: Arduino Programming Arduino Programming Part 1 ME 120 Mechanical and Materials Engineering Portland State University
Arduino Application: Speed control of small DC Motors
ME 120: Photoresistors and Arduino Programming Arduino Programming Case Study: Photoresistor Measurements ME 120 Mechanical and Materials Engineering Portland.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Microcontroller basics
If you want to swing an robot arm or …
Microcontroller basics
Exploring lighting effects with LED’s and Arduino
Arduino Programming Part II
Microcontroller basics
Get Your Project Started with Arduino
Exploring more with LED’s and Arduino
Arduino Part 1 Topics: Microcontrollers Programming Basics
3.0 ARDUINO WORKSHOP PRESENTATION FOR STUDENTS IN 4º DEGREE OF COMPULSORY SECONDARY EDUCATION 3.0.
Въведение в Arduino.
Week 6: Microcontrollers II
Arduino Application: Speed control of small DC Motors
Arduino Week 2 Lab ECE 1020 Prof. Ahmadi.
C Programming Getting started Variables Basic C operators Conditionals
EXPRESSIONS, PAUSES AND SOUNDS
The while Looping Structure
Setting up a basic program with Arduino
SAURABH GINGADE.
Pulse-Width Modulation: Simulating variable DC output
Arduino Programming: “if” statements
Presentation transcript:

ME 120: Arduino PWM Breathing LED Code: Implementation on Arduino ME 120 Mechanical and Materials Engineering Portland State University

ME 120: Arduino PWM Overview We have a mathematical model of brightness We can control LED brightness with PWM The next step is to develop the Arduino code 2

ME 120: Arduino PWM Review: Mathematical model 1.Choose V min and V max 2.Choose t 2, t 3 and t 4 3.Use algebra to compute a in, b in, a ex and b ex 4.Formulas for v in (t) and v ex (t) are used to specify voltage to the LED 3

ME 120: Arduino PWM Implementation Preview Code in the loop function creates the repeated pattern. 4

ME 120: Arduino PWM A simplistic model with fixed delays Constant levels do not simulate breathing Fixed delays do not allow PWM signal to vary with time 5 int LED_pin = 11; void setup() { pinMode(LED_pin, OUTPUT); } void loop() { int vin=40, vpause=250, vex=20; analogWrite(LED_pin, vin); delay(2000); analogWrite(LED_pin, vpause); delay(500); analogWrite(LED_pin, vex); delay(2500); }

ME 120: Arduino PWM Working with the on-board timer One Arduino timer can be read with two commands ❖ millis() returns the number of milliseconds ❖ micros() returns the number of microseconds Values returned by millis() and micros() are relative to the last time the Arduino was restarted Values returned by millis() and micros() can be very large. Store the return values in unsigned long variables. 6

ME 120: Arduino PWM A simplistic model using the timer What happens when tm > t4 ? How can we account for the cyclic nature of breathing with a timer that increases continuously? 7 int LED_pin = 11; void setup() { pinMode(LED_pin, OUTPUT); } void loop() { int v, vin=40, vpause=250, vex=20; unsigned long tm, t2=2000, t3=2500, t4=5000; tm = millis(); if ( tm<=t2 ) { v = vin; } else if ( tm<=t3 ) { v = vpause; } else if ( tm<=t4 ) { v = vex; } else { v = 0; } analogWrite(LED_pin,v); Serial.print(tm); Serial.print(" "); Serial.println(v); }

ME 120: Arduino PWM The breathing pattern repeats The timer count returned by millis() increases indefinitely The breathing pattern repeats every t 4 milliseconds 8

ME 120: Arduino PWM The modulo operator returns the remainder Arduino (C language) operator % is called modulo i%j returns the remainder of dividing i by j Examples: If t4 is the time to complete one breathing cycle, then millis()%t4 is the time (in milliseconds) in the current cycle 9 iji % j

ME 120: Arduino PWM Use % to extract cycle time from millis() Pattern repeats because tm%t4 is time from start of current cycle A subtle problem remains because millis() may not start at zero 10 int LED_pin = 11; void setup() { pinMode(LED_pin, OUTPUT); } void loop() { int v, vin=40, vpause=250, vex=20; unsigned long t, tm; unsigned long t2=2000, t3=2500, t4=5000; tm = millis(); // Current timer t = tm % t4; // Time from start of current cycle if ( t<=t2 ) { v = vin; } else if ( t<=t3 ) { v = vpause; } else if ( t<=t4 ) { v = vex; } else { v = 0; } analogWrite(LED_pin,v); Serial.print(tm); Serial.print(" "); Serial.print(t); Serial.print(" "); Serial.println(v); }

ME 120: Arduino PWM Correct for non-zero millis() at start-up Subtracting tstart from current millis() reading gives the timer value measured from the last reset or power-up 11 int LED_pin = 11; unsigned long tstart; // Global variable void setup() { pinMode(LED_pin, OUTPUT); tstart = millis(); // Time at start of program } void loop() { int v, vin=40, vpause=250, vex=20; unsigned long t, tm; unsigned long t2=2000, t3=2500, t4=5000; tm = millis() - tstart; // Count from program start t = tm % t4; // Time in current cycle if ( t<=t2 ) { v = vin; } else if ( t<=t3 ) { v = vpause; } else if ( t<=t4 ) { v = vex; } else { v = 0; } analogWrite(LED_pin,v); Serial.print(tm); Serial.print(" "); Serial.print(t); Serial.print(" "); Serial.println(v); }

ME 120: Arduino PWM Convert to time in seconds It will be easier to evaluate the breathing formulas in floating point arithmetic than using long int s. Convert t, t2, t3 and t4 to time in seconds 12 int LED_pin = 11; unsigned long tstart; // Global variable void setup() { pinMode(LED_pin, OUTPUT); tstart = millis(); // Time at start of program } void loop() { int v, vin=40, vpause=250, vex=20; unsigned long tm; float t, t2=2.0, t3=2.5, t4=5.0; tm = millis() - tstart; // Count from program start t = tm%(long(t4*1000)); // Time in current cycle (ms) t = t/1000.0; // Time in current cycle (sec) if ( t<=t2 ) { v = vin; } else if ( t<=t3 ) { v = vpause; } else if ( t<=t4 ) { v = vex; } else { v = 0; } analogWrite(LED_pin,v); Serial.print(tm); Serial.print(" "); Serial.print(t); Serial.print(" "); Serial.println(v); }

ME 120: Arduino PWM Finishing touches Modify the code given in these slides ❖ Add variables and code to evaluate the two exponential functions: one for inhale, one for exhale ❖ Compute coefficients a in, b in, a ex and b ex and update code ❖ Make sure t2, t3, and t4 in the code are consistent with the model equations for the exponentials (Consistency check) Experiment with the system and adjust voltage levels to get the desired brightness Experiment with the system and adjust time markers t2, t3, t4 to get desired breathing speed Any change in voltage level or time markers will require recalculation of a in, b in, a ex and b ex 13