Week 6: Style and Peripherals

Slides:



Advertisements
Similar presentations
EMS1EP Lecture 8 Pulse Width Modulation (PWM)
Advertisements

Anurag Dwivedi & Rudra Pratap Suman.  Open Source electronic prototyping platform based on flexible easy to use hardware and software.
How to use Arduino By: Andrew Hoffmaster.
Re-programming the Simon Says with Arduino Linz Craig, Brian Huang.
Khaled A. Al-Utaibi  Digital Vs Analog Signals  Converting an Analog Signal to a Digital One  Reading Analog Sensors with the.
Servo Control Using Analog Signal Obtain “analog” input using analogRead().
Working with Arduino: Lesson #2: Variable, Photo, and Force Sensitive Resistors EGN1007.
Introduction.
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.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
Colorado Space Grant Consortium Gateway To Space ASEN 1400 / ASTR 2500 Class #12 Gateway To Space ASEN 1400 / ASTR 2500 Class #12 T-58.
Lecture 9: Microcontrollers – part 1 BJ Furman 29OCT2012.
Microcontroller Hands-on Workshop #3 Ahmad Manshad New Mexico State University Institute of Electrical and Electronics Engineers November 7, 2009.
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.
Lecture 7: Microcontrollers & I/O Bryan Burlingame 14 October 2015.
BM-305 Mikrodenetleyiciler Güz 2015 (3. Sunu) (Yrd. Doç. Dr. Deniz Dal)
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
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,
Lecture 10: Peripherals Bryan Burlingame 04 November 2015.
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.
Week 5: Microcontrollers & Flow Control Bryan Burlingame 2 March 2016.
Embedded systems and sensors 1 Part 2 Interaction technology Lennart Herlaar.
Microcontroller basics Embedded systems for mortals.
Robotics Grant Agreement No LLP UK-LEONARDO-LMP Project acronym: CLEM Project title: Cloud services for E-Learning in Mechatronics Technology.
Lecture 7: Variable Scope B Burlingame March 16, 2016.
Pulse-Width Modulation: Simulating variable DC output
Microcontroller basics Embedded systems for mortals.
Pulse Width Modulation Instructor Dr Matthew Khi Yi Kyaw.
1 Microcontrollers. 2 Programmers work in the virtual world Machinery works in the physical world Microcontrollers connect the virtual and physical world.
Harpeth Hall Jan 2016 Introduction to Arduino Prepared for Harpeth Hall Winterim January 2016.
Microcontrollers & Electronics Basics OR…
BM-305 Mikrodenetleyiciler Güz 2016 (3. Sunu)
Week 4: Microcontrollers & Flow Control
Assist. Prof. Rassim Suliyev - SDU 2017
Microcontroller basics
Microcontroller basics
Arduino Programming Part II
Microcontroller basics
Lab 1: Arduino Basics Topics: Arduino Fundamentals, First Circuit
Arduino Part 1 Topics: Microcontrollers Programming Basics
3.0 ARDUINO WORKSHOP PRESENTATION FOR STUDENTS IN 4º DEGREE OF COMPULSORY SECONDARY EDUCATION 3.0.
Въведение в Arduino.
Arduino - Introduction
Control a motors angular position with a flex sensor
BM-305 Mikrodenetleyiciler Güz 2017 (3. Sunu)
Analog Input through POT
Introduction to Arduinos
Week 5: Microcontrollers
ARDUINO     What is an Arduino? Features 14 Digital I/O pins 6 Analogue inputs 6 PWM pins USB serial 16MHz Clock speed 32KB Flash memory 2KB SRAM.
Lecture 7: Functions Revisited / Analogue Signals
Week 6: Microcontrollers II
What is an Arduino ? Open Source electronic prototyping platform based on flexible easy to use hardware and software.
IoT Programming the Particle Photon.
Arduino Week 2 Lab ECE 1020 Prof. Ahmadi.
Arduino 101 Credit(s):
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Secret Door Knock Detector
Arduino : Introduction & Programming
Programming 2: The Arduino IDE & First Sketches
Arduino Practice: Photoresistors, PWM, Potentiometers, Motors
Sensors and actuators Sensors Resistive sensors
UNIT 5 Analog signals.
Introduction to Arduino
Lecture 8: Arduino 20 March 2019.
Introduction to Arduinos
Pulse-Width Modulation: Simulating variable DC output
Presentation transcript:

Week 6: Style and Peripherals Bryan Burlingame 27 September 2017

The Plan for Today Discuss peripherals on the Experimenter & the Arduino Discuss programming style Homework 3 due. Homework 4 due Oct 11 Extra Credit Due in two weeks (5 exam points) Find an academic journal article related to this class and write a one page response (not a synopsis!) to the article Had planned to talk about blocking and non-blocking code and debouncing switches.

Learning Objectives Discuss the importance of style in code Define scope and variable duration

Programming Style The layout of program directives using white space, syntax, & punctuation to visually highlight a programmer’s intention Good Style is Consistent – A given structure always looks the same Illustrative – The intent of the code is shown by the grammar Clear – Any reasonable programmer should be able to follow the intent

Programming Style Why does it matter? Reduces bugs – the human eye is excellent at detecting patterns Reduces development time Allows others to understand how to approach your code Style is a fundamental part of code documentation https://en.wikipedia.org/wiki/Indent_style 9 different style documented

No style #include "stdio.h" #include "math.h" int main() { double time, max_height, velocity_at_apex, time_at_apex,height,velocity = 0; printf( "%4s %10s %14s\n", "time", "height (m)", "velocity (m/s)" ); printf( "%4s %10s %14s\n", "----", "----------", "--------------" ); for( time = 0.0; time < 49.0; time = time + 0.01 ) { height = -0.12 * pow( time, 4.0 ) + 12.0 * pow( time, 3.0 ) - 380.0; velocity = (-0.48 * pow( time, 3.0 ) + 36.0 * pow( time, 2.0 ); if( height > max_height ) { max_height = height; velocity_at_apex = velocity; time_at_apex = time; } } printf( "Max height %.2lf, at %.3lf hours, while moving %.3lf m/s\n",max_height, time_at_apex, velocity_at_apex); return(0); }

Student Style #include "stdio.h" #include "math.h" int main() { double time, max_height, velocity_at_apex, time_at_apex,height,velocity = 0; printf( "%4s %10s %14s\n", "time", "height (m)", "velocity (m/s)" ); printf( "%4s %10s %14s\n", "----", "----------", "--------------" ); for( time = 0.0; time < 49.0; time = time + 0.01 ) { height = -0.12 * pow( time, 4.0 ) + 12.0 * pow( time, 3.0 ) - 380.0; velocity = (-0.48 * pow( time, 3.0 ) + 36.0 * pow( time, 2.0 ); if( height > max_height ) { max_height = height; velocity_at_apex = velocity; time_at_apex = time; } } printf( "Max height %.2lf, at %.3lf hours, while moving %.3lf m/s\n",max_height, time_at_apex, velocity_at_apex); return(0);

K&R Style #include "stdio.h" #include "math.h" int main() { double time = 0; double max_height = 0; double velocity_at_apex = 0; double time_at_apex = 0; double height,velocity = 0; printf( "%4s %10s %14s\n", "time", "height (m)", "velocity (m/s)" ); printf( "%4s %10s %14s\n", "----", "----------", "--------------" ); for( time = 0.0; time < 49.0; time = time + 0.01 ) { height = -0.12 * pow( time, 4.0 ) + 12.0 * pow( time, 3.0 ) - 380.0; velocity = (-0.48 * pow( time, 3.0 ) + 36.0 * pow( time, 2.0 ); if( height > max_height ) { max_height = height; velocity_at_apex = velocity; time_at_apex = time; } printf( "Max height %.2lf, at %.3lf hours, while moving %.3lf m/s\n",max_height, time_at_apex, velocity_at_apex); return(0);

Allman Style/BSD Style #include "stdio.h" #include "math.h" int main() { double time = 0; double max_height = 0; double velocity_at_apex = 0; double time_at_apex = 0; double height,velocity = 0; printf( "%4s %10s %14s\n", "time", "height (m)", "velocity (m/s)" ); printf( "%4s %10s %14s\n", "----", "----------", "--------------" ); for( time = 0.0; time < 49.0; time = time + 0.01 ) height = -0.12 * pow( time, 4.0 ) + 12.0 * pow( time, 3.0 ) - 380.0; velocity = (-0.48 * pow( time, 3.0 ) + 36.0 * pow( time, 2.0 ); if( height > max_height ) max_height = height; velocity_at_apex = velocity; time_at_apex = time; } printf( "Max height %.2lf, at %.3lf hours, while moving %.3lf m/s\n",max_height, time_at_apex, velocity_at_apex); return(0);

Recall: Definition of a Microcontroller A microcontroller is an integrated circuit which combines: Microprocessor Memory Input/Output Peripherals Ex: Serial console, digital to analog converters

Arduino Peripherals (Uno) Serial (TTL) 6 PWM modules (Digital to Analog (DAC)) 6 ADC (Analog to Digital) SPI bus TWI (I2C) bus

Experimenter Red/Green/Blue (RGB) Light Emitting Diode (LED) 4 x Red LEDs 4 x Active low switches Servoelectric motor header (servo) Piezo-electric speaker (piezo) Potentiometer (POT) Photocell resistor Temperature sensor

Spartronics Experimenter Digital Pin Assignments 13 12 11 10 9 8 7 6 5 4 3 2 1 SCK MISO MOSI SS OC1 ICP AIN1 AIN0 T1 T0 INT1 INT0 TXD RXD LED pwm LED0 LED1 LED2 LED3 red green blue piezo servo SW0 SW1 SW2 SW3 Piezo rectangle is hyperlinked to an image of the actual Experimenter board. The piezo speaker is hyperlinked back to this slide.

Digital Signals Most basic form of sending information to the world On or Off (1 or 0) Active High Logic Think of a light

Experimenter Switches Active Low Unpressed, 5V (HIGH) Pressed, Ground 0V (LOW) A pull-up resistor is required The Arduino has them as a peripheral Pulls the voltage up to high pinMode(SW1, INPUT_PULLUP) Without PULLUP, an unpressed switch is left “floating”, i.e. it has an unknown potential (voltage)

Spartronics Experimenter Digital Pin Assignments 13 12 11 10 9 8 7 6 5 4 3 2 1 SCK MISO MOSI SS OC1 ICP AIN1 AIN0 T1 T0 INT1 INT0 TXD RXD LED pwm LED0 LED1 LED2 LED3 red green blue piezo servo SW0 SW1 SW2 SW3 Piezo rectangle is hyperlinked to an image of the actual Experimenter board. The piezo speaker is hyperlinked back to this slide.

Analog Signals Real world systems tend to be analog Covers the range from 0V to 5V Recall: Microcontrollers work in binary, so how does this work We approximate! Step-wise approximation of a continuous function

Analog Out (PWM) Concept No facility exists on most microcontrollers to directly output an analog voltage (i.e., a voltage that varies continuously over the range of 0 to 5V) Use Pulse Width Modulation (PWM) to approximate Digital outputs are capable of 0V or 5V Over a fraction (ton) of a time period tcycle, keep pin at 5V, the rest of the time, at 0V Average voltage is proportional to ton/tcycle, called the ‘Duty Cycle’ 5V time So far, we’ve talked about digital I/O and analog input. How about getting an analog output?

Arduino analogWrite( ) analogWrite(pin, value); 0  value  255 (How many bits?) 0% duty cycle --> 0 V --> analogWrite(pin, 0); 100% duty cycle --> 5 V --> analogWrite(pin, 255); Must be a PWM pin

Analog Output Example Fade the red LED in, then out const byte ledPin = 3; // red RGB LED on Experimenter const byte FADE_MAX = 255; // max value for setting duty cycle const byte FADE_INC = 5; // increment for changing duty cycle void setup() { pinMode(ledPin, OUTPUT); // note pinMode is not necessary for analog outputs } void loop() int fadeValue = 0; // PWM value // fade in from min to max in increments of 5 points: for(fadeValue = 0 ; fadeValue <= FADE_MAX; fadeValue +=FADE_INC) analogWrite(ledPin, fadeValue); // sets the value (range from 0 to 255): // fade out from max to min in increments of 5 points: for(fadeValue = FADE_MAX; fadeValue >= 0; fadeValue -=FADE_INC) Fade the red LED in, then out duty cycle is incremented then decremented 256 steps 0% to 100%

Arduino analogRead( ) analogRead(pin); Returns an integer 0 – 1024 (how many bits?) 0 V --> analogRead(pin) == 0 5 V --> analogWrite(pin) == 1024 Must be an Analog pin

Spartronics Experimenter Analog Pin Assignments 7 6 5 4 3 2 1 photocell POT temp sensor The rectangle around cells for pins 2, 1, 0 are hyperlinked to an image of the actual Experimenter board. Each respective element is hyperlinked back to this slide.

Resolution All analog to digital and digital to analog systems have a minimum detectable change. This is the resolution of the system On these Arduinos 8 BIT DAC (digital to analog conversion) 5V / 28 = 20 millivolts 10 BIT ADC (analog to digital conversion) 5V / 210 = 5 millivolts

Arduino map map(val, from_low, from_high, to_low, to_high); y = map(x, 0, 256, 0, 1024); Maps a value stored in x which has a range from 0 – 256 and spreads over the range from 0 – 1024 (i.e. 0 stays 0, 128 becomes 512, and 256 becomes 1024) and stores the new value in y

Serial console Allows the microcontroller to provide text feedback to the programmer

Arduino Text Output No screen Serial output RS232 over USB Console built into the Arduino IDE

Example: Serial Console void setup() { // initialize serial comm at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { Serial.println("Wax on"); delay(100); Serial.println(“Whine at Mr. Miyagi"); Serial.println("Wax off"); // place at least a 1 millisecond delay for stability

Analog In with Serial Out #define MAX_DELAY_TIME 1000 // max delay in ms #define MIN_DELAY_TIME 10 // min delay in ms #define MAX_POT_VALUE 855 // max pot reading #define MIN_POT_VALUE 0 // min pot reading const byte POTPIN = 1; // pot output on pin 1 const byte LEDPIN = 6; // blue LED on pin 6 void setup() { pinMode(LEDPIN, OUTPUT); pinMode(POTPIN, INPUT); // documents use of potpin Serial.begin(9600); // init serial comm at 9600 bps } void loop() { static unsigned int potVoltage = 0;// value of pot voltage static unsigned int delay_ms = 0; potVoltage = analogRead(POTPIN); // read pot delay_ms = map(potVoltage, // convert pot value to delay MIN_POT_VALUE,MAX_POT_VALUE, MIN_DELAY_TIME,MAX_DELAY_TIME); Serial.print("sensor = " ); // print to monitor Serial.print(potVoltage); Serial.print(" delay, ms = " ); Serial.println(delay_ms); // print delay and linefeed digitalWrite(LEDPIN, HIGH); // turn the LED on delay(delay_ms); // wait for delay_ms digitalWrite(LEDPIN, LOW); // turn the LED off: Read the POT Note: analog voltage! 0 V  0 5 V  1023 Blink an LED at a rate proportional to the pot voltage Output the pot voltage to the serial monitor Initialize with Serial.begin() Map voltage to delay Write a line with Serial.print or Serial.println So far, we’ve been dealing with digital I/O: just on or off. Now we want to work with a sensor that gives an output signal that can vary in a continuous (non-discrete) way over 0V to 5V. The analog pins can be used as digital pins if desired.

This Week In Lab – Servo and Laser Module Standard RC Servo Diode Laser Module http://ecx.images-amazon.com/images/I/31q1izm4VQL._SL500_AA300_.jpg http://socal.mavin.com/pictures/ss/red_laser_diode_1_2.jpg

RC Servo Construction Operation Wiring Black is ground Red is power White/yellow is control Arduino code #include <servo.h> Create a servo object Attach the servo object to a pin servo.write(position), where position is an integer between 0 and 180

#include <Servo.h> #define POT_PIN 1 #define SERVO 10 #define SERVO_DELAY 10 Servo myservo; //Create servo object to control servo void setup() { myservo.attach(SERVO); // Attaches servo object to pin 10 } void loop() static int pos = 0; // variable to store servo position for(pos = 0; pos < 180; ++pos) //goes from 0 to 180 degrees in 1 degree increments myservo.write(pos); //tell servo where to go delay(SERVO_DELAY); //delay to let servo move to position for(pos = 180; pos > 0; --pos) //returns to 0 from 180 in 1 degree increments myservo.write(pos); delay(SERVO_DELAY); Servo_Knob.pde

References Modular Programming in C http://www.icosaedro.it/c-modules.html math.h http://www.opengroup.org/onlinepubs/007908799/xsh/math.h.html Arduino Home Page. (2017, September 21). Retrieved September 21, 2017, from http://arduino.cc/