187k views
4 votes
Modify the program so that it can do any prescribed number of multiplication operations (at least up to 25 pairs). The program should have the user first indicate how many operations will be done, then it requests each number needed for the operations. When complete, the program should then return "done running" after displaying the results of all the operations in order. It is in the Arduino Language

You can write the code from scratch if you'd like without modifying the given code. In any expected output.

//Initializing the data
float num1 = 0;
float num2 = 0;
int flag1 = 0;

// a string to hold incoming data
String inputString = " ";
// Whether the string is Complete
boolean stringComplete = false;
//This is where the void setup begins
void setup() {
//Initializing the serial
Serial.begin (9600);
//Reserving 200 bytes for the inputString
inputString.reserve (200);
Serial.println("Provide The First Number");
}

void loop() {
//Print the string when the new line arrives
if (stringComplete) {
if (flag1 == 0) {
Serial.println(inputString);
num1 = inputString.toFloat( );
flag1 = 1;
//Asking the user to input their second string
Serial.println("Provide The Second Number");
}
else if (flag1 == 1) {
Serial.println(inputString);
num2 = inputString.toFloat( );
flag1 = 2;
Serial.println("The Product is Calculating...");
}
if (flag1 == 2) {
Serial.println("The Product is: ");
Serial.println(num1*num2);
Serial.println("Press Reset");
flag1 = 5;
}
//This clears the string
inputString = " ";
stringComplete = false;
}
}

void serialEvent() {
while (Serial.available( )) {
// get the new byte
char inChar = (char)Serial.read( );
//add it to the inputString
inputString += inChar;
//if the incoming character is a newline, set a flag so the main loop can
//do something about it

if (inChar == '\\') {
stringComplete = true;
}
}
}

User FrankBr
by
7.0k points

1 Answer

4 votes

Final answer:

The provided Arduino program was modified to allow user input for the total number of multiplication operations. It prompts the user, performs each multiplication as numbers are input, and outputs results immediately after each operation, indicating completion with 'Done running.'

Step-by-step explanation:

The student is working with a program written in the Arduino programming language, which involves performing multiple multiplication operations. To modify the program to handle a user-specified number of operations, we need to include a loop and some condition checks to process multiple pairs of numbers. Below is an example sketch that allows the user to specify the number of multiplication operations to perform, input each pair of numbers, and then output the results in order:

int numOperations = 0;
int count = 0;
float result;

void setup() {
Serial.begin(9600);
Serial.println("Enter the number of multiplication operations:");
}

void loop() {
if (Serial.available() > 0) {
if (numOperations == 0) {
numOperations = Serial.parseInt();
Serial.println("Now enter pairs of numbers to multiply:");
} else if (count < numOperations * 2) {
float num = Serial.parseFloat();
if (count % 2 == 0) {
result = num;
} else {
result *= num;
Serial.print("Result ");
Serial.print((count / 2) + 1);
Serial.print(": ");
Serial.println(result);
if ((count / 2) + 1 == numOperations) {
Serial.println("Done running.");
}
}
count++;
}
}
}
This code replaces the existing loop and setup functions. It first prompts the user for the total number of operations and then reads the pairs of numbers from the serial input, calculates the product, and outputs the result immediately after each pair is entered. When all operations are complete, it prints "Done running." to the serial monitor.
User Dimnnv
by
7.2k points