Presentation is loading. Please wait.

Presentation is loading. Please wait.

EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2.

Similar presentations


Presentation on theme: "EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2."— Presentation transcript:

1 EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2.
Lab #5 and Homework #5 due next week. Exam #1 next week. -Preview Exam #1. -This week’s big idea is loops, of which there are two main kinds: For loops and While loops. -In the programs we’ve written up to now, code on the block diagram executed exactly once; more sophisticated programs use loops to execute part or all of your block diagram more than once.

2 Breaking a Bad Habit In prior weeks we’ve often used the Run Continuous button to run a program over and over. To stop such a program, we’ve used the Abort button. But from now on, you should use loops instead of the Run Continuous button. You should use the Abort button only as a last resort to stop a runaway program.

3 Structures Structures (which in other programming languages are called control structures) control the flow of a program’s execution. This week we look at two kinds: For Loops While Loops Later we’ll look at other kinds: Sequence Structures Case Structures -Example: Notification.vi from last week contains a loop, a sequence structure, and case structures.

4 For Loop A For Loop executes the code inside its borders a specified number of times. The code inside the For Loop’s borders is called a subdiagram. A For Loop has two terminals: the count terminal and the iteration terminal. (Bishop, p. 214)

5 For Loop Example Count terminal Iteration terminal
-Identify count terminal, iteration terminal, and subdiagram. Iteration terminal

6 Placing a For Loop For Loops are found on the Functions>> Programming>> Structures palette. Click it, and then drag to create a loop on the block diagram. Then place items inside the loop to build your subdiagram. (Bishop, p. 214) -Have them build and run example on previous slide. -Also demo how to clear a chart. (We’ll discuss charts in detail later in the course.)

7 Be Careful to Place Items Inside the Loop
If you’re not careful, you can end up with items “hovering” over a loop instead of being located inside the loop. This can happen if you move a loop over existing code, or if you use the arrow keys to move an item over a loop. Correctly located inside the loop. Not inside the loop, but hovering over it.

8 Count Terminal A For Loop’s Count Terminal, labeled N, lets you set how many times the loop will execute. You can set the count to a constant, or to a value set by the user through a control, or to the output of a function, etc. The count is available to be used inside the loop. (Bishop, p. 214) Modify example on previous slide by adding a numeric control to let the user decide how many numbers to generate.

9 Iteration Terminal A For Loop’s Iteration Terminal, labeled i, contains the number of loop iterations that have been completed. The iteration number is available to be used inside the loop. It starts at 0 and increases to N-1. (Bishop, p. 214) -Example on previous didn’t use N or i inside the loop. -Modify so that both N (count terminal) and i (iteration terminal) are wired to numeric indicators in loop, and set N=10. -It will run so fast that they can’t see it execute, but notice that at the end, i is equal only to 9, not 10. -Slow it down by turning on Execution Highlighting, and observe how N and i behave.

10 Inserting a Time Delay Loops usually run so quickly that the user can’t see what’s happening. To add a time delay, use either the old-fashioned Wait (ms) function or the newer Time Delay Express VI. Both are found on the Functions>> Programming >>Timing palette. Place either one anywhere inside the loop. -Demo using previous example. -Then add a numeric control (inside loop) to let user set time delay. -Using another random # and indicator, demo that code inside the loop executes repeatedly, but code outside executes only once.

11 Good Practice: Include a Time Delay in All Loops
What if you don’t want to slow down the loop to “human speed,” but want it to run as fast as possible? It’s still a good programming practice to include a small (10 ms) delay in your loop. This prevents LabVIEW from consuming all of your processor’s time, and lets the operating system perform necessary tasks.

12 Tunnels If a wire crosses the border of a loop (or other structure), a tunnel automatically appears on the border. Doing this is often useful, but can also lead to confusion unless you keep in mind the following point….

13 Tunnels and Data Flow No data passes into or out of a structure while the structure is executing. Input data is read before the structure executes; subsequent changes to the input values are ignored. Output data is not available until after the structure finishes executing. -Demo by showing that a time delay cannot be dynamically changed by a control outside the loop. -For output data: place a numeric indicator outside the loop, and wire it to i. This will cause a broken arrow, which you can fix by right-clicking on the tunnel and disabling indexing. -Run it, and notice that the numeric indicator outside the loop does not get updated until after the loop finishes executing all of its iterations.

14 Example For Loop in BASIC
The type of loop we’ve been discussing is called a “For Loop” because in text-based programming languages (such as BASIC or C++) it is coded using the word FOR. Example: CLS FOR i = 1 TO 15 PRINT i, 2 * i NEXT i Save prior work as ForLoop.vi.

15 Integer Representations
Recall that blue terminals and blue wires represent integers. Integer terminals can be further categorized into byte signed integer (I8), word signed integer (I16), long signed integer (I32), etc. This is called the “representation” of the number, and you can change it by right-clicking on a terminal and choosing “Representation.”

16 Integer Representations (Continued)
These representations differ in the range of values they can handle and the amount of memory they use. Representation Max. Value Memory Bytes Byte signed integer (I8) 127 1 Word signed integer (I16) 32,767 2 Long signed integer (I32) 2,147,483,647 4 Quad signed integer (I64)  11019 8 Byte unsigned integer (U8) 255 Word unsigned integer (U16) 65,535 Long unsigned integer (U32) 4,294,967,295 Quad unsigned integer (U64)  2 1019 -Show what happens if you try to type 200 into a numeric control set to I8. -Also show what happens if you have an I8 control and an I8 indicator, and you try to multiply the input number (100) times 3.

17 Floating-Point Representations
Recall that orange terminals and orange wires represent floating–point numbers. Floating-point terminals can be further categorized into single precision, double precision, and extended precision.

18 Floating-Point Representations (Continued)
These representations differ in the range of values they can handle and the amount of memory they use. For more info on numeric data types: Representation Max. Value Memory Bytes Single-precision (SGL)  3.40 x 1038 4 Double-precision (DBL)  1.79 x 10308 8 Extended-precision (EXT)  1.19 x 16 -Can also get to this table through Help > Building the Block Diagram > Numeric Conversion > Numeric Data Types > Numeric Data Types Table.

19 Coercion Dots If you wire together two terminals of different numeric representations, LabVIEW must convert the number from one representation to the other. In these cases a red dot called a coercion dot will appear on the terminal where the conversion takes place. Coercion dots are bad. They waste memory, and can lead to rounding errors that are difficult to find. (Bishop, p. 216) -Demo: on a blank VI, wire numeric ctrl to a numeric indicator, and show effect of changing the representation of one or the other. Show how wiring a floating pt control to an integer indicator will round off the number; likewise if we divide 3 by 2 and display on I8 indicator. -Coercion dots won’t prevent a program from running, as does wiring two different data types together.

20 Numeric Conversion Functions
LabVIEW has functions for converting from any representation to any other representation. (For example, the To Word Integer function converts any number to the I16 representation.) These functions are found on the Functions > Numeric > Conversion palette. These are sometimes useful in eliminating coercion dots. See example on next slide…

21 Numeric Conversion Functions: Example
Suppose we want to add a double-precision floating-point number to a loop’s iteration terminal. The iteration terminal’s representation is I32 and cannot be changed. But you can use a conversion function to convert it to double.

22 While Loop A While Loop executes the code inside its borders repeatedly until a certain condition is met. A While Loop has two terminals: the iteration terminal and the conditional terminal. (Bishop, p. 221) Examples of possible conditions: user presses a button; user types in string that matches the password; random number is > 50.

23 While Loop Example Iteration terminal Conditional terminal
-Identify iteration terminal, conditional terminal, and subdiagram. Iteration terminal Conditional terminal

24 Placing a While Loop While Loops are found on the Functions >> Programming>> Structures palette. Click it, and then drag to create a loop on the block diagram. Then place items inside the loop to build your subdiagram. -Have them build and run example on previous slide. -Then replace STOP button with code to stop it when the number is > 0.9.

25 Iteration Terminal A While Loop’s Iteration Terminal, labeled i, contains the number of loop iterations that have been completed. It behaves just like a For Loop’s iteration terminal. The iteration number is available to be used inside the loop. (Bishop, p. 221)

26 Conditional Terminal A While Loop’s Conditional Terminal determines at the end of each loop execution whether the loop will be executed again. You set the Conditional Terminal to either Stop if True or Continue if True. Usually you’ll wire a Boolean control or the output of a Boolean function to this terminal. (Bishop, p. 221)

27 Conditional Terminal: Stop if True
When the Conditional Terminal is set to Stop if True, it looks like a red stop sign on the block diagram. A true condition will cause the loop to stop executing, but a false condition will cause it to execute again. (Bishop, p. 221)

28 Conditional Terminal: Continue if True
When the Conditional Terminal is set to Continue if True, it looks like a green looping arrow on the block diagram. A true condition will cause the loop to execute again, but a false condition will cause it to stop executing. (Bishop, p. 221) Be careful: when using your mouse to move the conditional terminal, it’s easy to accidentally change it from Stop if True to Continue if True (or vice versa).

29 Example While Loop in BASIC
This type of loop is called a “While Loop” because in text-based programming languages it is coded using the word WHILE. Example: CLS INPUT “Guess my age. ”, guess WHILE guess <> 46 INPUT “No. Try again. ”, guess WEND PRINT “You got it!”

30 For Loop With a Conditional Terminal
It’s possible to add a conditional terminal to a For Loop, creating a loop that behaves like a cross between a For Loop and a While Loop. To do this, right-click a For Loop’s border and select Conditional Terminal. (Bishop, p.220) We won’t use this feature in this course: Whenever I refer to a For Loop, I mean a plain For Loop without a conditional terminal. A For Loop with a conditional terminal executes until the condition occurs or until all iterations complete (whichever occurs first).


Download ppt "EET 2259 Unit 5 Loops Read Bishop, Sections 5.1 and 5.2."

Similar presentations


Ads by Google