Question: Step 2 : Upload the Code Upload the following partial code to the Arduino IDE and edit to include math functions: void setup ( )

Step 2: Upload the Code
Upload the following partial code to the Arduino IDE and edit to include math functions:
void setup(){
Serial.begin(9600); // Start Serial communication
Serial.println("Welcome to the Arduino Math Lab!");
Serial.println("Commands:");
Serial.println(" ADD x y - Add two numbers");
Serial.println(" SUB x y - Subtract two numbers");
Serial.println(" MUL x y - Multiply two numbers");
Serial.println(" DIV x y - Divide two numbers");
Serial.println("Type your command:");
}
void loop(){
if (Serial.available()>0){
String input = Serial.readStringUntil('
'); // Read input from Serial Monitor
input.trim(); // Remove any whitespace or newline characters
processInput(input); // Process the command
}
}
void processInput(String input){
// Split the input into command and arguments
String command = input.substring(0, input.indexOf(''));
String args = input.substring(input.indexOf('')+1);
// Extract numbers from the arguments
int firstSpace = args.indexOf('');
if (firstSpace ==-1){
Serial.println("Invalid command format. Use: CMD x y");
return;
}
String num1Str = args.substring(0, firstSpace);
String num2Str = args.substring(firstSpace +1);
float num1= num1Str.toFloat();
float num2= num2Str.toFloat();
// Execute the math command
if (command == "ADD"){
//CODE HERE
}
else if (command =="???"){
//CODE HERE
}
//add additional commands here
else {
//CODE HERE}
}
else {
Serial.println("Unknown command. Please use ADD, SUB, MUL, or DIV.");
}
Serial.println("Commands:");
Serial.println(" ADD x y - Add two numbers");
Serial.println(" SUB x y - Subtract two numbers");
Serial.println(" MUL x y - Multiply two numbers");
Serial.println(" DIV x y - Divide two numbers");
}
Step 3: Run the Program
Open the Serial Monitor in the Arduino IDE (Ctrl + Shift + M).
Set the baud rate to 9600.
Enter commands in the Serial Monitor in the following format:
ADD 53 to add 5 and 3. SUB 104 to subtract 4 from 10. MUL 78 to multiply 7 and 8. DIV 204 to divide 20 by 4.
Tasks:
Task 1: Perform Basic Math Operations
Use the Serial Monitor to perform:
Addition of two numbers (e.g., ADD 1510). Subtraction of two numbers (e.g., SUB 205). Multiplication of two numbers (e.g., MUL 46). Division of two numbers (e.g., DIV 183).
Observe the results printed on the Serial Monitor.
Task 2: Handle Edge Cases
Try dividing a number by zero (e.g., DIV 100) and observe the error message.
Enter invalid commands (e.g., XYZ 55) or incomplete inputs (e.g., ADD 5) and observe the program's response.
Task 3: Modify the Code
Add a new math operation (e.g., modulus MOD to calculate remainders).
Test your modification by entering commands like MOD 103.
Expected Output
When entering ADD 73, the Serial Monitor should display:
Result: 10
When entering DIV 80, the Serial Monitor should display:
Error: Division by zero is undefined.
For invalid input like XYZ 55, the Serial Monitor should display:
Unknown command. Please use ADD, SUB, MUL, or DIV.
Discussion Questions
How does the program parse and extract numbers from the input string?
Why is trim() used on the input string?
What happens if the user enters invalid input, and how can this be improved?
Assessment
Successfully perform all four math operations.
Demonstrate how the program handles edge cases.
Modify the code to include a new math function.
Key Takeaways
You should gain hands-on experience with Serial Monitor input/output.
Understand the use of strings, parsing, and basic error handling in Arduino programming.
Explore the versatility of Arduino in performing computational tasks.
Step 2 : Upload the Code Upload the following

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!