Required Java Skills: ArrayList
Sample code available for educators. Get Access
In the last lesson, you used arrays to store values measured by the Finch. Other Finch programs can use lists to store values. In this lesson, you will create a way for young children to program the Finch. The Finch will have two modes. In the recording mode, a child can tilt the Finch in different directions. Your program will save this sequence of tilts in a list. In the play mode, the Finch will translate the sequence of tilts into a sequence of movements.
Write a method called findOrientation() that returns the orientation of the Finch based upon the Finch accelerometer values. The following String values should be returned: “beak up”, “beak down”, “right wing down”, “left wing down”, “level”, and “other”. For example, when the x-acceleration is less than -0.8, the method should return “beak up”.
Next, write a method to handle the recording mode. In this mode, the user will tilt the Finch in different directions (beak up, beak down, left wing down, right wing down, and level). Your goal is to record this sequence of tilts in a list called listOfOrientations. The requirements for record mode are listed below.
- The Finch should record for approximately 30 seconds. Hint: System.currentTimeMillis() will return the current system time in miliseconds.
- The robot should record the Finch’s orientation every 0.1 seconds (you can use your method from Exercise 1!). This orientation should be added to the end of listOfOrientations. You may ignore the “other” orientation.
- When the Finch is in record mode, its beak should be red.
- When the Finch leaves record mode, its beak should turn off (no light).
Write a method to handle the play mode. In this mode, the program should move through listOfOrientations. For each orientation in the list, the Finch should make a movement. The requirements for play mode are listed below.
- The function should move through each item in listOfOrientations.
- The Finch orientations (beak up, beak down, left wing down, right wing down, and level) should correspond to different movements of the robot. For example, level should correspond to stopping the Finch. You should make the other four orientations correspond to these four Finch movements: forward, back, turn right, and turn left. You may choose which orientation corresponds to each movement.
- The program should wait 0.1 seconds between items on the list.
- When the Finch is in play mode, its beak should be green.
Use your methods from the last two exercises to make the Finch record the user’s gestures and then play them. You may want to include a pause between the two modes to give the user time to set the Finch on the floor before it goes into play mode. When you have a working project, be sure to find some younger students to try it out!
Extension: Finch Plays Simon
Program your Finch to play a modified version of the memory game Simon. The Finch will print an orientation, either beak up, beak down, left wing down, or right wing down. You will then need to move the Finch to that orientation. If you do this successfully, the Finch will print a new orientation – you’ll have to move the Finch to the first orientation, then to the new one. This game goes on for as many moves as you can remember. When you finally mess up, the Finch will tell you how many moves in a row you got right and either compliment or insult your performance.
- Start by writing a method that accepts a Finch orientation and determines whether the user places the Finch in that orientation within 5 seconds.
- What components can you reuse from this lesson?
- If the user puts the Finch in the target orientation any time within 5 seconds, the function should return true.
- Otherwise, the function should return false.
- Write a method to randomly generate a new move: beak up, beak down, left wing down, or right wing down.
- The main portion of the program should include a loop that ends when the player makes an incorrect move. Within this loop, the program should do the following:
- Add a new move to a list called listOfOrientations.
- Print the move to the screen.
- Print that the user needs to repeat all of the moves done so far.
- Move through listOfOrientations and check that the user moves the Finch to each orientation in the list.
- If the player gets a move correct, the Finch’s beak should flash green for 1 second, and the Finch should beep happily.
- If the player gets the whole sequence correct, the game should continue and generate a new move for them to remember.
- If the user misses any of the movements in the list, the game is over.
- At the end of the game, the program should tell the user the maximum number of moves they successfully completed. Based on this number, the program should also compliment or insult the user.