Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 6: Microcontrollers II

Similar presentations


Presentation on theme: "Week 6: Microcontrollers II"— Presentation transcript:

1 Week 6: Microcontrollers II
Bryan Burlingame 28 Feb 2018

2 Announcements & The Plan for Today™
Homework #3 due up front Homework #4 due next week Read 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.

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

4 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

5 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).

6 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

7 Assignment Operators Recall a basic assignment
float x = 5.0; // stores 5.0 in memory location x x = x ; // 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

8 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) +, -, *, /, |, &, ^, %, <<, >>

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

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

11 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

12 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: or ? 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

13 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

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

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

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

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

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

19 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

20 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

21 // 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

22 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

23 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

24 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); // = 10.00 printsum(x, z); // = 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

25 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

26 References Modular Programming in C math.h


Download ppt "Week 6: Microcontrollers II"

Similar presentations


Ads by Google