64.3k views
1 vote
Write an Arduino Program to: Enter ADD 5,6 or SUB 7,2 or MUL 4,9

in the serial port of the Arduino microprocessor as user input.
Then perform and execute the entered instruction.

User Explodes
by
7.2k points

1 Answer

0 votes

Final answer:

To write an Arduino program that performs different mathematical operations based on user input, you can use the Serial library in Arduino. Here's the step-by-step process.

Step-by-step explanation:

To write an Arduino program that performs different mathematical operations based on user input, you can use the Serial library in Arduino. Here's the step-by-step process:

  1. Include the Serial library at the beginning of your code: #include <SoftwareSerial.h>
  2. Define the SoftwareSerial object to communicate with the serial port: SoftwareSerial serial(10, 11);
  3. In the setup function, initialize the communication speed: serial.begin(9600);
  4. In the loop function, check if there is any data available on the serial port: if (serial.available())
  5. Read the incoming data: String input = serial.readStringUntil(',');
  6. Extract the operation and operands from the input string using string manipulation functions like substring and parseInt.
  7. Perform the desired mathematical operation based on the extracted values.
  8. Send the result back to the serial port: serial.println(result);

For example, if the user enters 'ADD 5,6' in the serial port, you can extract the operation 'ADD' and operands 5 and 6. Then, you can use the arithmetic addition operation to calculate the result, and send it back to the serial port.

User GuiFalourd
by
8.2k points