Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 6: Style and Peripherals

Similar presentations


Presentation on theme: "Week 6: Style and Peripherals"— Presentation transcript:

1 Week 6: Style and Peripherals
Bryan Burlingame 27 September 2017

2 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.

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

4 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

5 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 9 different style documented

6 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 ) { height = * pow( time, 4.0 ) * pow( time, 3.0 ) ; velocity = (-0.48 * pow( time, 3.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); }

7 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 ) { height = * pow( time, 4.0 ) * pow( time, 3.0 ) ; velocity = (-0.48 * pow( time, 3.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);

8 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 ) { height = * pow( time, 4.0 ) * pow( time, 3.0 ) ; velocity = (-0.48 * pow( time, 3.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);

9 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 ) height = * pow( time, 4.0 ) * pow( time, 3.0 ) ; velocity = (-0.48 * pow( time, 3.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);

10 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

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

12 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

13 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.

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

15 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)

16 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.

17 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

18 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?

19 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

20 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%

21 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

22 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.

23 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

24 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

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

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

27 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

28 Analog In with Serial Out
#define MAX_DELAY_TIME // max delay in ms #define MIN_DELAY_TIME // min delay in ms #define MAX_POT_VALUE // max pot reading #define MIN_POT_VALUE // 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.

29 This Week In Lab – Servo and Laser Module
Standard RC Servo Diode Laser Module

30 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

31 #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

32 References Modular Programming in C math.h Arduino Home Page. (2017, September 21). Retrieved September 21, 2017, from


Download ppt "Week 6: Style and Peripherals"

Similar presentations


Ads by Google