Session: Programming with Loops – Creating an Interactive Calculator with Robotics
Duration: 60 minutes
Learning Objectives:
In this session, students will:
- Understand and implement for loops to repeat a set of instructions and manage user input.
- Practice error-checking with loops to ensure valid user responses.
- Develop a basic calculator that uses Web BLE to gather user input for mathematical operations.
- Use conditional statements to handle different operations (addition, subtraction, multiplication, division).
- Add interactive components by making the robot respond with emojis, notifications, and movement based on calculations.
- Engage in real-life examples and discussions to understand how repeated instructions and error handling are used in various applications.
Equipment Required:
- M3D Robotics kit with Web BLE support
- Laptop or PC with Arduino IDE
- USB/OTG cables to connect robots
- Projector for live code demonstration (optional)
1. Introduction to For Loops and Repeating Commands (10 minutes)
Starting Discussion:
You may start by asking:
“Have you ever tried doing something repeatedly, like practicing a new dance move or saying a word over and over? Why might it be helpful for our robot to repeat instructions automatically?”
Lead into the concept:
“Just like how we repeat actions to get better, we can make the robot repeat instructions automatically using a for loop. This lets us code a set of instructions once, but have them repeated as many times as we want.”
Real-Life Examples of Loops:
“Imagine an elevator that opens and closes its doors for each floor. It repeats a set of actions every time it reaches a floor. That’s similar to how a for loop works in programming.”
Basic Code Explanation:
- Introduce the basic format of a for loop, explaining each part:
for (int i = 0; i < 5; i++) { go.forward(10); // Moves the robot forward 10 cm, 5 times }
You may ask students:
“What do you think
i < 5means in this code? Why would it stop the loop after 5 repetitions?”
Explanation: Guide them to understand that this tells the robot to repeat moving forward only 5 times.
2. Using Loops for Error-Checking with User Prompts (10 minutes)
Discussion on Input Validation:
You may ask:
“Have you ever used a form online that told you to re-enter information if something was wrong? What if we needed our robot to keep asking a question until it got a correct answer?”
Lead into the idea:
“We can use a for loop to keep prompting the user to enter a correct response, such as choosing one of the math operators (+, -, *, /). This lets us make sure the user only enters something we can use in our program.”
Example Code to Prompt User for Valid Operator:
- Introduce code with a loop that keeps asking the user to enter one of the four operators until they input a valid one.
String operator; for (;;) { // This loop continues until it receives a valid operator operator = remote.askString("Choose an operation: +, -, *, or /"); if (operator == "+" || operator == "-" || operator == "*" || operator == "/") { break; // Exit the loop if the input is valid } else { display.text("Please enter a valid operator!"); // Prompts again if input is invalid } }
Question to Students:
- “Why do you think we have the
breakcommand here? What would happen if we removed it?”
Explanation: This allows students to see that the loop would keep running without the break statement, showing the importance of having a way to exit the loop once the input is correct.
3. Building the Calculator Step-by-Step (25 minutes)
Step 1: Getting User Inputs for the Numbers
- Guide students to write code that prompts the user for two numbers. Explain each line carefully and ask guiding questions.
int num1 = remote.askInt("Enter the first number:"); int num2 = remote.askInt("Enter the second number:");
Ask students:
“What do you think would happen if we didn’t ask for these two numbers? Would the robot be able to calculate anything?”
Step 2: Writing Conditional Statements for Each Operator
- Explain how the program uses conditional statements to perform the operation the user selected.
int result; if (operator == "+") { result = num1 + num2; display.text("Result: " + String(result)); } else if (operator == "-") { result = num1 - num2; display.text("Result: " + String(result)); } else if (operator == "*") { result = num1 * num2; display.text("Result: " + String(result)); } else if (operator == "/") { result = num1 / num2; display.text("Result: " + String(result)); }
Discussion Questions:
- “Why do we need to check for each operator individually?”
- “What would happen if we skipped one of these checks?”
Explanation: Help students understand that without checking each operator individually, the program wouldn’t know which calculation to perform.
Step 3: Making the Robot Move Based on Result
- Explain how to make the robot move according to the calculation’s result.
go.forward(result); // Robot moves forward by the result in cm
Ask students:
“If the result is 20, how far will the robot go? What if the result is smaller or larger? What if it is negative?”
Explanation: This step visually reinforces the concept of using calculated results to control actions.
4. Testing and Debugging with Real-Time Interaction (10 minutes)
Activity:
- Have students test their code by entering different operators and numbers. Encourage them to test for edge cases (like entering invalid operators) to see how the error-checking works.
Teacher’s Role:
- Walk around to support students, answer questions, and help troubleshoot any issues.
5. Wrap-Up and Reflection (5 minutes)
Reflection Questions:
- “How did the for loop help us make this calculator user-friendly?”
- “Why was it important to have the loop repeat the operator question if it was incorrect?”
- "What other tasks could we use loops and conditionals for with our robot?”
Cool Fact:
“Did you know? Loops and conditionals are used in real-world applications like online shopping forms, which automatically check your information, and self-driving cars, which use loops to keep checking their environment for safety."
Learning Outcome:
By the end of this session, students will understand how to use loops and conditionals to create interactive programs, enabling them to build a simple calculator that interacts with them through Web BLE. They’ll have a solid grasp of for loops, error-checking, and conditional logic, and see the practical use of programming to create systems that respond dynamically to user input.