Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 4: Microcontrollers & Flow Control

Similar presentations


Presentation on theme: "Week 4: Microcontrollers & Flow Control"— Presentation transcript:

1 Week 4: Microcontrollers & Flow Control
Bryan Burlingame 13 Sept 2017

2 Announcements & The Plan for Today™
Homework #1 & #2 due up front Homework #3 Discuss the flow control statements, if, for, & while Discuss the Arduino and embedded systems Read: finish chapter 5 & all of chapter 6 in the text Had planned to talk about blocking and non-blocking code and debouncing switches.

3 Recall: Boolean Logic And && False True Exclusive Or (Xor) ^^ False

4 Numeric Comparison Operators ! (not) <, <=, >, >=
Changes true to false and false to true <, <=, >, >= Less than, and less than or equal to are different operations Note: !(<) is the same as >= ==, != (not equal) Note: equivalence uses a double ‘=‘, assignment uses a single ‘=‘, be wary = returns the value being assigned Technically, a = b = c = d = 5; is legal. Why? = is performed right to left, so the d is assigned 5, which returns 5. That 5 is assigned to c

5 Examples float b = 17.0; float d = 3.14; float c = 20.0; float e = 33.0; (b < c); //true (b + c); //true (not zero) ((int)(b/c)); //false (is zero, why?) (b < c) && (d > e); //false (b < c) || (d > e); // true (b < c) && (d > e) || (c < e); //true (b < c) && (d > e) || (b + c); //true, why? printf(“%f”, b) && (b + c); //true, why?

6 Flow control These Boolean operations are used along with flow control (or branching) statements to control the flow of a program Decisions True False

7 Flow control if/if else/else – do this, do that, or do the other
switch – choose between a bunch of items for – Do something for some number of times also commonly referred to as iteration i.e. iterating over a range or iterating over a data set while – For as long as some decision is true, keep doing some stuff do .. while – Do something. At the end, if some thing is true, do it again.

8 Selection Structure Overview
Three kinds of selections structures if (also called, ‘single-selection’) if condition is true Perform action if condition is false, action is skipped, program continues if/else (also called, ‘double-selection’) else (if condition is false) Perform a different action (this will be skipped if condition is true) switch (also called ‘multiple-selection’) Allows selection among many actions depending on the integral value of a variable or expression

9 Single Selection IF - Flowchart
connector flow line action symbol decision symbol TRUE Print “You’re speeding” Speed > 65 FALSE If the speed is greater than 65, take the action to print a message. If the speed is not greater than 65, just keep going on in the program. What does Speed > 65 evaluate to if Speed == 72 ? The symbol > is a Relational Operator. The Expression “speed > 65” evaluates to 1 if true, 0 if false

10 if - example int speed = 5; int b = 4; if( speed > 65 ) { // do everything until the closing } printf( “You are speeding!\n” ); } // technically, when one statement is between // the curly braces, the braces are optional. // Even so, don’t omit them

11 Double-Selection IF - Flowchart
Print “Within speed limit” Print “Over speed limit” Speed > 65 FALSE TRUE Note that if Speed > 65 is true, then the message “Over speed limit is printed”, and then the program continues. If Speed > 65 is not true, then the message “Within limit” is printed.

12 if - example int speed = 5; int b = 4; if( speed > 65 ) { // do everything until the closing } printf( “You are speeding!\n” ); } // technically, when one statement is between // the curly braces, the braces are optional. // Even so, don’t omit them else { // note the indentation. printf( “Speed is within legal limits\n” ); }

13 if - example int speed = 5; int b = 4; if( speed > 65 ) { // do everything until the closing } printf( “You are speeding!\n” ); } // technically, when one statement is between // the curly braces, the braces are optional. // Even so, don’t omit them else if( speed < 65 ) { // note the indentation. printf( “Speed is within legal limits\n” ); } else { printf( “Speed is precisely 65\n” );

14 for Loop Structure – Flow Chart
initialization T F Terminal decision statement Iteration operation Initializes the loop control variable: ex. a = 0; Tests the loop control variable to see if it is time to quit looping: ex. a < 5; Increments the loop control variable: ex. ++a

15 for Loop Structure – Flow Chart
initialization T F Terminal decision statement Iteration operation for( a = 0; a < 5; ++a ) { //for( initialization, termination, iteration) printf( “%d\n”, a ); }

16 while Loop - Flowchart View
Statement is executed while condition is true Note that the condition must first be true in order for the statement to be executed even once statement TRUE FALSE condition

17 while Loop - Flowchart View
statement TRUE FALSE condition while( a < 5 ) { printf( “%d\n”, a ); ++a; }

18 What is a Microcontroller?
A small computer usually implemented on a single IC that contains a central processing unit (CPU), some memory, and peripheral devices such as counter/timers, analog-to-digital converters, serial communication hardware, etc. ATmega328 the ‘brain’ of the Arduino

19 Where are Microcontrollers Used?
Everywhere! Car Phone Toothbrush Microwave oven Copier Television PC keyboard Appliances

20 The Arduino Platform http://arduino.cc/
USB jack Microcontroller power jack Voltage regulator Pwr/GND Pins ICSP Header Reset Button Power LED Pin 13 LED Rx + Tx LEDs Digital Pins Atmel ATmega328 microcontroller 14 digital I/O pins 6 with PWM 6 analog I/O pins 32 kB (-2 kB) Flash memory 2 kB RAM 1 kB EEPROM 16 MHz clock $22 - $30 built $13 ‘breadboardable’ FTDI USB chip Analog Pins

21 Fundamental Flow of an Arduino Program
Start Setup Loop End

22 Programming the Arduino
/* Blue_LED_button_cntrl1 - turns on blue LED when SW0 on Experimenter board is pressed, off otherwise */ /* pin assignments */ const byte RGB_blue_pin = 6; const byte SW0_pin = 12; /* configure pins */ void setup() { pinMode(RGB_blue_pin, OUTPUT); pinMode(SW0_pin, INPUT_PULLUP); } /* loop forever */ void loop() if(digitalRead(SW0_pin) == LOW) digitalWrite(RGB_blue_pin, HIGH); else digitalWrite(RGB_blue_pin, LOW); An arduino program == ‘sketch’ Must have: setup() loop() configures pin modes and registers runs the main body of the program forever like while(1) {…} Where is main() ? Arduino simplifies things Does things for you This slide is meant as a quick overview of an Arduino sketch. We’ll unpack and describe the pieces in more detail later. Think of setup() as what you would do when you first get in a rental car or a car you have borrowed from a friend: adjust the mirrors, adjust the steering wheel, the seats, change the stereo channel settings, etc. The car can be driven by anyone, but everyone likes to customize it for their comfort. main() {    init();    setup();    while (1)       loop(); } And the reason that they do it this way is so it's guaranteed that init() gets called, which is critical for Arduino initialization.

23 Using setup() A digital pin can either be an output or an input Output
const byte RGB_blue_pin = 6; const byte SW0_pin = 12; /* configure pins */ void setup() { pinMode(RGB_blue_pin, OUTPUT); pinMode(SW0_pin, INPUT_PULLUP); } A digital pin can either be an output or an input Output your program determines what the voltage on a pin is (either 0V (LOW or logic 0) or 5V (HIGH or logic 1) Information is sent out Input the world outside the microcontroller determines the voltage applied to the pin Information is taken in pinMode() sets whether a pin is an input, input_pullup or an output ledPin  byte constant assigned the value of 13 OUTPUT is a macro defined constant Which has the value 1 INPUT is a macro …  ? Emphasize that the distinction between inputs and outputs. As an example, you might have the class envision that the front row of students in the class are digital pins (everyone else are internals elements of the microcontroller). Designate one student as an OUTPUT. Have the rest of the students (in unison) tell the OUTPUT student to raise his or her hand. (Have the designated ‘OUTPUT’ follow the command!). Then have the rest of the class (in unison) tell the ‘OUTPUT’ to put his or her hand down. Thus, the microcontroller has sent information ‘out’, and the pin designated as an OUTPUT has responded accordingly. Similarly designate one student in the front row as an INPUT. Have the rest of the class ask the INPUT student what the state of the room light is (is it on or off?). Have the INPUT student report to the rest of the class the state of the room light. Explain the declaration of ledPin as a const byte variable. Explain the advantage over #define ledPin 13 Digital HIGH = = 5V (usually taken as logical 1) Digital LOW = = 0V (usually taken as logical 0) wiring.h in arduino/hardware/cores/arduino #DEFINEs OUTPUT as 0x1 and INPUT as 0x0 where can you find out about the commands, etc?

24 Digital I/O Example - loop() Algorithm
Refine the pseudocode, cont.: loop forever (use function loop()) If button is not pressed: voltage on button pin 12 will be _______ make pin 6 voltage low (LED will go off or stay off) If button is pressed: make pin 6 voltage high (LED will go on or stay on) high (5V) low (0V) void loop() { if(digitalRead(SW0_pin) == LOW) digitalWrite(RGB_blue_pin, HIGH); } else digitalWrite(RGB_blue_pin, LOW);

25 Digital I/O Example - Modification
/* Blue_LED_button_cntrl1 - turns on blue LED when SW0 on Experimenter board is pressed, off otherwise */ /* pin assignments */ const byte RGB_blue_pin = 6; const byte SW0_pin = 12; /* configure pins */ void setup() { pinMode(RGB_blue_pin, OUTPUT); pinMode(SW0_pin, INPUT_PULLUP); } /* loop forever */ void loop() if(digitalRead(SW0_pin) == LOW) digitalWrite(RGB_blue_pin, HIGH); else digitalWrite(RGB_blue_pin, LOW); Modify Arduino program, so that LED is on until button is pressed, then turns off How? Pin assignments? setup()? Need to turn on the LED! loop()? Swap values of second argument in digitalWrite calls Pin assignments stay the same - no change to the external hardware or its connection to the Arduino setup() - add a line to turn on the LED loop() - swap the arguments in the digitalWrite() function calls: if button is pressed, then LOW, else, HIGH

26 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 green blue red 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.

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

28 References Modular Programming in C math.h


Download ppt "Week 4: Microcontrollers & Flow Control"

Similar presentations


Ads by Google