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