Bike Riding for Data
Investigate using mathematical functions in a program
Questions to investigate
• How can we use mathematical functions in our program?
• Ensure SPIKE Prime hubs are charged, especially if connecting through Bluetooth.
(Group Discussion, 5 minutes)
Engage students in a conversation about riding a bicycle as a mode of transportation.
Ask students to specifically talk about riding a bicycle to get from place to place, making sure to talk about the pros and cons of bicycle transportation.
Discuss places you might ride a bike, especially places that might make more sense to ride a bike then walk or take another type of transportation. Prompt students with examples if needed.
(Small Groups, 20 minutes)
Students will investigate riding a bicycle as a mode of transportation.
Direct students to the BUILD section in the SPIKE App. Here students can access the building instructions for the Smart Bike model. Ask students to build the model. The building instructions are also available at https://education.lego.com/en-us/support/spike-prime/building-instructions.
Direct students to open a new project in the Python programming canvas. Ask students to erase any code that is already in the programming area. Students should connect their hub.
Prompt students to think about how they program their Smart Bike to move. Students should write a program that allows the bike to move forward at a constant speed.
from spike import PrimeHub, MotorPair hub = PrimeHub() bike = MotorPair('E', 'C') \#move -75 , centimeters, steering 0 (straight), 25% power bike.move(-25, 'cm', 0, 25)
Moving with Math
Once students have their bike moving, challenge them to add to their program to change the speed of their bike to move faster and slower. However, they should not just add new movements to the program. Instead, students should use variables and math functions to change their speed.
Introduce the following math functions that can be used. These should all be common and familiar to students. Remind students that the order of operations will be followed in complex lines of math code.
Using these math functions, students should be able to create a program that will increase and decrease the speed of the bike.
from spike import PrimeHub, MotorPair, LightMatrix from spike.control import wait_for_seconds hub = PrimeHub() bike = MotorPair('E', 'C') \# set the bike speed and function for the bike ride bike_speed = 25 def ride(): hub.light_matrix.write(bike_speed) bike.move(-25, 'cm', 0, bike_speed) \# set conditions for bike movement and speed while True: if hub.left_button.was_pressed(): bike_speed = bike_speed - 5 ride() wait_for_seconds(3) if hub.right_button.was_pressed(): bike_speed = bike_speed + 5 ride() wait_for_seconds(3)
Allow students time to explore the program and try different values to add and subtract.
(Whole Group, 5 minutes)
Discuss with students how the program worked.
Ask students questions like:
• How can we use math functions in our program?
• How did using the math functions change the way your program worked?
• What is the benefit of controlling the speed with a variable?
The function ride was created to allow the same code to be run in two different places in the program. Creating functions helps to keep the program length at a minimum and it also allows us to make our program more efficient.
(Small Groups, 10 minutes)
Allow students to continue modifying their program to try new math functions based on what was shared from other students.
Challenge students to try adding new functions to their program to see how it changes the movements. Ask students to share out their programs.
from spike import PrimeHub, MotorPair, LightMatrix from spike.control import wait_for_seconds hub = PrimeHub() bike = MotorPair('E', 'C') bike_speed = 25 def ride(): \#the speed must be an integer, so when dividing you must round to keep the variable a whole number hub.light_matrix.write(round(bike_speed)) bike.move(-25, 'cm', 0, round(bike_speed)) while True: if hub.left_button.was_pressed(): bike_speed = bike_speed/2 ride() wait_for_seconds(3) if hub.right_button.was_pressed(): bike_speed = bike_speed * 2 ride() wait_for_seconds(3)
Allow time for students to explore the program and using different math functions.
(Group Exercise, 5 minutes)
Discuss the program with students.
Ask students questions like:
• What happened when we added mathematical functions to our program?
• What did you learn about programming mathematical functions?
• What issue was discovered when division was used?
• Why was a motor pair used when there is only one motor moving the bicycle?
• What iterations can be made to our program?
Have students answer the following in their journals:
• What did you learn today about using math functions in your program?
• What characteristics of a good teammate did I display today?
• Ask students to rate themselves on a scale of 1-3, on their time management today.
• Ask student to rate themselves on a scale of 1-3, on their materials (parts) management today.
• Program a bike model to move forward at a constant speed
• Create a program increase and decrease the speed of the bike model using math functions
• SPIKE Prime sets ready for student use
• Devices with the SPIKE App installed
• Student journals
2-CS-02 Design projects that combine hardware and software components to collect and exchange data.
2-AP-10 Use flowcharts and/or pseudocode to address complex problems as algorithms
2-AP-13 Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
2-AP-16 Incorporate existing code, media, and libraries into original programs, and give attribution.
2-AP-17 Systematically test and refine programs using a range of test cases.
2-AP-19 Document programs in order to make them easier to follow, test, and debug.