TI Codes: TI-84 Plus Technology
Encourage students who have some programming experience to build on their knowledge with activities that take them a little deeper into coding.
Guessing Game
Write a Simple Guessing Game
The guessing game includes several components:
1. Variable N and randInt() used to pick the number to guess.
2. A Repeat loop that repeatedly asks for a guess until the player guesses correctly.
3. The Prompt command used to get a guess from the player.
4. If conditionals and Disp used to tell the player if their guess is too high or too low.
5. Variable M and Disp used to track and report how many guesses the player made.
Guessing Game
How it Works
How the Guessing Game Works
The diagram below is a flowchart of how the program works. If you haven’t seen a flowchart before, you read it by following the arrows around the diagram, beginning at START and ending at Stop. If you spend a minute comparing this diagram to the code for [PRGM]GUESS, you’ll see that each block corresponds to 1-3 lines of the program
Step 1
Line-by-line, here’s how it works:
:randInt(1,50)➔N
Pick a random number between 1 and 50, and store that to variable N. Each time the program gets a new guess from the player, it will compare it to N to check if it’s correct.
:0➔M
Store the number of guesses made so far to variable M. Each time the player makes another guess, the program will add 1 to M, then display M at the very end of the program.
Step 2
:Repeat G=N
Repeat the code between this Repeat command and the corresponding End command until G=N. In this program, G is the guess from the user, and N is the target number, so the loop ends when the guess is equal to the number the player is trying to guess. One special quirk of Repeat: it only checks the condition (G=N) after it has completed an entire run through the loop. Therefore, we don’t have to initialize G by storing a value like 0 to it before the loop starts. If we were using a While loop, we would have to initialize G, because While checks the condition before it starts running the loop.
:Prompt G
Ask the player for a value for G, then store the number the player types into variable G
Step 3
:If G>N
:Disp "TOO HIGH"
If the guess G is larger than the target number N, display TOO HIGH so the player knows to guess lower. The If command will run the next line of the program if the condition (G>N) is true; otherwise, it will skip it.
:If<><>
:Disp "TOO LOW"
Similarly, if the guess G is smaller than the target number N, display TOO LOW. If G=N, the loop won’t have finished yet, but neither Disp command will run, because G is not greater than N, and G is also not less than N.
:M+1➔M
Add 1 to the number of guesses made.
Step 4
:End
End the loop and check the Repeat condition. If the condition G=N is false, run the loop again, starting at Repeat. If it’s true, continue with the following code after the End.
:Disp "CORRECT AFTER:"
:Disp M
:Disp "GUESSES"
Display three lines, including the number of guesses made. You could optimize this into a single Disp line with commas
Step 5
Going Further: Expand Your Game
- Let the player choose the smallest and/or largest possible number to guess. The game in this lesson picked a number between 1 and 50.
- Display the range in which the player should be guessing, here 1 to 50.
- Keep track of the high score (the lowest number of guesses made). You could keep track of other statistics as well, like the average number of guesses or the worst score (the highest number of guesses). To store information in more permanent locations than the numeric variables A-Z, take a look at the information on custom lists.
- Use Input instead of Prompt to ask for a guess.
- Find a way to [CLEAR] the screen before the game begins.
You can, of course, also use your imagination to think of other features you want to add to the game. Good luck!
Event Loops
Moving a Letter around the Homescreen
The program that we’ll be creating in this lesson is quite short, but brings together a number of important programming concepts. We’ll be building a program around an event loop: a section of code executed over and over again that waits for and reacts to events. In this program, the events we’ll be looking for will be keypresses, and our program will respond to these events by moving a letter around the calculator’s homescreen. Event loops in computer programs might wait for a time or date, a mouse click, a key on the keyboard, or even arriving packets from the network.
Step 1
Event Loops and getKey
The following diagram shows the basic structure of an event loop-based program. It executes code to set up the loop, like initializing variables, then it continually repeats the event loop until some escape condition occurs.
Step 2
There are three major components of an event loop:
- An escape condition that defines when the event loop stops repeating. In a computer program, an escape condition might be something like exiting from the program or pressing [ESC]. In our program, we’ll check for the [CLEAR] key, and our escape condition could be expressed as “end the event loop if [CLEAR] is pressed”.
- Non-event code. In this program, we won’t have any non-event code. However, in other programs, this is code that runs regardless of whether events occur or not.
Check for (and react to) events. In this program, our events are keys being pressed. If we want to check for and react to keypresses, though, we’ll need to be able to execute or skip sections of code based on whether a key is pressed.
Step 3
To construct our event loop, we’ll need conditional statements and getKey.
getKey is a TI-Basic command that your programs can use to detect if a key has been pressed. If the user pressed a key since the last time getKey was called, getKey will return a number between 11 and 105, indicating which key was pressed (see the diagram below). If no key was pressed, it returns 0. Unlike Input and Output, the getKey command doesn’t pause your program, so you can create more complex programs and games.
Step 4
Step 5
Moving a Letter around the Homescreen
Let’s explore the MOVECHAR program. The MOVECHAR program is one of the simplest possible examples of an event loop-based program, repeatedly listening for keypress events and acting on them. The flowchart represents the MOVECHAR program. The event loop itself loops from “Redraw M” to “Check for keypresses” and then back around to “Redraw M”. The escape condition that we discussed earlier was the condition to leave the event loop; here, you can see that the only way to leave the loop is to press [CLEAR].
Note that if you’re using the TI-83 Plus or TI-84 Plus, the only differences are in the values used for the edges of the homescreen and the starting point, due to differences in the sizes of each calculator’s homescreen.
Step 6
Line-by-line, here’s how the code works:
:13➔A
:5➔B
Initialize the x- and y-coordinates of the letter that will be drawn. We don’t use the X and Y variables, because the calculator’s operating system sometimes modifies the Y variable if you use graphscreen commands. In this program, A is the x-coordinate, and B is the y-coordinate. Column 13, row 5 is roughly the center of the color calculator’s homescreen.
Step 7
:ClrHome
[CLEAR] the homescreen.
:Repeat K=45
Repeat all of the code between Repeat and End (the event loop) until the variable K equals 45 (the keycode for [CLEAR]).
Step 8
:Output(B,A,"M”)
Draw the letter M at row B, column A on the homescreen. If there was already an M on the screen at the same coordinates, it simply draws over it, so the screen doesn’t change.
:getKey➔K
Get the keycode for any key pressed since the last time getKey was called, and store it in the variable K. This returns immediately, instead of waiting for a key to be pressed: if no key was pressed, it stores 0 to K
Step 9
: If K≠0
If a key was pressed, we want to erase the letter already on the screen, because we might be moving it. If we didn’t erase it, we’d leave a trail of Ms around the homescreen.
:Output(B,A,"_”
Draw a space over the M that was on the homescreen, effectively erasing it. This line of code is only executed if K is not zero.
:If K=24 and A>1
:A-1➔A
Step 10
Step 11
:If K=26 and A<>
:A+1➔A
:If K=25 and B>1
:B-1➔B
:If K=34 and B<>
:B+1➔B
Step 12
Handle the right, up, and down arrows. Each conditional statement checks that both an arrow key has been pressed, and that the letter isn’t already at the corresponding edge of the screen.
:End
Loop back up to the Repeat command, unless K=45. If K is 45, the loop will end, and because this is the last line of the program, the program will end as well.
Step 13
Going Further: Expand MOVECHAR
With this simple event loop program that listens for and reacts to keys, you can build many fun programs and especially games.
- An easy place to start would be to make something other than an M move around the screen. Perhaps a different letter? Or a group of letters in a shape?
- Handle diagonal movement so that the letter can move in 8 directions instead of just 4. Because getKey can’t register multiple arrow keys held together, you could use the 1–9 keys as arrows instead (where 2 is down, 7 is up-left, and so on).
- If you’re familiar with drawing text on the graphscreen, try moving this program to the graphscreen. Which commands do you need to change, and which stay the same? What are the new bounds you need to use to keep the text from going off the screen?
- Try turning the MOVECHAR program into a space shooter game: the M can become your spaceship, and you might be trying to shoot asteroids before they reach you. How can you implement a weapon, and keep track of where the asteroids are and the score?
You can, of course, also use your imagination to think of features you want to add. Good luck!
Step 14
Snake
Writing a “Snake” Game
Create the Snake program on your calculator as prgmSNAKE. If you have a color-screen calculator like the TI-84 Plus CE or TI-84 Plus C Silver Edition, you can enter it exactly as shown. For the the TI-84 Plus and TI-84 Plus Silver Edition, calculators, you need to account for the fact that the homescreen is smaller
Step 1
Line-by-line, here’s how the code works:
:ClrHome
Clear the homescreen.
13➔A
:5➔B
:10➔X
:1➔P
:34➔D
Initialize all the variables to be used:
Step 2
- A is the x-coordinate of the snake’s head, and B is the y coordinate of its head. Row 5, column 13 is roughly the center of the TI-84 Plus CE’s homescreen.
- X is the length of the snake, here, 10 segments. If you wanted to make this a real snake game, you’d have to start with a small X, and increase it as the snake ate food.
- P is a pointer to the element holding the snake’s tail. L1(P) is the y coordinate of the snake’s tail, and L2(P) is the x-coordinate of the snake’s tail. As the snake moves, P moves as well, so that it always points to the end of the snake (without having to move the data in L1 and L2 forward).
- D is the direction in which the snake is heading. To reuse code you’ve seen before, we use the keycodes for up, down, left, and right (25, 34, 24, and 26, respectively) to represent the direction the snake is going.
Step 3
:X➔dim(L1)
:X➔dim(L2)
Create two lists, each X elements long, for our circular buffers of snake segment coordinates.
Step 4
:Fill(B,L1)
:Fill(A,L2)
Fill the two lists with the starting coordinates. This way, all 10 segments of the snake will start out on a single spot on the homescreen, and as the game starts, the snake will appear to grow out of that spot to its full length. Making the snake start already drawn at its full length would be more complicated.
Step 5
:Repeat K=45
As in Lesson 2, repeat the main event loop until [CLEAR] is pressed.
:Output(L1(P),L2(P)," ”)
Erase the tail of the snake. Because we’re using circular buffers, we change P to always point to the elements of L1 and L2 holding the snake’s current tail. L1(P) is the y-coordinate (the row) of the tail’s last segment, and L2(P) is the -coordinate (the column) of the last segment.
Step 6
:Output(B,A,"0")
Draw the new head. A and B always hold the coordinates of the snake’s head, and "O" just happens to look good as a snake segment. You could use any character you want instead.
:B➔L1(P)
:A➔L2(P)
Store the current head coordinates into the circular buffers. Because P+1 will point to the tail on the next iteration, P will point to the head, so we store these over the old tail coordinates at P.
Step 7
:P+1-X(P=X)➔P
Update P to point to the next element of the lists. If P is less than X, then P=X is false (0), and this simplifies to P+1➔P. If P is already pointing to the last element of the circular buffers, which means P=X, then P=X is true (1), and this simplifies to P+1-X➔P, or X+1-X➔P, or 1➔P. Thus, when P reaches the end of the lists, it will wrap around to the beginning again.
Step 8
:getKey➔K
:If max(K={24,25,26,34})
:K➔D
Handle keypresses. As always, we store the keypress in K, so that the Repeat K=45 condition can tell if [CLEAR] has been pressed. The conditional statement is a shorthand for If K=24 or K=25 or K=26 or K=34. K={24,25,26,34 produces a 4-element list, holding the elements {K=24,K=25,K=26,K=34}: that is, a series of 1s and 0s. The max() command returns the largest value in a list, so if K is equal to any of those four numbers, there will be a 1 in the resulting list, and the maximum will be 1. If K is not equal to any of those four keycodes, the list will be filled with 0s, and the maximum of the list will be 0. Therefore, the conditional will only be true if K is equal to one of those four keycodes. Moreover, if K holds an arrow key’s keycode, update D to contain a new direction for the snake to go.
Step 9
:A+(D=26)-(D=24)➔A
:B+(D=34)-(D=25)➔B
Use the direction variable D to update the coordinates of the head. If D=26 or D=24, then the snake’s head is moving horizontally. Update A (the -coordinate) accordingly. Try plugging D=26 and D=24 into the first line to see how this works. The second line updates B, the -coordinate of the head, if D=34 or D=25, which means the head is moving up or down.
Step 10
:A+26((A=0)-(A=27))➔A
:B+10((B=0)-(B=11))➔B
The preceding two lines could make the head of the snake go off the edges of the screen. If we try to draw a character off the edge of the screen, the program will end with an ERR:DOMAIN, so we need to fix it. These two lines wrap the head around the edges of the screen. For example, if it reaches the top of the screen, it will reappear at the bottom instead.
:End
Loop back up to the Repeat command, unless K=45. If K is 45, the loop will end, and because this is the last line of the program, the program will end as well.
Step 11
Going Further: Build a Complete “Snake” Game
The SNAKE game as presented contains everything you need to start building a “Snake” game. The three biggest missing pieces are: (1) a growing snake, (2) scoring, and (3) food to eat. Here’s what you could do to make a complete “Snake” game out of this basic version:
- First, you’ll need a second variable to handle a head pointer. In this version, we used P as both the head and tail pointer, because the circular buffer was always completely full.
- Next, you’ll need to have circular buffers which are bigger than the starting size of the snake, because the snake will need to be able to grow. This will lead you to some interesting design decisions: should you start with lists as large as the snake could possibly grow? Or should you expand the lists when the snake is in danger of outgrowing the buffers?
- Next, you’ll need some food for the snake to eat. How will you make sure that you don’t put the food on top of the snake? Some approaches store the contents of the homescreen in a matrix, so you can easily test which spots on the homescreen are occupied.
Step 12
- You’ll need to detect if the snake is about to (or has just) eaten the food, so that you can increment the score and the snake’s length, and place a new piece of food.
- You can go even further and add things like walls and differently-shaped levels. If you do this, you might want to strongly consider the matrix approach to easily detect when the snake has hit a wall (or its own tail).
You can, of course, also use your imagination to think of features you want to add. Good luck!
You can learn more about programming for the TI-84 Plus family of graphing calculators in “Programming the TI-83 Plus/TI-84 Plus,” by Christopher Mitchell, Ph.D.
Maze (New)
Mini Project 1: Detect Which Keys are Pressed
Download Teacher/Student DocsAfter completing a series of mini projects, you’ll have a maze game similar to the one on the right.
Along the way you will learn how to:
1. Detect which keys are pressed.
2. Use key presses to move a word stored in a string value.
3. Draw objects using pixels.
4. Move objects using key presses and variables.
5. Create a specific maze.
6. Randomize maze attributes.
7. Create the final maze project (with option to add sound using the TI-Innovator™ Hub).
Step 1
The getKey command returns the value of the key that is pressed.
To find the getKey command, press [prgrm] then select the I/O menu for input/output.
The line getKey→K stores the value of the key into a variable named K.
Write the following lines of code:
:ClrHome
:getKey→K:Disp K
If you execute the program now, it will execute and display a 0 automatically. The program doesn’t wait for you to press a key. If it doesn’t detect a key push it displays 0.
Step 2
Insert the two lines between ClrHome and the getKey line
([alpha] [graph]) will give you an option to insert a blank line).
:0 →K
:While K ≠45
At the end of your code add
:End
The WHILE loop will continue to execute the code to get and
display the values of keys pressed until the clear key is pressed.
Run your program.
What type of values are displayed when you press a key?
What type of values are displayed when you don’t press a key?
Step 3
Insert the following IF statement before the display line.
:If K ≠ 0
:Then
:Disp K
:End
Before running your modified program, predict how the new
lines of code will change the program.
Step 4
Use your program to explore the keypad.
What are the values of the arrow keys?
What is the value of the enter key?
What value is the clear key?
Use the blank template of the keypad (provided in the 'Download- Teacher/Student Docs folder' and can be printed ) to write down the numeric value of each key. Do you notice the pattern?
Mini Project 2: Use Key Presses to Move String
Download Teacher/Student DocsStep 1
Your screen is divided into multiple rows that go down the screen and multiple columns that go across the screen.
Type the code below, insert your name.
Note: Str1 can be found under Vars →String.
:3 →R
:1 →C
:“NAME” →Str1
:Output(R,C,Str1)
Execute your code.
What row and column contains your first initial?
Step 2
You’ll now modify your code to get a key press value. You’ll use this value to change either the row or column variable which will change where your name is displayed.
- Use the variable K to store the value from the key press
- Use a WHILE loop to continually get key press values until the clear key is pressed
- You will need four IF statements, one for each arrow key
Do you remember the arrow key numbers from Mini Project 1? If not, return to it, and determine the number for each arrow key.
What do you think the IF statements will look like to control the movement of the string?
Step 3
The down key gives the value of 34. To move the text down the screen, check to see if key 34 was pressed. If it was pressed, increase the row by 1 and clear the old text off the screen.
Add this code inside your loop so it is repeated each time the program loops through and checks for a key press.
:If K = 34
:Then
:R+1 →R
:ClrHome
:End
Step 4
Be careful when you execute the code. If R (number of rows) goes off the screen, you will get the following error.
How times can you press the down arrow before you get an error? How many rows of text fit on the screen?
Code the Up, Left and Right arrow keys. Check your code with the answers on the next step.
Step 5
Can you error trap your code so the Domain Error doesn’t occur when the user moves the name off the screen?
Step 6
Error trapping
The row and column variables have to be larger than 0.
:If R = 0
:Then
:1 →R
:End
:If C= 0
:Then
:1 →C
:End
There are 10 rows.
:If R = 11
:Then
:10 →R
:End
Mini Project 3: Draw Objects Using Pixels
Download Teacher/Student DocsIn this mini project, you’ll:
- Design two characters — a PLAYER character and a GOAL character.
- Learn how to turn pixels on and off.
- Change pixel colors using pre-defined colors.
- Create two programs — PLAYER and GOAL.
Step 1
Think of your calculator as a piece of graph paper. The graph paper has 164 rows and 264 columns. To color one pixel the code is:
Pxl-On(row number, column number, color)
To create a blue rectangle that has a width of five and a height of two, type the code below.
- AxesOff is under FORMAT (2nd zoom).
- Colors can be found using the [prgm] > COLORS.
- All other commands are under DRAW (2nd prgm).
:AxesOff
:BackgroundOff
:ClrDraw
:Pxl-On(0, 0, BLUE)
:Pxl-On(0, 1, BLUE)
:Pxl-On(0, 2, BLUE)
:Pxl-On(0, 3, BLUE)
:Pxl-On(0, 4, BLUE)
:Pxl-On(1, 0, BLUE)
:Pxl-On(1, 1, BLUE)
:Pxl-On(1, 2, BLUE)
:Pxl-On(1, 3, BLUE)
:Pxl-On(1, 4, BLUE)
Step 2
Modify your code. Create a brown rectangle that has a width of 20 and a height of 3.
The previous step had you write a line of code for each pixel. To make a 3 row x 20 column BROWN rectangle, you would need 60 lines of code. To simplify this process, we will use a loop.
:AxesOff
:BackgroundOff
:ClrDraw
:For(C,0,19)
:Pxl-On(0, C, BROWN)
:Pxl-On(1, C, BROWN)
:Pxl-On(2, C, BROWN)
:End
Note: If you want to color a row, but turn a few points off, use the command Pxl-Off(row number, column number).
Step 3
For the maze project, you need to create two objects. Each object will be coded in its own program. One object will be your PLAYER that moves on the screen. The other will be a stationary GOAL object. Your objects need to be between 10 and 15 pixels in width and height.
Use graph paper to sketch out each of your objects.
Sample objects:
Heart.8xp
Smile.8xp
Step 4
Here’s some sample code for the first sample object. If you designed your own objects, the snippets might help in coding your own designs.
Note: Don’t start coding your characters until you’ve read step 6.
Step 5
Here’s some sample code for the second sample object.
Note : Don’t start coding your characters until you’ve read step 6
:For(C,3,13)
:Pxl-On(6, C, YELLOW)
:Pxl-On(7, C, YELLOW)
:Pxl-On(8, C, YELLOW)
:Pxl-On(9, C, YELLOW)
:Pxl-On(10, C, YELLOW)
:End
:Pxl-Off(6,3)
:Pxl-Off(6,13)
:For(R,6,7)
:Pxl-On(R,6,Black)
:Pxl-On(R,7,Black)
:Pxl-On(R,9,Black)
:Pxl-On(R,10,Black)
:End
:Pxl-On(10,5,Black)
:Pxl-On(10,11,Black)
Step 6
The PLAYER character will be placed in the upper left-hand corner of the maze. Code your PLAYER program so your character will be ready for import into Mini Project 7.
The GOAL character will be placed in the upper right-hand corner of the maze. Code your GOAL program so your character will be ready for import into Mini Project 7.
Note: It might be useful to know how to copy and paste lines of code. Press [alpha] [graph] for the f5 menu that contains copy and paste options.
Mini Project 4: Move Objects Using Key Presses and Variables
Download Teacher/Student DocsIn this mini project, you will be moving your PLAYER object, which will be similar to how you moved your name in MP2. You will need to:
- Draw the initial heart.
- Use a loop to detect key press values.
- Do the following X, if an arrow key is pressed.
- Turn all the heart pixels off.
- Redraw the heart in the new location.
Step 1
Create a new program named PLAYER. You’ll need two variables to shift your picture vertically (V) and horizontally (H). We’ll use K to represent the button push value.
Type the following code:
:ClrDraw
:0 →K
:0 →V
:0 →H
Use recall to import the code from image you’d like to be the PLAYER.
rcl ([2nd] [sto→])
prgm
EXEC
Select the name of your file
Press the enter key
Step 2
Moving your object is similar to moving your name like you did in Mini Project 2.
You will use:
- A WHILE loop around all of your pixel code that executes until the clear button is pressed
- The getKey command to determine when arrow keys are pressed
- IF statements to change the vertical and horizontal variables V and H
- Add a WHILE statement at the end of code. The statement should continue while the user doesn’t press the clear button.
- Use the getKey command to store the keys pressed in the value K.
- Write an IF statement that will happen if an arrow key is pushed.
Step 3
Your code should look something similar to the code on the right.
Next you’ll write the code to hide the old shape.
Step 4
Add a comment line so it will be easier to debug your code.
:”HIDE OLD
Then import the heart code.
:rcl prgmHeart
Delete the ClrDraw from the beginning of the heart.
Note:If you leave the ClrDraw in here, it will delete your maze and your GOAL when we import this code into the maze game.
Step 5
You need to modify every pixel line of the form
Pxl-On(row number, column number, color)
to
Pxl-Off(row number + V, column number + H)
Note: Remember V is the vertical shift and H is the horizontal shift.
This will turn off the old pixel from the old shifted location.
Add an End statement to the last line to end the IF for the key press.
Step 6
Now you’ll change the vertical and horizontal shift based on the key pressed.
- If the key is the left arrow key, subtract 30 from H
- If the key is the right arrow key, add 30 to H
- If the key is the up arrow key, subtract 20 from V
- If the key is the down arrow key, add 20 to V
:Then
:H-30→H
:End
......<———Add the code for the Right, Up and Down key
Step 7
If an arrow key was pressed, you need to redraw the shape after the shift.
Insert the following:
:If K=24 or K=25 or K=26 or K=34
:Then
:”DRAW NEW
Now import the heart file one more time.
Delete the ClrDraw.
Step 8
Lastly, you need to modify every pixel line of the form:
Pxl-On(row number, column number, color)
and
Pxl-Off(row number, column number)
Since the variables V and H hold the shift variables, the modifications are:
Pxl-On(row number + V, column number + H, color)
and
Pxl-Off(row number + V, column number + H)
Add an End for the IF.
Add an End for the WHILE loop.
Mini Project 5: Create a Specific Maze
Download Teacher/Student DocsIn this mini project, you will create a maze. You'll use lists to store values for each column indicating open and closed doors. You'll use Pxl-Onto turn on pixels. You will use lists, loops and the Pxl-on command to draw a maze similar to the one here.
Step 1
The maze has six columns of walls not including the border (L1, L2 …).
The maze has eight rows.
You will use lists to store the properties of the walls.
Step 2
In each column, we will use the number 1 to present a “closed” door and an 0 to represent an “open” door. Each column will have eight values.
Finish labeling the other four walls.
Step 3
Does your picture match the one below?
You could also write the values to the left of the wall.
The picture below models the same maze on the left, only the first column has two open doors.
Step 4
Now code the status of the six columns. Store each list of numbers in a separate list. Make sure to use list {} notation to define your list.
:ClrDraw
:{1,0,1,1,0,1,1,1} →L1
:{1,0,1,1,0,1,1,1} →L2
:{1,0,1,1,0,1,1,1} →L3
… add in the code for L4 , L5 and L6.
Step 5
Now to draw the walls using pixels. Each wall segment has a length of 20 pixels.
The top wall has a 1 stored in L1(1). To code the first door we could use the following.
(3 + 0, 60)
(3 + 1, 60)
(3 + 2, 60)
…
(3 + 20, 60)
The next wall is open because 0 is stored in L1(2).
The third wall is solid because 1 is stored in L1(3).
(3 + 20*2 + 0, 120)
(3 + 20*2 + 1, 120)
(3 + 20*2 + 2, 120)
…
(3 + 20*2 + 20, 120)
The fourth wall is solid because 1 is stored in L1(4).
(3 + 20*3 + 0, 150)
(3 + 20*3 + 1, 150)
(3 + 20*3 + 2, 150)
…
(3 + 20*3 + 20, 150)
Can you write a loop to draw the walls represented in L1?
Step 6
Step 7
Code the four loops to create the four line segments for the border.
Mini Project 6: Randomize Maze Attributes
Download Teacher/Student DocsStep 1
You can skip Mini Project 6 and go straight to Mini Project 7, but then your game will be the same each time you open it. In this mini project, you’ll learn how to use the randInt() function to randomly create 1s and 0s in the six lists.
Create a new program named RMAZE.
Type the following code:
:For(A,1,8)
:randInt(0,1) → L1(A)
:Disp L1(A)
:End
Execute your code several times.
Approximately what percent of the doors will be open and closed?
How will that effect your game?
Step 2
In this step you will modify your existing code. Change the parameters for the random integer function to randInt(1,10). This will generate numbers 1 through 10.
Modify your code to match the code below. This code will generate a 0 (door) approximately 20% of the time for any given slot.
:For(A,1,8)
:If randInt(1,10) < 3
:Then
:0 →L1(A)
:Else
:1 →L1(A)
:End
:End
Create a FOR loop for each of the six lists.
Step 3
If the probability of generating a 1 is 20% for any given element, what is the probability all eight elements in a row are 1s creating one solid wall without any doors?
Since each number is independent of the next, the probability is
0.8x 0.8x 0.8x 0.8x 0.8x 0.8x 0.8x 0.8 = 0.88 ≈ 0.167.
Currently, the code will generate a column without any open spaces approximately 16.7% of the time. To fix this problem, we will check to see if the sum of all the items in the list equal 8. If the sum equals 8, all items equal 1. If this happens, code the project to randomly change one element to 0.
:If sum(L1) = 8
:Then
:0 →L1(randInt(1,8))
:End
Step 4
Now that the code randomly generates values for the lists, you need to import the code from the basic maze. After the import, delete the six lines that create the basic six lists from before.
Use recall to import the code from an image you’d like to be the PLAYER.
rcl ([2nd] [sto→]) prgm EXEC Select the MAZE file Press the enter key
Mini Project 7: Create the Final Maze Project
Download Teacher/Student DocsStep 1
You now have most of the components already coded that you need to create the final maze game. In this last mini project, you will import code from other mini projects and make some small alterations and additions to complete the maze game.
File to import | Modifications |
---|---|
PLAYER (Mini Project 4) | Only move left/right if there is a door |
RMAZE (Mini Project 6) | |
GOAL (Mini Project 3) [Sample is SMILE] | Modify the location if needed |
Step 2
1. Create a final project MZGAME.
2. Add a comment line, then import your maze created in project 6 named RMAZE using the recall method.
:”MAZE
:rcl RMAZE
3. Add a comment then import your goal object created in Mini Project 3 using the recall method. The sample was names SMILE, your file might be different.
:”GOAL
:rcl SMILE
Remove the ClrDraw from the beginning of the GOAL code.
4. Add a comment line then import your player code.
:”PLAYER CODE
:rcl PLAYER
Remove the ClrDraw from the beginning of the PLAYER code.
5. Run your code make sure you don’t have any errors. If all the objects don’t stay on the screen, go line by line through your code looking for extra ClrDraw statements. You should have a ClrDraw at the beginning.
Step 3
Can you modify your object 20 pixels when you move down vertically if it keeps your object on in the maze?
Step 4
Did your modifications to your PLAYER work? Does your PLAYER stay in the maze and only move if a wall isn’t present?
Here is one possible way to accomplish appropriate vertical movement.
Step 5
You need to modify the left and right arrow keys from the PLAYER code. You first need to know how many spaces down and over your PLAYER has traveled. Inside the WHILE loop for your key press, Type the following:
:V/20 + 1 →Y
:H/30 + 1 →X
Step 6
How do you restrict the PLAYER so it doesn’t walk through walls?
The Y value will let you know which row you are on. You’ll use this when determining if the element in list is a 0 (no wall) or a 1 (wall). The X value will let you know which list to analyze.
Step 7
Locate your If statement to move left (If K = 24) and to move right (If K = 26).
Here is one way to modify the code.
Left | Right | ||
Include L3, L4, L5 |
:If K=24 and X > 1 :Then :If X-1 = 1 AND L1(Y)=0 :Then :H-30 →H :End :If X-1 = 2 AND L2(Y)=0 :Then :H-30 →H :End … (code continues) :End |
Include L3, L4, L5 |
:If K=26 and X ≤ 6 :Then :If X = 1 AND L1(Y)=0 :Then :H+30 →H :End :If X = 2 AND L2(Y)=0 :Then :H+30 →H :End … (code continues) :End |
Check your game. Make sure it works before you continue. Fix any errors you find.
Step 8
How do you let the PLAYER know he or she has reached the GOAL? At the end your project, above the last End statement that controls the key press loop, add the following lines:
:If Y=1 and X = 7
:Then
:TextColor(BLACK)
:Text(75,110,“YOU WIN”)
:End
Step 9
Optional — Add Sound Using the TI-Innovator™ Hub
Connect your calculator to the TI-Innovator™ Hub.
Use the “Set Sound” command in each of the four IF statements for the arrow keys. You could use the same tone or a different tone for each key. Don’t forget to connect your TI-Innovator™ Hub for the sound to work.
:If K=24 and X > 1
:Send(“SET SOUND 262 Time 0.5”)
:If X-1 = 1 AND L1(Y)=0
:Then
:H-30 →H
:End
:If X-1 = 1 AND L2(Y)=0
:Then
:H-30 →H
:End
…
:End
- MP1
- MP2
- MP3
- MP4
- MP5
- MP6
- MP7
Basketball Game (New))
Mini Project 1: Draw the Background
Download Teacher/Student DocsIn this first mini project, you’ll import or create a background for your game. You will also set up the screen dimensions. You will:
- Download or draw the background image.
- Use TI-Connect to import the background.
- Display the background using code.
Step 1
Connect your calculator to your computer and run the TI Connect™ CE software application.
Open the Emulator Explorer workspace.
Import your drawing as IMAGE8.
Create a new program named BACKGRND.
TI-Connect CE software is a free download, and can be found here.
Step 2
You need to:
- Store all your graph settings so they can be restored
- Turn angle mode to degree
- Turn functions off
- Turn plots off
- Set your x minimum and x maximum
- Set your y minimum and y maximum
To do that, type the following code:
:Degree
:FnOff
:PlotsOff
:1→Xmin
:265 →Xmax
:1 →Ymin
:165 →Ymax
Step 3
You need to:
- Set the background to your image.
- Display graph
Type the following code:
Step 4
Execute your program.
Make sure the display looks similar to the one on the right.
Step 5
After executing the program you should reset your graph settings and turn off the background image. If you added the commands now, they’ll execute immediately and override your background. Therefore, you’ll add a pause. The pause will the execution of the following lines until the user presses the enter key. Pause is located in PRGM. Type the following code:
:Pause
:BackgroundOff
:RecallGDB 1
Execute the program. After pressing the enter key does the background turn off?
Mini Project 2: Draw the Net
Download Teacher/Student DocsIn this mini project, you’ll draw the net. You’ll randomly generate a height for the next basket, so each time you play the net will be at a different location. You will:
- Import the code from the background file.
- Use randInt to generate a random height for the basketball hoop.
- Use Line to draw your net.
Step 1
Create a new program named NET.
You’ll import your BACKGRND code from Mini Project 1. We import this code to help trouble shoot bugs. If at any point your program becomes too hard to understand, you can start over and import the functioning BACKGRND code.
Use recall to import the background code:
Rcl ([2nd][sto→])
prgm
EXEC
Select BACKGRND
Press the enter key
Step 2
:Pause
:BackgroundOff
:RecallGDB 1
Step 3
To make the basketball hoop, you’ll need six line segments to make the rim. You’ll need to create multiple line segments for the net.
Recall from Mini Project 1, the y values for the screen range from 1 to 165. The height of your hoop will be random. To start, you’ll let it be a random number from 30 to 100. You may adjust these numbers as you play with the game.
Step 4
To generate a random height, type:
:randInt(30,100) →Y
You’ll need three more variables to store the various heights for your basketball hoop. Storing these values will make future coding in this, and later basketball projects, easier. The screenshot to the right demonstrates where these values will occur on your hoop. Write the three lines of code to create these variables.
Step 5
To draw the yellow hexagonal rim takes six lines of code.
Each line should be in the form
:Line(X1, Y1, X2, Y2, Color)
The top line in for the demo hoop is
:Line(245, Y, 255, Y, YELLOW)
The left side is:
:Line(240, S, 245, Y, YELLOW)
:Line(240, S, 245, Z, YELLOW)
What are the three lines of code to create the base and the right side?
:
:
:
Step 6
Let’s talk Net:
There isn’t a required number of line segments for the net. The demo picture has eight. Notice they aren’t all slanted the same direction.
One sample line of code is:
:Line(240, Z, 250, N, DARKGRAY)
Code your line segments for your net. Execute your program often to check the location of your net pieces
Step 7
Execute your code several times.
Run the program several times to make sure that the position of the basket changes.
Once you are satisfied your code functions properly, delete the ClrDraw from the top of the code. We won’t need that line there for the rest of the projects.
If you deleted the three lines in Step 3, add them to the end of the program.
:BackgroundOff
:RecallGDB 1
Mini Project 3: Power Gauge
Download Teacher/Student DocsIn this mini project, you’ll code the velocity gauge. The gauge won’t change based on arrow keys yet. This is just the drawing of the gauge. You’ll import this code in two different places in MP 5. You will:
- Create a variable F to store the velocity.
- Use the Line function to create a velocity meter.
Step 1
It will be useful to have your background loaded as reference. We won’t do this in code because it is already coded in the NET code you’ll use in later projects.
Press [2nd][sto→]
Under Background, select the image that has your basketball background.
Step 2
Create a new program named GAUGE.
Add a comment line and a ClrDraw line. It will be beneficial to have the comment line when writing the final project code.
:"Gauge
:ClrDraw
Draw three line segments to outline the gauge. The height of the gauge should be 100. The width in the demo is 10. Make sure the base of your gauge matches the base of your picture. Your lines should be in the form:
:Line(X1, Y1, X2, Y2, Color)
Step 3
Does your code look similar to the code on the right? The y-values may or may not be the same based on where your background picture starts.
Now you’ll color in part of the gauge.
1. You’ll create a variable F to store a force from 1 to 100. Initialize the value of F to 30.
2. Use a FOR loop to draw lines from the base to the value of F in one color. You’ll use another FOR loop to draw line segments from F+1 to 100 in white.
Can you code both FOR loops?
Step 4
Does your code look similar to the code on the right?
Make sure your code generates a picture similar to the one below. You’ll import this code later into Mini Project 5.
Step 5
Optional: You can change the color of the power-up bar based on the value of F.
Note: If you choose to not change the color, delete the ClrDraw from the top of your program.
Option 1: Entire bar changes color based on the value of F. |
Option 2: The bar has different color segments based on the value of F. |
Step 6
:If F > 33
:Then
:ORANGE →C
:End
:If F > 66
:Then
:BROWN →C
:End
:For(A, 1, F)
:Line(12, A + 12, 19, A + 12, C)
:End
:For(A, F+1,99)
:Line(12, A + 12, 19, A + 12, WHITE)
:End
Note: Once your program is complete and functioning, delete the ClrDraw from the top of your code.
Step 7
Color Option 2: Each layer of the power gauge is based on a selection. Therefore, the IF is inside the FOR statement. Based on the results of the IF, you’ll set the color for that line. You get to decide how many IFs to include, which will determine the number of color choices. Your modification should look something like the lines below.
:For(A, 1, F)
:MAGENTA →C
:If A > 33
:Then
:ORANGE →C
:End
:If A > 66
:Then
:BROWN →C
:End
:Line(12, A + 12, 19, A + 12, C)
:End
:For(A, F+1,99)
:Line(12, A + 12, 19, A + 12, WHITE)
:End
Note: Once your program is complete and functioning, delete the ClrDraw from the top of your code.
Mini Project 4: Angle Gauge
Download Teacher/Student DocsIn this mini project, you’ll code the angle gauge. The gauge won’t change based on arrow keys yet. This is just the drawing of the gauge. You’ll import this code in two different places in MP 5. You will:
- Create a variable θ to store the shot angle.
- Use the Line function to create an angle meter.
Step 1
It will be useful to have your background loaded as reference. We won’t do this in code because it is already coded in the NET code you’ll use in later projects.
Press [2nd][ZOOM]
In Background, select the image that contains your basketball background.
Step 2
Create a new program named ARC.
Add a comment line and a ClrDraw line. It will be beneficial to have the comment line when compiling the final project.
:”ANGLE GAUGE
:ClrDraw
You’ll create the gauge by drawing a series of line segments similar to the ones on the power gauge. Instead of parallel lines like the power gauge, these lines will need to radiate from a single point as they are drawn.
Step 3
Each line segment of the arc has the same radius.
You’ll use some right triangle trigonometry to find the horizontal and vertical displacement of each line segment.
Do you know how to find the height and width of the triangle on the right?
Step 4
In a right triangle:
sin(θ)= opposite/hypotenuse cos(θ)= adjacent/hypotenuse
Therefore,
sin(θ)= y/r cos(θ)= x/r
Solve the first equation for y.
Solve the second equation for x.
Step 5
r*sin(θ)=y r*cos(θ)=x
We’ll use this relationship to draw the line segments in the angle gauge.
:Line(X1, Y1, X1 + r*cos(θ), Y1 + r*sin(θ), Color)
You’ll start with a radius of 30. You may modify this value to fit your picture.
:Line(X1, Y1, X1 + 30*cos(θ), Y1 + 30*sin(θ), Color)Step 6
:45→ θ
:For(A, 90, θ, -1)
:Line(40, 80, 40 + 30*cos(A°), 80+30∗sin(A °), WHITE)
:End
Step 7
: 45→ θ
:For(A, 90, θ, -1)
:Line(65, 95, 65 + 30*cos(A°), 95+30∗sin(A °), WHITE)
:End
:For(A, θ, 0, -1)
:Line(65, 95, 65 + 30*cos(A°), 95+30∗sin(A °), MAGENTA)
:End
Be careful. Make sure all the thetas are θ and all the zeros are 0.
Note: If you choose to skip the next three steps, delete the ClrDraw from the top of the program after you have thoroughly tested your code.
Step 8
Option 1 (Step 9):
Entire meter changes color based on the value of θ. |
Option 2 (Step 10): The meter has different color segments based on the value of θ. |
Step 9
Option 1: The entire power gauge color is based on the value of θ. Therefore, you’ll use multiple IFs to select the color before the For statement. Based on the results of the IF, you’ll set the color. You get to decide how many IFs to include, which will determine the number of color choices. Your modification should look something like the lines below.
:MAGENTA →C
:If θ > 33
:Then
:NAVY →C
:End
:If θ > 66
:Then
:GREEN →C
:End
:For(A, 90, θ, -1)
:Line(40, 80, 40 + 30*cos(A°), 80+30∗sin("A" °), WHITE)
:End
:For(A, θ, 0, -1)
:Line(40, 80, 40 + 30*cos(A°), 80+30∗sin("A" °), C)
:End
Note: Once your program is complete and functioning, delete the ClrDraw from the top of your code.
Step 10
Option 2: Each layer of the power gauge is based on a selection. Therefore, the IF is inside the FOR statement. Based on the results of the IF, you’ll set the color for that line. You get to decide how many IFs to include which will determine the number of color choices. Your modification should look something like the lines below.
:For(A, 90, θ, -1)
:Line(40, 80, 40 + 30*cos(A°), 80+30∗sin(A °), WHITE)
:End
:For(A, θ, 0, -1)
:MAGENTA →C
:If A > 33
:Then
:NAVY →C
:End
:If A > 66
:Then
:GREEN →C
:End
:Line(40, 80, 40 + 30*cos(A°), 80+30*sin(A °), C)
:End
Note: Once your program is complete and functioning, delete the ClrDraw from the top of your code.
Mini Project 5: Compile projects & code the arrow keys
Download Teacher/Student DocsIn this mini project, you’ll import the code from all four previous projects. You’ll code the left and right arrow keys to change the value of the angle gauge. You’ll also code the up and down arrow keys to change the value of the power gauge. You will:
- Add the NET code.
- Add the GAUGE code.
- Add the ARC code.
- Use getKey to change velocity and angle.
Step 1
Create a program named BB.
Import the NET code:
rcl ([2nd][sto→] )
prgm
EXEC
Select NET
Press the enter key
Delete the last three lines:
:Pause
:BackgroundOff
:RecallGDB 1
Step 2
Import the GAUGE code:
rcl ([2nd][sto→] )
prgm
EXEC
Select GAUGE
Press the enter key
Import the ARC code:
rcl ([2nd][sto→] )
prgm
EXEC
Select NET
Press the enter key
Scroll through your code. Make sure ClrDraw is only in the first couple of lines. Delete all other ClrDraw commands you find.
Step 3
You’ll use the arrow keys to change the values of F, the power level and θ the shot angle.
- If the left arrow key is pressed and θ < 88 you’ll increase θ by 2.
- If the right arrow key is pressed and θ > 2 you’ll decrease θ by 2.
- If the up arrow key is pressed and F < 100 you’ll increase F by 2.
- If the down arrow key is pressed and F > 2 you’ll decrease F by 2.
- If the left or right arrow key is pressed you’ll redraw the angle gauge.
- If the up or down arrow key is pressed you’ll redraw the power gauge.
All of this code will be embedded inside a loop that will repeat the selection statements until the ENTER key is pressed.
:”ARROW KEYS:0 →K
:While K≠105
:getKey →K
:
:MISSING IF STATEMENTS
:
Step 4
Can you code the first two IF statements inside the WHILE statement?
- If the right arrow key is pressed and θ > 2 you’ll decrease θ by 2.
- If the left arrow key is pressed and θ < 88 you’ll increase θ by 2.
:”ARROW KEYS
:0 →K
:While K≠105
:getKey →K
:
: MISSING IF STATEMENTS
:
Step 5
Does your code match the code on the right?
Can you code the next two IF statements inside the WHILE statement?
- If the up arrow key is pressed and F < 100 you’ll increase F by 2.
- If the down arrow key is pressed and F > 2 you’ll decrease F by 2.
All of this code will be embedded inside a loop that will repeat the selection statements until the ENTER key is pressed.
:”ARROW KEYS
:0 →K
:While K≠105
:getKey →K
:
: MISSING IF STATEMENTS
:
Step 6
Does your code match the code on the right?
If the left or right arrow key is pressed you’ll redraw the angle gauge. This will be easy, since you’ll import most of the code from the ARC program.
:If K=24 or K = 26
:Then
:
:
rcl ARC
Make sure to add an End to complete the If statement.
Step 7
Lastly, you need to update the power gauge.
If the up or down arrow key is pressed you’ll redraw the power gauge. This will be easy, since you’ll import most of the code from the GAUGE program.
:If K=25 or K = 34
:Then
:
:
rcl GAUGE
Make sure to add an End to complete the IF statement.
Add one more End statement to complete the WHILE statement
Mini Project 6: Toss the Ball
Download Teacher/Student DocsIn this mini project, you’ll import the code from MP5. You’ll learn the math to model the basketball’s path. You will draw the path of the ball when the user presses the enter key. You will:
- Import the code from the BB code.
- Use an IF statement to make a selection.
- Use DrawF to draw the shot.
- Use an IF statement to determine if a shot is made.
Step 1
Create a program named TOSS.
The first two lines of code will create variables to keep track of your shots made and the total number of shots attempted. Use the variable H for hits and T for tosses.
:0 →H
:0 →T
Import the BB code:
rcl ( [2nd][sto→)
prgm
EXEC
Select NET
Press the enter key
Step 2
When a ball is tossed, it has both a vertical and horizontal component as it travels. When the ball leaves the player’s hand, the velocity force (F) on the ball is split between the horizontal and vertical components.
Once the ball leaves your hand, nothing acts on it horizontally, so the equation is x = X1 + F*cos(θ)*t, where X1 is the starting x value of the ball and t is time.
The vertical component has gravity pulling it down, so it has an additional component - ½ * gravity*time2 = - 4.9*t2. Time starts when the ball leaves the hand. On Earth, gravity acceleration has the value 9.8m/s2, thus ½*gravity = 4.9. The final equation is y = Y1 + F*sin(θ)*t - 4.9*t2, where Y1 is the starting y value of the ball and t is time.
Step 3
x = X1 + F*cos(θ)*t y = Y1 + F*sin(θ)*t - 4.9*t2
In the demo code the values for X1 and Y1 are 65 and 95 respectively. Therefore,
x = 65 + F*cos(θ)*t y = 95 + F*sin(θ)*t - 4.9*t2
Can you solve for t in the x equation?
Step 4
x = 65 + F*cos(θ)*t y = 95 + F*sin(θ)*t - 4.9*t2
t=(x-65)/(F∗cos(θ))
You can combine these two equations into one with a little substitution:
y = 95 + F*sin(θ)*(x-65)/(F∗cos(θ)) - 4.9*((x-65)/(F∗cos(θ)))2
y = 95 + sin(θ)*(x-65)/(cos(θ)) - 4.9*((x-65)/(F∗cos(θ)))2
y = 95 + tan(θ)*(x-65) - 4.9*((x-65)/(F∗cos(θ)))2
If your starting X1 and Y1 values are different, make sure you use those values instead of 65 and 95 on the next step.
Step 5
Delete the last End statement at the end of your code. This ends the WHILE statement used to get a key pressed. You’ll insert an IF statement that will execute if the enter key is pressed.
Write the following lines of code:
:”SHOOT BASKET
:If K = 105
:Then
Step 6
You could simply DrawF ([2nd][[DRAW]) with the equation you found in Step 4. However, that will draw the entire parabola. We only want the part when X > 65. (Your x value may be different. Use your starting X value.) Therefore, we will add a small conditional statement to the equation.
Add the two new lines:
:If K = 104
:Then
:DrawF ((95 + tan(𝜽°)*(X-65) – 4.9*((X-65)/(F*cos(𝜽°)))2)/(X>65))
:End
Add one more End to complete the WHILE loop.
Mini Project 7: The Game
Download Teacher/Student DocsIn this mini project, you’ll code the scoring components of the game. You’ll create an equation to determine if the ball was a “SWISH” or a “MISS” and give the user feedback. You’ll also learn how to keep track of the number of baskets made overall. You will:
- Import the code from the TOSS file.
- Use an IF statement to determine if the shot is made.
- Create a flag variable to determine if a new hoop should be generated.
- Use an IF statement to determine if another hoop should be generated.
- Add another WHILE statement to continue play.
Step 1
Create a program named BBGAME.
You could extend the code in TOSS instead of creating a new file. However, it is a good idea to keep a backup of your code in case you need to start over.
Import the TOSS code:
rcl ([2nd][sto→])
prgm
EXEC
Select TOSS
Press the enter key
Make sure your code works.
Step 2
Scroll through your code. At the end of your code there is an IF statement that executes when the enter key is pressed. This section “shoots” the ball.
Now you’ll code 17 lines to determine if the shot was made or missed. You’ll update the variables H (hit) and T (toss).
Insert 17 blank lines after the DrawF function before the End.
Step 3
In order to make the shot, the ball needs to pass through the hoop. That means for some X value between 241 and 259, the function should evaluate to a y value between Y and Y+10.
You’ll create a variable, A, to store the amount to add to the score. The value of A will start at 0. You’ll check all 19 x-values to see if one of them evaluates to a Y between Y and Y + 10. If one value matches the criteria, set the variable A to 1. After you check all 19 values, add A to the number of hits.
The pseudocode looks like:
Set A to 0
Loop through all 19 x values
If Y > f(x) > Y+10
Set A to 1
Add A to H
Add 1 to T
Can you add this loop on the blank lines you created in the previous step?
Step 4
Does your code look similar to the code below?
:0 → A
:For(X, 241, 259)
:95 + tan(𝜽°)*(X-65) – 4.9*((X-65)/(F*cos(𝜽°)))2 →W
:If W < Y and W > Y-10
:Then
:1 → A
:End
:End
:A + H → H
:1 + T → T
Run your code. Make sure there aren’t any build errors. There is a slight issue with the calculation you’ll fix in the next step.
Step 5
There is a slight problem with the code. The original Y value of the hoop is lost when the DrawF command executes. Because all built-in graphing routines use the variables X and Y for graphing, the DrawF command graphs a function Y in terms of X, thus Y changes for each value of X in the domain of the window.
To fix this, we’ll create a temporary variable to store the hoop Y value before DrawF. Then we will store this value back into Y after DrawF.
Insert a line above DrawF and one below DrawF. Then add the following code:
Step 6
If the player made the shot, display “SWISH” otherwise display “MISS”.
In those remaining seven blank lines, you can:
- Set the text color to any color you like
- If the value of A indicates the shot was made, use text to display “SWISH” otherwise display “MISS”.
Note: To display the message, you’ll use the Text function in the draw menu.
Step 7
Does your code look similar to the code below?
:TextColor()
:If A = 1
:Then
:Text(80,80,“SWISH”)
:Else
:Text(80,80,“MISS”)
:End
Step 8
You have a great game already. But, wouldn’t it be nice to not have to restart the program to throw the ball at a new target? Shouldn’t you display the score?
Right now, your program does the following:
Load the background
Generate a hoop and draw the net (NET code)
Draw both gauges (GAUGE and ARC code)
While the enter key isn’t selected let the user adjust the gauges
If the arrow keys are pressed
update the appropriate gauge
If the enter key is pressed
draw the path
update H and T (you haven’t displayed these yet)
display a descriptive message
End the enter key loop
How do you think we’ll modify the program to create continued play and show the score?
Step 9
Look at the modifications below. Are they similar to what you thought?
Load the background
Generate a hoop and draw the net (NET code)
Draw both gauges (GAUGE and ARC code)
1. While the clear key isn’t pressed continue playing game
While the enter key isn’t selected let the user adjust the gauges
If the arrow keys are pressed
update the appropriate gauge
If the enter key is pressed
draw the path
update H and T (you haven’t displayed these yet)
display a descriptive message.
End the enter key loop
2. Wait until the “+” key is pressed (Use a while)
3. Generate a hoop and draw the net (NET code)
4. Draw both gauges (GAUGE and ARC code)
5. Display the score
6. End the game loop
It might look a little overwhelming at first, but there will only be a few lines of code for you to actually type. Isn’t it easy to import code using the rcl method? That’s one useful reason for modular coding. It allows you to code and debug smaller chunks of code, then import them into larger projects.
Step 10
Let’s tackle the first new section of code.
Draw both gauges (GAUGE and ARC code)
1.While the clear key isn’t pressed continue playing game
While the enter key isn’t selected let the user adjust the gauges
It should be below the 0 →K. We still need to initialize the value of K before we use it in the WHILE loop.
We’ll use the clear button, number 45, to exit the game.
:While K≠45
:While K≠105
:getKey→K
:If K = 4
:Then
:Stop
:End
Step 11
Close the BBGAME file.
|
1. While the clear key isn’t pressed continue playing game |
Step 12
The code for Steps 2–6 should go at the very end of your program. The three End statements that are already there will need to stay above the new code you enter.
Step 13
You don’t want the code to automatically execute a reset. That’s why you need an IF statement to wait for the user to decide to play a new version.
You’ll get the key press value. If it is a “+”, clear the screen.
:getKey → K
:If K=95
:Then
:ClrHome
:End
:End
Step 14
In order, insert the NET code, GAUGE code and ARC code one at a time. Remember, you’ll use the rcl method for each one. |
1. While the clear key isn’t pressed continue playing game While the enter key isn’t selected let the user adjust the gauges If the arrow keys are pressed update the appropriate gauge If the enter key is pressed draw the path update H and T (you haven’t displayed these yet) display a descriptive message End the enter key loop 2. Wait until the “+” key is pressed (Use a while) 3. Generate a hoop and draw the net (NET code) 4. Draw both gauges (GAUGE and ARC code) 5. Display the score 6. End the game loop |
Step 15
The last two steps are to display the score in the upper left corner and end the continuous play loop.
:Text(1, 1, “SCORE=”, H, “/”, T)
:End
1. While the clear key isn’t pressed continue playing game
While the enter key isn’t selected let the user adjust the gauges
If the arrow keys are pressed
update the appropriate gauge
If the enter key is pressed
draw the path
update H and T (you haven’t displayed these yet)
display a descriptive message
End the enter key loop
2. Wait until the “+” key is pressed (Use a while)
3. Generate a hoop and draw the net (NET code)
4. Draw both gauges (GAUGE and ARC code)
5. Display the score
6. End the game loop
Mini Project 8: The Trophy
Download Teacher/Student DocsIn this final mini project, you’ll create fireworks and a trophy to appear after the user sinks enough baskets. You will:
- Create a temporary file Win.
- Use FOR statements to repeat code.
- Use the TI Connect™ CE software application to import a background.
- Import the Win code into your final project.
Step 1
You could add this code directly to the end of the basketball game. However, you’d have to run through the entire game each time you needed to fix a bug or wanted to see your fireworks progress. Therefore, we are going to create a short program W that will allow you to design your fireworks and trophy code without all the other lines you have done so far.
Create a new program named WIN.
You’ll import your BACKGRND code from Project 1. We import this code so we’ll have all the settings set to the same as the game’s settings.
Use recall to import the keyboard code:
rcl ([2nd][sto→])
prgm
EXEC
Select BACKGRND
Press the enter key
Step 2
Connect your graphing calculator to your computer and open up the TI Connect™ CE software application.
Open the Emulator Explorer workspace.
Import the night sky as IMAGE7.
Import the trophy as IMAGE9.
Step 3
Change the IMAGE to Image7 (the night sky).
The image options are located in:
Vars
Picture & Background
Background
Step 4
You can place the WINNER text any where on the screen. The generic code is: Text(row, column, “text”)
The text seen at the right starts at row 90 and column 150.
:ClrDraw
:Text(90,150,“WINNER”)
Step 5
To draw the fireworks, you’ll use the Circle command. Remember, it uses Circle(x, y, radius, color)
Step 7
The loop for the first firework is:
:For(I, 0, 7)
:Circle(80 + I*4, 180 – (I-10)2, 1, NAVY)
:Circle(80 - I*4, 180 – (I-10)2, 1, NAVY)
:Circle(80 + I*10, 180 – (I-10)2, 1, LTBLUE)
:Circle(80 - I*10, 180 – (I-10)2, 1, LTBLUE)
:Circle(80 + I*25, 180 – (I-10)2, 1, WHITE)
:Circle(80 - I*25, 180 – (I-10)2, 1, WHITE)
:END
Notice how, to make the colors symmetric, one equation adds to 80 the other equation subtracts the same amount from 80.
Execute your code. Debug any errors.
Step 8
To draw the second firework:
Clear the drawing
Draw the WINNER text at (10,10)
Center the new fireworks at (180,80)
Can you draw the third firework before looking at the code on the next slide?
Step 9
The loop for the second firework is
:ClrDraw
:For(I, 0, 7)
:Circle(180 + I*4, 80 – (I-10)2, 1, RED)
:Circle(180 - I*4, 80 – (I-10)2, 1, RED)
:Circle(180 + I*10, 80 – (I-10)2, 1, ORANGE)
:Circle(180 - I*10, 80 – (I-10)2, 1, ORANGE)
:Circle(180 + I*25, 80 – (I-10)2, 1, RED)
:Circle(180 - I*25, 80 – (I-10)2, 1, RED)
:END
Execute your code. Debug any errors.
Step 10
To draw the third firework:
Clear the drawing
Draw the WINNER text at row 90, column 50
Center the new fireworks at (120,160)
Step 11
Last step before you import your code to the game, clear the drawing and display the trophy
:ClrDraw
:BackgroundOn Image9
Execute your program and debug any errors.
Step 12
Open the code for BBGAME.
Go to the end of your code. Insert four blank lines above the last End statement.
Step 13
What constitutes a winning game? One basket? Five baskets? Is 5/6 a winner but 5/100 not?
For now, you’ll say one basket is a winner so we can test the code. Insert the following lines of code. They will set the key value K to 45 which exits the overarching WHILE statement:
:IF H = 1
:Then
:45 →K
:End
Step 14
Scroll to the end of your code. Fireworks are the last thing that should happen.
Import your WIN code.
Use recall to import the keyboard code:
rcl ([2nd][sto→])
prgm
EXEC
Select WIN
Press the enter key
Step 15
Play your game a few times.
Change the winning setting to a setting of your choice.
You might say three SWISHES are a win, so your IF statement would be If H = 3.
You might say at least three SWISHES and at least 50% are a win, so your IF statement would be:
H/T →P
If H ≥3 and P ≥ 0.5
- MP1
- MP2
- MP3
- MP4
- MP5
- MP6
- MP7
- MP8
The TI-Innovator™ Piano (New)
Mini Project 1: Piano Setup
Download Teacher/Student DocsAfter completing a series of 5 mini-projects, you will have a few different ways to play a piano simulation on your calculator. You will be able to play the piano using the keys on your handheld, using the brightness sensor on the TI-Innovator Hub, using a separate ultrasonic sensor or using the ultrasonic sensor on the TI-Rover. The first mini project will be used as the base code.
In this project, you’ll:
- Import a piano picture.
- Use if statements to make selection.
- Draw a point to indicate which piano key is pressed.
- Use a FOR statement to repeat code.
Note: If you have yet to complete the Maze project, you may want to complete the Mini Project 1 from that project “Detect Which Keys Are Pressed” before you start the Piano project.
Step 1
Connect your calculator to your computer and run the TI Connect™ CE software application.* You’ll need to download the files from the folder to your computer first, and then transfer to the calculator.
Open the Emulator Explorer workspace.
Import the piano drawing as IMAGE1.
Create a new program named PIANO.
*TI Connect™ CE software is a free download.
Step 2
Your graphing calculator has a width of 165 pixels and a height of 265 pixels.
By default, the upper left corner is (0,0).
This may seem odd, but often in programming, as you go down a screen the values increase.
Step 3
You can change the orientation of the coordinate grid so (0,0) is in the lower left and the value increase as you go up the screen.
Put ClrDraw as your first line of code ( [2nd] [prgm] ). This will clear any old graphics off the screen.
Set the x values using the code:
:0 →Xmin
:264→Xmax
Set the y values using the code:
:0→Ymin
:164→Ymin
Step 4
You need to:
- Turn functions off
- Turn plots off
- Set the background to your image
The following code will accomplish the three tasks:
:FnOff
:PlotsOff
:BackgroundOn Image1
:DispGraph
Step 5
The natural (white) keys start at x = 10. Each natural key has a width of 30 pixels. You have eight natural keys in your picture.
The sharp () keys start at x = 35. Each sharp key has a width of 10 pixels. They are also 30 pixels apart. There are six sharp keys in your picture.
Step 6
To represent a pressed key, you’ll draw a point using the point on command.
To draw the point, the code is Pt-On( X, Y, Style, Color) where the point (X,Y) is the center of the point. We will use Style = 2, but feel free to explore style values from 1–7.
What would the line of code look like to draw the point to the right?
Step 7
Did your code look similar to the code below?
:Pt-On(25, 30, 2, MAGENTA)
What would the line of code look like to draw the square to the right?
Step 8
If you want to draw squares on all the natural keys, you could write eight different lines of code, or you could write a FOR loop similar to the one below.
:For(A,0,7)
:Pt-On(25 + 30*A, 30, 2, MAGENTA)
:End
Write a FOR loop to draw squares on all the sharp keys.
Hint: You’ll need an IF statement inside your loop to skip the two missing keys.
Step 9
Did your code look similar to the code below?
:For(A,0,7)
:If A≠2 and A ≠6
:Pt-On(41 + 30*A, 90, 2, MAGENTA)
:End
Now you know how to draw points on all the keys to represent playing the piano. You’ll utilize this skill in the next activity which has you incorporate key press values from the keypad to draw different points. You’ll then use key press values to play the correct note for the piano key.
Delete the loops you have written but remember formula format. You’ll use the formula later. The only base code you’ll need for the next project is shown on the right.
Mini Project 2: Playing the Piano
Download Teacher/Student DocsIn this mini project, you will be able to "play" the piano using the keys on your handheld. You will :
- Import PIANO to draw your keyboard.
- Use getKey to retrieve key press values.
- Use IF statements to make decisions.
- Use lists to store information.
- Use a WHILE statement to repeat code.
Step 1
Create a new program named PIANOKEY.
Use recall to import the keyboard code from the first mini project.
rcl ( [2nd] [sto→])
prgm
EXEC
Select PIANO
Press the enter key
Step 2
In 10 Minutes of Code for the TI-Innovator™ Hub, Unit 2, Skill Builder 3, it says that “Musical notes are determined by the frequency of a vibrating object such as a speaker, drum head, or as in a guitar or a piano. The notes of the musical scale have a special mathematical relationship. If a note has a frequency F, then the very next note has a frequency F * 21/12.”
Multiplying a note’s frequency by 21/12 gives the next note in the scale.
Step 3
We’ll turn the calculator sideways and use the lowest two rows to represent the keys on the piano keyboard.
Step 4
Middle C, represented by the [2nd] key, key 21, has a frequency of 261.64 Hz.
The next note C# has a frequency of 261.64*21/12 ≈ 277.20 Hz.
D has a frequency of 261.64*22/12 ≈ 293.68 Hz.
D# has a frequency of 261.64*23/12 ≈ 311.14 Hz.
What is the frequency for E?
What is the frequency for B?
What is the frequency for the C above middle C (key 91)?
Step 5
Middle C, our first key, has a frequency of 261.64 Hz. Using that as our base,
C = 261.64*20/12
C# = 261.64*21/12
D = 261.64*22/12
D# = 261.64*23/12
Step 6
Now to match the key number to the exponent numerator.
Complete the table below.
Can you find a mathematical pattern to relate the key number to the exponent’s numerator?
Step 7
There are several patterns in your table, but not one consistent formula that matches the key number to the exponent. Therefore, we’ll store the relationship in a list then use the list and an IF statement to make a selection. Here’s our code:
:{21,22,31,32,41,51,52,61,62,71,72,81,91,92}→L1
Now write a loop to continuously get a key press value until the user presses the clear button.
Step 8
Does your code look like the code below?
:{21,22,31,32,41,51,52,61,62,71,72,81,91,92}→L1
:0→K
:While K≠45
:getKey →K
Step 9
Now to set the frequency based on the key press. The items in the list are L1(1) = 21, L1(2) = 22 ... L1(14) = 92. We’ll write the following IF statement to determine the frequency.
For(A,1,14)
If L₁(A)=K
Then
261.24*(2^((A-1)/12)) →F
End
End
Step 10
Now to play a given frequency when a key is pressed. The generic code is Send(“SET SOUND frequency TIME time).
Insert the line Send(“SET SOUND eval(f) TIME 0.5”) into your loop right after you find the frequency.
Connect your graphing calculator to the TI-Innovator™ Hub.
Run your program. Try several different keys.
Step 11
Recall in the previous Mini Project 1, you wrote the code to highlight all the natural keys using points.
:For(A,0,7)
:Pt-On(25+30*A, 30, 2, MAGENTA)
:End
Now, you’ll write similar code. Notice all the natural keys end in a 1 because they’re in column 1 on your calculator. All the sharp keys end in a 2 because they’re in row 2.
How can you determine in code the last value of a number? If the remainder of the key number divided by 10 equals 1, the key is a natural key.
remainder(K,10) will give you the remainder
Step 12
In the previous project, you used A = 0 to 7 in your loop. Notice if we remove the 1s off the keys, they are numbered 2, 3, 4 … 9. These are in order now starting at 2.
iPart(K,10) gives you the integer part of K/10
Can you use these two pieces of information to write the FOR loop to draw the points based on the key pressed? This FOR loop should go right before the last End statement so it is inside your WHILE statement. Check your answer on the next step.
Step 13
Did you write an IF statement similar to the one below? You need ClrDraw to clear any old drawn points. Adding the comment “NATURAL KEYS is not required, but it makes it easier to debug your code.
:“NATURAL KEYS
:If remainder(K,10)=1
:Then
:ClrDraw
:iPart(K/10) - 2 →R
:Pt-On(25+ 30*R, 30, 2, MAGENTA)
:End
:End <——————Ends the WHILE K≠45 loop
Execute your code. Make sure it works before you go to the next step.
Step 14
Write a similar IF statement to draw a point on a sharp key if it is pressed.
You should insert your IF statement above the last End statement.
Remember the code to draw points on all sharp keys was:
:For(A,0,7)
:If A≠2 and A ≠6
:Pt-On(41+30*A, 90, 2, MAGENTA)
:End
Step 15
Did your code look similar to the one below?
:”SHARP KEYS
:If remainder(K,10)=2
:Then
:ClrDraw
:iPart(K/10) - 2 →R
:If R≠2 and R ≠6
:Pt-On(41+ 30*R, 90, 2, MAGENTA)
:End
:End <————— Ends the WHILE K≠45 loop
Execute your full program. You now have a functioning digital piano. In the next mini project, you’ll use the brightness sensor on the TI-Innovator™ Hub to play the piano.
Mini Project 3: Play the piano using the Brightness Sensor on the TI-Innovator
Download Teacher/Student DocsIn this next mini project, you’ll use the brightness sensor on the TI-Innovator™ Hub to play the piano. You’ll:
- Import PIANO to draw your keyboard.
- Use the brightness sensor on the TI-Innovator™ Hub.
- Use IF statements to make decisions.
- Use lists to store information.
- Use a WHILE statement to repeat code.
Step 1
Do you recall how to use the brightness sensor on the TI-Innovator™ Hub? What is the smallest value? What is the largest value?
Lets do a little investigation to find out. Create a new program named PIANOBRT.
Type the following code:
:0→K
:While K≠4
:getKey→K
:Send(“READ BRIGHTNESS “)
:Get(B)
:Output(6,1,B)
:End
Hint: A light source, such as a cell phone flashlight or lamp, might be helpful. Remember to shine it on the brightness sensor of the TI-Innovator™ Hub.
Step 2
The piano is a background image. Therefore, we only need to draw it once. You’ll insert the base code from the piano project above the code you just wrote.
Arrow up to your first line of code. Press [alpha] [graph].
Then choose Insert Line Above (three times).
:
:
:
:0→K
:While K≠45
:getKey→K
:Send(“Read Brightness “)
:Get(B)
:Output(6,1,B)
:End
Step 3
On the first line, use recall to import the keyboard code:
rcl ([2nd][sto→])prgm
EXEC
Select PIANO
Press the enter key
Execute your code. It should draw the keyboard first. Then display the brightness sensor data. The display will remove your keyboard, which is okay for now. Make sure you don’t have any errors.
Step 4
You’ll now edit the brightness sensor code inside the loop. You’ll code two frequencies: one for low light and one for more light.
We will use 10 in the demo code, but you can use any number you like based on your trial. If there’s a lot of light, we will play middle C with a frequency of 261 Hz. If there’s low light, we will play a lower note F at 174 Hz.
:
:
:
:0→K
:While K≠45
:getKey →K
:Send(“READ BRIGHTNESS“)
:Get(B)
:Output(6,1,B)
:End
Step 5
In the WHILE loop used to get the brightness, delete the last two lines of the loop.
:0→K
:While K≠45
:getKey →K
:Send(“READ BRIGHTNESS “)
:Get(B)
You’ll us an IF to play a low frequency for low light and a high frequency for more light. We will use 10 in the demo code, but you can use any number you like based on your trial from Step 1. If there’s a lot of light, we will play middle C with a frequency of 261 Hz. If there’s low light, we will play a lower note F at 174 Hz.
Code the IF statement to play a frequency of 261 Hz if the brightness is below 10, otherwise play a frequency of 174 Hz.
Step 6
Does your loop look similar to the one below?
:0→K
:While K≠45
:getKey →K
:Send(“READ BRIGHTNESS “)
:Get(B)
:If B<10
:Then
:Send("SET SOUND 174”)
:Else
:Send("SET SOUND 261”)
:End
:End
Did you try your code? Did you fix any errors? It is very important you check your code as you go. It makes finding and fixing errors easier.
Step 7
Our piano keyboard has 14 keys on it.
You could make 14 IF statements to play each note based on the light level. For example, you could say:
:If B>5 and B <15
:Then
:Send("SET SOUND 261”)
:End
That would take you 14*5 = 70 lines of code!
How could we use a mathematical relationship to help decrease the number of lines of code?
Step 8
In the first piano project, we used the equation F = 261.64*2N/12 where F is the frequency and N was a whole number exponent.
Depending on your light source, the brightness (B) can range from 0 to 100. Therefore, we need to split the range of values over 14 keys. If we split the values evenly, we get the equation:
number of keys * x = brightness range
14x = 100
x = 100/14=50/7≈7.14
That means the frequency should change each time the brightness increases by 7.14.
C should play when the brightness is from 0 to 7.14.
C# should play when the brightness if from 7.15 to 14.28.
D should play when the brightness is from 14.29 to 21.42.
Step 9
F = 261.64*2N/12
Lets say B, the brightness, is 18.
18/7.14 ≈2.52
That should play D, which had an exponent of 2.
Lets say B, the brightness, is 70.
70/7.14 ≈9.80
That should play A which, had an exponent of 9.
To get n for the formula, you need the integer part of result. To do this, you’ll use the command iPart() located in the Math menu under Num. t returns just the integer part of a result. How do you think you should modify your code to play the right note?
Step 10
Does your loop look similar to the one below?
:0→K
:While K≠45
:getKey →K
:Send(“READ BRIGHTNESS “)
:Get(B)
:iPart(B/7.14) →N
:Send("SET SOUND eval(261.14*2^(N/12))”)
:End
Did you try your code? Does it work?
Note: If you have a darker room, you might have to change your value of 7.14 to a different number to get the higher notes.
Step 11
Insert (Alpha Graph) two blank lines above the last WHILE statement.
In order to draw a point on the selected key, you need to know if the exponent numerator, N, matches a sharp or a flat.
Store these values in two different lists.
Natural list:
{0, 2, 4, 5, 7, 9, 11, 12} →L1
Sharp list:
{1, 3, -1, 6, 8, 10, -1, 13} →L2
Notice there are two negative 1s in the list. We need to save a space for the missing keys. In the code, if the value in the list is -1 we won’t draw it.
Step 12
Recall in the piano Mini Project 1, you wrote the code to highlight all the natural keys.
For(A,0,7)
Pt-On(25+30*A, 30, 2, MAGENTA)
End
Natural List:
{0, 2, 4, 5, 7, 9, 11, 12) →L1
You’ll combine these two concepts to highlight the appropriate key.
L1(1) = 0, L1(2) = 1, L1(3) = 4, L1(4) = 5 … L1(8) = 12
Insert (Alpha Graph) seven blank lines above the last End statement in your code.
:For(L, 1, 8)
:If L1(L) = N
:Then
:ClrDraw
:Pt-On(25+30*L, 30, 2, MAGENTA)
:End
:End
Step 13
Recall in Mini Project 1, you wrote the code to highlight all the sharp keys.
:For(A,0,7)
:If A≠2 and A ≠6
:Pt-On(41+30*A, 90, 2, MAGENTA)
:End
:End
Sharp List:
{{1, 3, -1, 6, 8, 10, -1, 13} →L2
You’ll combine these two concepts to highlight the appropriate key.
L2(1) = 1, L2(2) = 3, L2(3) = -1, L2(4) = 6 … L2(8) = 13
Insert (Alpha Graph) seven blank lines above the last End statement in your code.
Can you write the FOR loop and IF statement to color the appropriate sharp key? Check your answer with the next step.
Mini Project 4: Play the Piano Using the Ultrasonic Sensor
Download Teacher/Student DocsIn this mini project, you’ll use an Ultrasonic Ranger Module, an external sensor that connects directly to the TI-Innovator™ Hub as shown here. (Note: Mini Project 5 offers an alternative approach and instead uses the ranger sensor on the TI-Innovator™ Rover. You can choose either approach, depending on the availability of materials.)
You’ll:
- Import PIANOBRT to draw your keyboard and import the loop.
- Use the range sensor on the TI-Innovator™ Hub.
- Use IF statements to make decisions.
- Use lists to store information.
- Use a WHILE statement to repeat code.
Step 1
Connect your Ultrasonic Ranger Module to IN 1 on your TI-Innovator™ Hub.
Make sure it is plugged into IN 1 in the TI-Innovator™ Hub.
Step 2
Let’s make sure your ranger is working. Create a temporary project named T.
In order to get information from the Ultrasonic Ranger Module, you must first connect the TI-innovator™ Hub to the ranger. We didn’t have to do this with the brightness sensor because it is built into the TI-Innovator™ Hub.
Type the following lines of code. You’ll get most of the code in the HUB menu. Do not type the code, get the code from the menu.
:Send(“CONNECT RANGER 1 to IN 1”)
*Send(“Connect Press PRGM then it is listed under HUB
*to is located in the HUB menu under Setting
*IN 1 is located in the HUB menu under Ports
Step 3
Now type the rest of the program, after the 'Connect Ranger' command:
:0 →K
:ClrHome
:While K≠45
:getKey →K
:Send(“READ RANGER 1”)
:Get(D)
:Output(6,1,D)
:End
Execute your code. Hover your hand over the sensor. What types of values do you get?
Step 4
Create a program named PIANODST.
On the first line, use recall to import the keyboard code:
rcl ( [2nd] [sto→] )
prgm
EXEC
Select PIANOBRT
Press the enter key
Execute your code. It should draw the keyboard first, then display the brightness sensor data. The display should override your keyboard, which is okay for now. Make sure you don’t have any errors.
Step 5
Delete the three lines of code that uses the brightness sensor. You will insert code to read from the ultrasonic ranger instead.
:0 →K
:ClrHome
:While K≠45
:getKey →K
1 :Send(“READ RANGER 1”)
2 :Get(B)
3 :iPart(B/7.14) →N
:End
:Send("SET SOUND eval(261.14*2^(N/12))”)
:End
Step 6
Insert the statements to read from the Ultrasonic Ranger Module.
:0→K
1 :Send(“CONNECT RANGER 1 to IN 1”)
:While K≠45
:getKey →K
2 :Send(“READ RANGER 1”)
3 :Get(D)
:
: Send("SET SOUND eval(261.14*2^(N/12))”)
Using the numbers from your investigation and Mini Project 3, what do you think goes in the empty line to produce the various notes?
Step 7
Does your loop look similar to the one below?
:0→K
:Send(“CONNECT RANGER 1 to IN 1”)
:While K≠45
:getKey →K
:Send(“READ RANGER 1”)
:Get(D)
:iPart(D*100) →N :
:Send("SET SOUND eval(261.14*2^(N/12))”)
:End
Did you try your code? Did you fix any errors? It’s very important you check your code as you go. It makes finding and fixing errors easier.
Mini Project 5: Play the Piano Using the Rover Distance Sensor
Download Teacher/Student DocsIn this mini project, you’ll use TI-Innovator™ Hub, and the ultrasonic sensor on the TI-Innovator™ Rover. Note: This is an alternative approach to the previous mini project that used an external ranger (Ultrasonic Ranger Module) attached to the TI-Innovator™ Hub.
You will:
- Import PIANOBRT to draw your keyboard and import the loop.
- Use the range sensor on the Rover.
- Use IF statements to make decisions.
- Use a WHILE statement to repeat code.
Step 1
Let’s make sure your Rover is working. Create a temporary project named T.
Type the following lines of code. You’ll get most of the code in the ROVER menu located in the HUB menu.
:Send(“CONNECT RV”)
:getKey →K
:While K≠45
:getKey →K
:Send(“READ RV.RANGER”)
:Get(D)
:Output(6,1,D)
:End
Execute your code. Hover your hand over the sensor. What types of values do you get?
Step 2
Create a program named PIANORVR.
On the first line, use recall to import the keyboard code:
rcl ([2nd][sto→])
prgm
EXEC
Select PIANOBRT
Press the enter key
Execute your code. It should draw the keyboard first, then display the brightness sensor data. The display should override your keyboard, which is okay for now. Make sure you don’t have any errors.
Step 3
Remove the three lines of code from the brightness sensor. You’ll insert code to read from the TI-Innovator™ Rover.
:0 →K
:While K≠45
:getKey →K
1 :Send(“READ BRIGHTNESS”)
2 :Get(B)
3 :iPart(B/7.14) →N
:Send("SET SOUND eval(261.14*2^(N/12))”)
:End
Step 4
Insert the line to read from the TI-Innovator™ Rover.
:0→K
:Send(“CONNECT RV”)
:While K≠45
:getKey →K
:Send(“READ RV.RANGER”)
:Get(D)
:
: Send("SET SOUND eval(261.14*2^(N/12))”)
Using the numbers from your investigation and Mini Project 4, what do you think goes in the missing line to produce the various notes?
Step 5
If your ranger is reading in meters, most of your numbers in the temporary project were under 1. If you multiply the distance by 100, your distance values will be about the same as the brightness variables from Mini Project 3. Therefore, we can use the following code:
:0→K:Send(“CONNECT RV”)
:While K≠45
:getKey →K
:Send(“READ RV.RANGER”)
:Get(D)
:iPart(D*100/7.14) →N
:Send("SET SOUND eval(261.14*2^(N/12))”)
- MP1
- MP2
- MP3
- MP4
- MP5
Play a Musical Scale (New)
Mini Project 1: Play a Musical Scale
Download Teacher/Student DocsIn this project, you will write a program that draws a musical note on a staff that corresponds to the tone played. The brightness readings from your TI-Innovator™ brightness sensor will determine the note to play. You will:
- Draw a music staff
- Use the brightness sensor on the TI-Innovator™ Hub
- Use IF statements to make decisions
- Use a WHILE statement to repeat code
- Use lists to store patterns
Step 1
Create a new program named SCALES.
Change the orientation of the screen so (0, 0) is in the lower left and the value increase as you go up the screen.
Put ClrDraw as your first line of code ([2nd][prgm]). This will clear any old drawing off the screen.
Use the [vars] menu to set the window using the following four statements.
:0→Xmin
:10 →Xmax
:0 →Ymin
:6 →Ymin
To draw a line, the basic command is: Line(X1, Y1, X2, Y2)
Write a FOR loop to draw the five lines of the musical staff on the board.
Step 2
Does your code look similar to the code below?
:ClrDraw
:0→Xmin
:10→Xmax
:0 →Ymin
:6 →Ymin
:For(Y,1,5)
:Line(2,Y,8,Y)
:End
Step 3
This particular scale starts at E. Each note has a y-value
Store each value in a list so you can use the heights later.
{1, 1.5, 1.5, 2, 2, 2.5, 2.5, 3, 3.5, 3.5, 4, 4, 4.5}→L1
Step 4
You will also use a list to determine IF you need to draw a sharp. You will use 0 to represent a natural note and a 1 to represent a sharp note.
Store these values in a second list
:{0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0} →L2
Step 5
Create a variable K to store key press values. If the user pressed the clear key (45), the While loop repeating your code will terminate.
You need to create a variable T to store the last note played. You’ll change the frequency of the note only if it’s a new frequency.
Store 0 as the first value for K.
Store 0 as the first value for T.
Step 6
- Write a loop that repeats while K doesn’t equal the value of the clear key (45).
- Inside the loop, read the value of the brightness sensor
- Store value as variable B.
- You need to scale the brightness value B. For this example, divide B by 8 then store only the integer portion back in B. (Based on your lighting you might have to adjust this scaled value later.)
Step 7
Does your code match the code on the right?
There are 13 values stored in L1 and 13 in L2.If the brightness level is between 1 and 13 and it isn’t the note already playing, you should
- Clear the old picture
- Draw the scale
- Draw the new note
- Draw the sharp, #, if it is a sharp note
- Play the new frequency
Take a moment to brainstorm how to accomplish the five tasks above.
Step 8
Can you code the following?
If the brightness level is between 1 and 13 and it isn’t the note already playing, you should:
- Clear the old picture.
- Draw the scale.
Step 9
Does your code match the code on the right?
Remember L1 stores the y-values for all the notes.
Draw the new note as a circle with a center at (5, L1(B)) and a radius of 0.5. Set the circle color to magenta or any other color of your choosing.
Step 10
Remember L2 has 1s and 0s stored representing the sharp note.
If the value in L2(B) is a 1, draw a sharp sign.
Write the IF statement and the lines of code to draw the sharp symbol.
The sharp should only be drawn IF L2(B) = 1.
Step 12
You may recall from 10 Minutes of Code for the TI-Innovator™ Hub, Unit 2, Skill Builder 3, that “Musical notes are determined by the frequency of a vibrating object such as a speaker, drum head, or as in a guitar or a piano. The notes of the musical scale have a special mathematical relationship. If a note has a frequency F, then the very next note has a frequency F * 21/12."
Multiplying a note's frequency by 21/12 gives the next note in the scale.
The scale starts at E, so 164.82 Hz will be the base frequency. In order to get E, the exponent needs to be 0. Subtract 1 from the B value for the exponent.