EET 2259 Unit 7 Case Structures; Sequence Structures

Slides:



Advertisements
Similar presentations
A. Virtual Instruments (VIs)
Advertisements

Previously… We created a simulated temperature reader which alerts if too hot or too cold… Download the solved practice to keep in sync: Thermostat.vi.
Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 2 Virtual Instruments Read Bishop, Chapter 2. Lab #2 and Homework #2 due next week. Quiz #1 next week.
Based on LabVIEW 2011 Student Edition
Loops in LabVIEW (while,for and case)
De la Rosa-Pohl ECE 1100 Introduction to Engineering Intro to LabVIEW: Programming for Symon University of Houston Diana de la Rosa-Pohl Len Trombetta.
Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 5 Loops  Read Bishop, Sections 5.1 and 5.2.  Lab #5 and Homework #5 due next week.  Exam #1 next.
True or false A variable of type char can hold the value 301. ( F )
CS320n –Visual Programming LabVIEW Foundations. Visual ProgrammingLabVIEW Foundations2 What We Will Do Today Hand back and review the midterm Look at.
Loops A.About While Loops B.About For Loops C.About Case structures D.Sequence Structures E.Formula Node You Will Learn:
Introduction to LabVIEW Seth Price Department of Chemical Engineering New Mexico Tech Rev. 10/5/14.
Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 4 SubVIs  Read Bishop, Chapter 4.  Lab #4 and Homework #4 due next week.
Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 8 Other Structures; Local Variables  Read Bishop, Sections 5.6 to  Lab #8 and Homework #8 due.
Virtual Instrumentation With LabVIEW. Course Goals Understand the components of a Virtual Instrument Introduce LabVIEW and common LabVIEW functions Create.
LabVIEW an Introduction
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
LabView instrumentoinnissa, 55492, 3op Labview in instrumentation Lecturer: Heikki Ojala, room C204a
CS320n –Visual Programming More LabVIEW Control Structures.
Samuel Alberg Thrysøe, PhD, PostDoc, Kontakt info: Tlf:
EET 2259 Unit 7 Case Structures; Sequence Structures  Read Bishop, Sections 5.4 and 5.5.  Lab #7 and Homework #7 due next week.  Quiz #3 next week.
Creating, Editing, & Debugging a VI A.How to Create VIs B.How to Edit VIs C.How to Debug VIs You Will Learn:
Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 3 Editing and Debugging VIs  Read Bishop, Chapter 3.  Lab #3 and Homework #3 due next week.  Quiz.
Digital Electronics and Computer Interfacing Tim Mewes 4. LabVIEW - Advanced.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Design of Bio-Medical Virtual Instrumentation Tutorial 2.
Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 9 Arrays  Read Bishop, Sections 6.1 to 6.3.  Homework #9 and Lab #9 due next week.  Quiz #5 next.
Workshop 2 of 4 Welcome!. Survey While You Wait ni.com/duke >> Recent Documents >> LabVIEW Workshop Participation Survey.
Digital Electronics and Computer Interfacing Tim Mewes 2. LabVIEW Basics part II.
Visual Basic.NET Windows Programming
EET 2259 Unit 13 Strings and File I/O
EGR 2261 Unit 11 Pointers and Dynamic Variables
EET 2259 Unit 4 SubVIs Read Bishop, Chapter 4.
EE 200 Design Tools Laboratory 14
EET 2259 Unit 11 Charts and Graphs
EGR 2261 Unit 5 Control Structures II: Repetition
Repetition Structures Chapter 9
Containers and Lists CIS 40 – Introduction to Programming in Python
Topics Introduction to Repetition Structures
EGR 2261 Unit 4 Control Structures I: Selection
EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2.
EET 2259 Unit 3 Editing and Debugging VIs
“Sequence” (Sıralı) Yapılar
Programming Fundamentals
Topics Introduction to Repetition Structures
Course Learning Map Lesson # Lesson Title Lesson 3 Lesson 2 Lesson 1
Introduction to LabVIEW
Lab 2 : Structures Muhammad Zaigham Abbas Shah DIGITAL INSTRUMENTATION SYSTEMS.
Doing some Boolean: On/Off
Topics Introduction to File Input and Output
Conditions and Ifs BIS1523 – Lecture 8.
EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2.
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Introduction to Primitive Data types
EET 2259 Unit 6 Shift Registers
Introduction to Problem Solving and Control Statements
Structures- case, sequence, formula node
Structured Program Development in C++
Topics Introduction to Repetition Structures
EET 2259 Unit 9 Arrays Read Bishop, Sections 6.1 to 6.3.
Introduction to LabVIEW
EET 2259 Unit 13 Strings and File I/O
Topics Introduction to File Input and Output
EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2.
EET 2259 Unit 11 Charts and Graphs
EET 2259 Unit 2 Virtual Instruments
EET 2259 Unit 4 SubVIs Read Bishop, Chapter 4.
EET 2259 Unit 6 Shift Registers
EET 2259 Unit 8 Other Structures; Local Variables
EET 2259 Unit 3 Editing and Debugging VIs
Introduction to Primitive Data types
Presentation transcript:

EET 2259 Unit 7 Case Structures; Sequence Structures Read Bishop, Sections 5.4 and 5.5. Lab #7 and Homework #7 due next week. Quiz #3 next week. -Handouts: Slide 23 (on tick count computations) -This week’s big idea is case structure. We’ll also look at sequence structures.

Review: Structures Structures control the flow of a program’s execution. We’ve looked at two kinds: For Loops While Loops This week we’ll look at two more: Case Structures Sequence Structures -Example: E-Mail Notification.vi from two weeks ago contains a loop, a sequence structure, and case structures. -In programs up to now, all of the code on block diagram executed whenever the program ran; case structures let a program decide which code to execute, depending on conditions defined by the programmer.

Case Structure Case structures provide a way to execute different code depending on which one of several possible conditions is true. Traditional programming languages accomplish the same thing using IF statements or CASE statements. (Bishop, p. 236) -Case structures, or something like them, exist in all programming languages.

Case Structure Versus the Select Function We’ve already used the Select function, which is one way in LabVIEW of choosing between alternatives. A Case structure serves a similar purpose, but it’s more powerful and more flexible than the Select function. In other words, you can use a Case structure to do anything that you can do with a Select function, but not vice versa.

Case Structure Versus the Select Function (Cont’d.) In simple cases like the one on the previous slide, a Select function works fine. But here are two situations where Case structures are clearly better than the Select function. You need to choose among many alternatives rather than just among two or three. Your choice is between different kinds of actions, such as choosing whether to display a string or to make an LED blink.

Placing a Case Structure Case structures are found on the Programming>Structures palette. As with loops, click it, and then drag to create a Case structure on the block diagram. (Bishop, p. 236) Demo; and point out selector label and selector terminal. Selector label Selector terminal

Subdiagrams A Case structure has multiple subdiagrams, one of which is executed when the structure runs. These subdiagrams are “stacked” on top of each other like cards in a deck, so that only one is visible at a time. The selector label on the top border tells which case you’re looking at, and has arrow buttons to let you step through the cases.

Selector Terminal A Case structure has one terminal, called the selector terminal. This terminal, labeled ?, determines which one of the structure’s subdiagrams will be executed. You’ll usually wire this terminal to a control or to the output of a function. (Bishop, p. 238) -Bishop’s example on p. 201: place numeric controls labeled “First number” and “Second number,” a toggle switch labeled “Add or Subtract?,” a numeric indicator labeled “Result.” On block diagram, on False subdiagram wire add function, and on True subdiagram wire subtract function. -Run a few times; turn on execution highlighting to show how it selects one subdiagram or the other. -Show how you could same thing using a Select function. -Then place entire block diagram inside a While loop with a Stop button.

Selecting Which Case to Execute: Boolean Example A Case structure with a Boolean selector terminal will have two subdiagrams: one that executes if the condition wired to the terminal is true, and one that executes if the condition is false. This is the example we’ve been dealing with.

Selecting Which Case to Execute: Numeric Example A Case structure with a numeric (integer) selector terminal can have many subdiagrams: the one that’s executed depends on the value wired to the terminal. -First use a 2-position knob (rep=I8) with text labels to do same thing as above. Show that even though it has these text labels, its output is an integer. Then connect it to a case structure to perform the correct function. -Extending previous example, add two more text labels for Multiply, Divide. -You could also do this with Select functions, but it would be a lot harder.

Numeric Selector Labels A numeric selector label can be: a single integer, such as 24 a list of integers, such as 1, 5, 11 a range of integers, such as 10..20 meaning all integers from 10 to 20 ..10 meaning all integers less than or = 10 10.. meaning all integers greater than or = 10 Selector labels cannot be floating-point numbers. (Bishop, p. 238) Demo by adding a 5th case, “Product,” and have it do same case as “Multiply.”

Adding and Deleting Cases Right-click a Case structure’s border and then choose Add Case Before or Add Case After to add a case. choose Duplicate Case to copy a subdiagram to a new case. choose Delete This Case to remove a case. (Bishop, p. 239) Demo

Default Case Numeric case structures and string case structures usually have a default case, which will execute whenever none of the other cases apply. To make a case the default case, right-click its border and choose Make This The Default Case. (Bishop, p. 240) Demo what happens if you don’t have a default case in the previous example.

Tunnels Recall that a tunnel automatically appears on a structure’s border when you run a wire across the border. If data flows from outside the structure to inside, the tunnel is an input tunnel. If data flows from inside the structure to outside, it’s an output tunnel. If an output tunnel is wired on one of a Case structure’s subdiagrams, then it must be wired on all of the subdiagrams. (Bishop, pp. 240-241) -Show how tunnel is white and the run arrow is broken until output tunnels are wired on all cases.

Example if Statement in C++ cout << "How many CDs are you buying? "; cin >> CDs; if (CDs >= 5) { totalCost = 9 * CDs; cout << "You get a discount! "; } else totalCost = 10 * CDs; cout << "Total cost is $" << totalCost; -IF statement is good if you have two different possibilities.

Order of Execution in Text-Based Languages In text-based programming languages, the order of the statements determines the order of execution.

Order of Execution in LabVIEW In LabVIEW, if two functions are connected by wires, you can tell which one must execute first. But you can’t always predict the order of execution. For example, in this code, which function executes first: the Subtract or the Greater Than?

Example Program On this block diagram, in which order will these operations execute? a+b c+d Time delay -Demo with SeqStrucExample.vi.

Sequence Structures Sequence structures can be used to force code to execute in a specific sequence. The code to be executed sequentially is entered on subdiagrams called frames. The analogy is to frames of movie film, which are projected one after another. (Bishop, p. 247) -Modify previous example. Run it, then run with execution highlighting on. Sequence structure with three frames

Measuring the Speed of Your Code Here’s a basic application of a sequence structure. It shows how you can measure the time needed to execute some code by: Reading the computer’s internal clock. Executing the code whose execution time you want to measure. Reading the clock again and finding the difference between the two clock readings. Use template above to measure speed of plotting 1,000,000 random numbers. (Then cube each number before plotting using built-in function and using subVI from Week 4.)

Tick Count The example on the previous slide used LabVIEW’s Tick Count function, which returns the number of milliseconds (ms) on your computer’s millisecond timer. Generally this timer is reset to zero whenever the computer is rebooted. Knowing that Tick Count uses the U32 datatype, let’s figure out how long it will take for it to “roll over” to zero again.

Tick Count Computations What is the largest integer we can represent with U32? _____________ If that number is in ms, how many seconds is that? ________________ How many minutes is that? ___________ How many hours is that? ____________ How many days is that? ____________

Flat or Stacked? LabVIEW has two kinds of Sequence structures: Flat Sequence structures, in which the frames are laid out side by side. Stacked Sequence structures, in which the frames are “stacked” like a deck of cards, with only one visible at a time. (Bishop, p. 247) -Use shortcut menu to convert previous example to a stacked sequence structure, and run again (with execution highlighting on).

Flat or Stacked? (Continued) The two kinds of Sequence structure are similar. The main difference is how much space they take up on the block diagram. But there are other differences, such as: If data is wired out through a tunnel from a frame in a Flat Sequence structure, the data passes out as soon as that frame finishes executing. If data is wired out through a tunnel from a frame in a Stacked Sequence structure, the data does not pass out until the entire structure finishes executing. -Continuing example from previous slide, convert it back to a flat structure, then move controls and indicators outside the frames so that data passes out through tunnels. Run it. -Then convert back to a stacked structure again, and run again.

Use Sequence Structures Only When Necessary Sequence structures serve a valid purpose, but they also tend to hide parts of the program and interfere with the natural flow of data in LabVIEW. Some programmers over-use them. Avoid using them unless you have a situation where you need to guarantee the order of execution, and LabVIEW’s natural data flow does not provide such a guarantee. (Bishop, p. 250) -Example: Look at sequence structure in E-Mail Notification.vi.