Loops

This will be added to a power point slideshow for loops... When I make the time... This approach is valid for any programming language to figure out how fast a system can run.

flow control:
If an individual is shown the instructions:
Go to door, Turn around 3 times, Come back.

The order matters and if these instructions are just presented as a group the instructions can be interpreted in 6 ways 
(3 first possibilities, 2 2nd possibilities, 1 3rd possibility = 3*2*1 = 6 STATISTICS IN ACTION!) 
  • Go to door, Turn around 3 times, Come Back
  • Go to door, Come back, Turn around 3 times
  • Turn around 3 times, Go to door, Come back
  • Turn around 3 times, Come back, Go to door
  • Come back, Go to door, Turn around 3 times
  • Come back, Turn around 3 times, Go to door

Instruction order matters and flow control tells the NXT the order to run each instruction. 
Flow Control In and then Flow Control out. 

For the big robot we will be using sequence blocks and a similar flow control to perform the same task.

Exercise 1: How fast is an NXT loop?
We are going to figure out how fast the NXT runs by running for a LOT of cycles which will put the aggrigate runtime into something people can recognize. 
We are going to use the Beep to give us a real world indication for when the loop starts and stops. 

"Beep" -> Flow control connected to LEFT side of "while loop" -> flow control through the loop to the right side loop wall -> 2nd "Beep" block

-Set up a logic comparison for the loop iteration(the little "i" on the left side of the loop tells how many times the loop has run or "iterated")
--Set the loop "stop condition" to stop when the loop has reached 100,000 iterations
--100,000!?!??! that's a LOT!

Run the Program, how long does that take in the real world 
-(watch the computer clock by clicking on the time and then watching the seconds)

If we divide the 100,000 by the amount of time that took, 
-how many loop cycles would it take to run for 1 second
Verify your answer by changing the "stop condition" comparison logic to your estimate
-how many loop cycles would it take to run for 2 seconds
-- Verify again

Exercise 2: Display the cycles

Send the iteration number to the display by putting a display block inside the loop
- you will need to modify the "flow control" so that the NXT know's when to run the display block (at this point it's not needed but is good practice)
-- Hint, change the "stop condition" comparison logic to 1,000
When you find the display block, the "iteration count" is a "integer data type"
- tell the display to display integers
- connect the "iteration count" to the input of the display block

Run the program
-How long did it take
- How many display cycles can be made in 1 second?

The time that the program takes for each loop actually changes based on what isinside the loop.
The processor is doing actual work and actual tasks to make things happen. 
These tasks each take a finite amount of time that will affect our programs.
If we want a program to run a fixed amount of time, we need to make sure that we are properly considering and testing all of our assumptions.

Exercise 3: "Wait" statements

Add a NXT "Wait" milliseconds block into the flow control
This block will cause the program to wait a specific amount of time in milliseconds.
1000 milliseconds = 1 second

A)
Set the input to the wait block to 1000
Set the iteration comparison to 2

Run the program

How long between the beeps?

Why?
How many loop iterations(cycles) were run?
If we want 2 seconds what should the comparison be set to?

B)
Set the input to the wait block to 10
Set the iteration comparison to 200

How long between the beeps?
How many loop iterations(cycles) were run?

The point is, you can achieve the same goal with different logic and inputs.
There are times when either version is the most appropriate and times where it really just doesn't matter.

Advanced exercise: Time Priority of a Loop

If there is a wait statement that is not in the NXT flow control path and we have a display command
Does the display command dominate or does the wait statement get added on top of the time for the display command.

Verify: Use math to calculate how many milliseconds a wait command takes
Set a wait command for 1 ms LESS time than the display takes
e.g. display takes 7mS, wait time = 6mS
Hypothesis: If the wait statement is in addition to the display command: 
1000 loops will take: 1000*wait time + 1000*display command

If the wait statement takes priority 
1000 loops will take: 1000*display command

If 1000 loops isn't enough to tell a difference, make it bigger.

Thursday:
Exercise 4: Beeping Loop

In music, "Beats per Minute", BPM, = how fast between notes.
a slow song has 60 bpm in 4:4 time this is 1 quarter note per second.
a fast song has 180bpm in 4:4 time this is 3 quarter notes per second

Convert 120bpm to milliseconds per beat

Use math blocks so that you can put any bpm and the program automatically does the conversion
-should the math be inside or outside the loop?
-- If we want to be able to change the beats per minute while the loop is running
-- If we are only going to set the beats per minute at the start

Remove the "wait" block
Move the Right "Beep" inside the loop.
Connect the milliseconds per beat to the "duration" input of the beep
- This functionally replaces the "wait" block because the program must finish the beep before it can do something else
- Set the iteration compare to make the program beep 8 times

Change the bpm input to various times.

Exercise 5: MAKING MUSIC with "CASE" structures!

1) explain sixteenth notes
2) explain using the wait statement to represent sixteenth notes
3) explain simple 2 case structure
- first half of iterations play one frequency
- second half of iterations play 2x frequency

4) explain multi case "case" structure
- play full "Star Wars" Theme or something everyone recognizes
- this is done by:
"If iteration = 0..4" play frequency, duration
"if iteration = 5..8 play frequency duration
Do this linearly for the entire song
(you might actually use duration instead of the wait statement for each individual case)

Make other songs
Comments