Week 6: Microcontrollers II

Slides:



Advertisements
Similar presentations
ARDUINO CLUB What we’re really doing. BASICS The setup() and loop() functions.
Advertisements

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)
1 Introduction to Coding. 2 Example Codes A lot of example codes are given with Arduino IDE A code can often be based on a previous example rather than.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
Embedded Programming and Robotics
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
chipKit Sense Switch & Control LED
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
Khaled A. Al-Utaibi  The Push Button  Interfacing Push Buttons to Arduino  Programming Digital Inputs  Working with “Bouncy”
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Code The Arduino Environment.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Week 8: Decisions Bryan Burlingame 21 October 2015.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Lecture 10: Peripherals Bryan Burlingame 04 November 2015.
Microcontrollers, Microcomputers, and Microprocessors
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.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Embedded systems and sensors 1 Part 2 Interaction technology Lennart Herlaar.
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?
:Blink Blink: Er. Sahil Khanna
Arduino “Getting Started” Instructor : Dr Matthew Miss Khin Yi Kyaw
ME 120: Arduino Programming Arduino Programming Part 1 ME 120 Mechanical and Materials Engineering Portland State University
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
1 Introduction to Coding. 2 Example Codes A lot of example codes are given with Arduino IDE A code can often be based on a previous example rather than.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Prof. Jeremy.
Harpeth Hall Jan 2016 Introduction to Arduino Prepared for Harpeth Hall Winterim January 2016.
Bill Tucker Austin Community College COSC 1315
Lecture 3: Logic Bryan Burlingame 06 Sept 2017.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Sparkfun Electronics ATtiny85 Arduino Quick Reference Sheet
Week 4: Microcontrollers & Flow Control
Assist. Prof. Rassim Suliyev - SDU 2017
Week 6: Style and Peripherals
Val Manes Department of Math & Computer Science
Microcontroller basics
Arduino Programming Part II
UTA010 : Engineering Design – II
Get Your Project Started with Arduino
Lecture 7: Repeating a Known Number of Times
UCD ElecSoc Robotics Club 2017/2018
INC 161 , CPE 100 Computer Programming
Arduino.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Introduction to Arduino Microcontrollers
Introduction to C Programming Language
Introduction to Arduinos
Week 5: Microcontrollers
IoT Programming the Particle Photon.
1 Code
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Introduction to C++ Programming
Structured Program
Arduino programs Arduino toolchain Cross-compilation Arduino sketches
C Programming Getting started Variables Basic C operators Conditionals
Lab #1: Getting Started.
DATA TYPES There are four basic data types associated with variables:
Setting up a basic program with Arduino
Introduction to Arduinos
UNIT 1 First programs.
Introduction to Arduino IDE and Software
Interrupts.
Presentation transcript:

Week 6: Microcontrollers II Bryan Burlingame 28 Feb 2018

Announcements & The Plan for Today™ Homework #3 due up front Homework #4 due next week Read Pull-up Resistors https://learn.sparkfun.com/tutorials/pull-up-resistors Read Chapter 5 Office Hours/Open Lab Eng 213, 4:30 PM – 7:00 PM Had planned to talk about blocking and non-blocking code and debouncing switches.

Announcements & The Plan for Today™ Introduce some new operators Boolean Logic Complex assignments Increment/Decrement Discuss the Arduino and embedded systems Pull-up resistors and switches Discuss variable duration and the static keyword Had planned to talk about blocking and non-blocking code and debouncing switches.

Boolean Logic Introduced by George Boole The Mathematical Analysis of Logic (1847) Also referred to as Boolean Algebra Fundamental to the development of digital logic Claude Shannon’s Switching Algebra (1937) Built on three fundamental operations And Or Not

Boolean Logic And && False True An And expression is true if and only if all operands are true Ex: (5 < 17) && (92 > 14) is true. (5 > 17) && (92 > 14) is false Or || False True An Or expression is true if any operand is true Ex: (5 < 17) || (92 > 14) is true. So is (5 > 17) || (92 > 14). Not ! False True Inverts the logic. False becomes True and True becomes False !((5 < 17) || (92 > 14)) is false. So is (5 > 17) || !(92 > 14).

Boolean Logic - Examples By convention, 0 is false. Nonzero is true. ( 6 < 14 ) && ( 9 > 5 ) 5 || (19 > 45) && (57 < 108) (My name is Bryan) && (This is ME30) (name == “Bryan”) && (class == “ME30”) !(printf(“Hello World”)) 42

Assignment Operators Recall a basic assignment float x = 5.0; // stores 5.0 in memory location x x = x + 12.0; // retrieves 5.0, adds 12, stores 17 in x (note: this is not algebra) There are number of short-form assignment operators for this common Retrieve Operate Store

Assignment operators x += 12.0; // x = x + 12.0 x -= y; // x = x – y x *= z + 2; // x = x * (z + 2) (note the implied parenthesis) x /= z + 2; // x = x / (z + 2) Works for all operators we’ve seen (and some we haven’t) +, -, *, /, |, &, ^, %, <<, >>

Increment / Decrement x += 1; is also very common (increment) Given: x++; or ++x; // x = x + 1 Similar function but different return values Given: int x = 5; printf( “%i\n”, ++x ); printf( “%i\n”, x++ ); What prints?

Increment / Decrement 6 Prints. ++x adds 1 and then returns the new value. x++ returns the original value and then adds 1. ++x saves one operations. Sometimes mildly faster There are also --x and x-- which work the same, but with subtraction (i.e. decrement)

Recall: A complete Arduino Sketch // Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keeps the count through loops digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED ++blue_count; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop Recall: A complete Arduino Sketch with integrated pseudocode Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

Recall: A comment about “Magic Numbers” // Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; Recall: A comment about “Magic Numbers” It is considered poor form to litter numbers throughout your code Typos with numbers can generate bugs which are wickedly difficult to see Which is correct: 3.1415926535898 or 3.1415926353898 ? Better: Use constant variables to hold any number more complex than one or zero The compiler will catch any typo and you only need to get it correct in one place

Note the symbolic constants const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keeps the count through loops digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED ++blue_count; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop Note the symbolic constants Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

Pull-up Resistors Given a digital pin on an Arduino, what value is read on a digitalRead ?

Pull-up Resistors This pin is floating (not connected to anything). It may be high or low, unpredictably. Let’s fix this

Pull-up Resistors Connect a switch What value is read, when the switch is pressed? When the switch is no pressed?

Pull-up Resistors Connect a switch Let’s add a resistor to high This is called a “pull-up resistor”

Pull-up Resistors The Arduino has pull up resistors built-in, but we must turn them on pinMode(pin, INPUT_PULLUP);

BLUE_LED  byte constant assigned the value of 6 pinMode(PIN, MODE); sets whether a pin is an input, input_pullup or an output BLUE_LED  byte constant assigned the value of 6 OUTPUT, INPUT, and INPUT_PULLUP are constants defined in Arduino Output are signals generated by the Arduino For example, the voltage necessary to turn on an LED Input are signals read by the Arduino INPUT_PULLUP is frequently used by switches Turns on a pullup resistor inside the microcontroller connected to high When the switch (button) is pressed, it drives the pin low This is an active low signal /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } Notice how the switch is wired

Returns the value detected on a pin (HIGH or LOW) Digital Input digitalRead(pin); Returns the value detected on a pin (HIGH or LOW) Recall: LOW is returned, when a button is pressed on the experimenter board // Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keeps the count through loops digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED blue_count++; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

// Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keeps the count through loops digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED ++blue_count; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop When the switch is pressed, specifically when a LOW (0V or ground) is detected on the pin connected to switch 0, perform the tasks between the curly braces { } Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

Variable Duration Recall block scope x, y, z are valid in main #include <stdio.h> /////////////////////////////////////// void printsum(float a, float b); /////////////////////////////////////// int main( void ) { float x = 4.5; float y = 5.5; float z = 6.5; printsum(x, y); printsum(x, z); } void printsum(float a, float b) float total = a + b; static float grand = 0; grand += total; printf("Total:\t\t%.2f\n", total); printf("Grand total:\t%.2f\n",grand); Recall block scope x, y, z are valid in main a, b, total, grand are valid in printsum

Variable Duration #include <stdio.h> /////////////////////////////////////// void printsum(float a, float b); /////////////////////////////////////// int main( void ) { float x = 4.5; float y = 5.5; float z = 6.5; printsum(x, y); printsum(x, z); } void printsum(float a, float b) float total = a + b; static float grand = 0; grand += total; printf("Total:\t\t%.2f\n", total); printf("Grand total:\t%.2f\n",grand); Usually, a variable ceases to exist when it goes out of scope The static keyword changes this

Variable Duration Static preserves the variable across function calls #include <stdio.h> /////////////////////////////////////// void printsum(float a, float b); /////////////////////////////////////// int main( void ) { float x = 4.5; float y = 5.5; float z = 6.5; printsum(x, y); // 4.5 + 5.5 = 10.00 printsum(x, z); // 4.5 + 6.5 = 11.00 } void printsum(float a, float b) float total = a + b; static float grand = 0; grand += total; printf("Total:\t\t%.2f\n", total); printf("Grand total:\t%.2f\n",grand); Static preserves the variable across function calls The declaration line only triggers once

Since the loop routine restarts when it ends usual variables reset on each loop To keep the value of the variable across loops, use the static keyword In this example, blue_count keeps the count through loops // Symbolic Constants///////////////////////////////////////////////////// const int BLUE_LED = 6; const int SWITCH0 = 12; const int COMM_SPEED = 9600; const int SLEEP_DURATION = 250; /////////////////////////////////////////////////////////////////////////// void setup() // Initialize the system for use { pinMode(BLUE_LED, OUTPUT); // Set the blue LED pin to output pinMode(SWITCH0, INPUT_PULLUP); // Set switch 0 to input with pullup Serial.begin(COMM_SPEED); // Start the serial console } ///////////////////////////////// //////////////////////////////////////// void loop() // Primary system loop static int blue_count = 0; // Create an int to count blues, keeps the count through loops digitalWrite(BLUE_LED, LOW); // turn off the blue LED if(LOW == digitalRead(SWITCH0)) // if the button is pressed digitalWrite(BLUE_LED, HIGH); // turn on the blue LED ++blue_count; // add one to the running blue count Serial.print("Blue Count: "); // print "Blue Count:" Serial.println(blue_count); // print the actual count and a newline delay(SLEEP_DURATION); // sleep for SLEEP_DURATION } // Go back to the start of loop Introduce the concept of a complete Arduino sketch, with two main parts: a setup function which runs once and a loop function which repeats forever

References Modular Programming in C http://www.icosaedro.it/c-modules.html math.h http://www.opengroup.org/onlinepubs/007908799/xsh/math.h.html