Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade
Importance and Utility of a Calculator
A calculator is a fundamental tool in various fields, including engineering, science, and mathematics. It allows users to perform complex calculations quickly and accurately. Calculators have evolved from basic handheld devices to powerful software applications, but building a calculator using Arduino can be a fun and educational project. By creating a calculator from scratch, you can gain a deeper understanding of how calculations are performed and improve your programming skills.
Project: Building a Basic Calculator
In this project, we will create a basic calculator using an Arduino board. The calculator will be able to perform addition, subtraction, multiplication, and division operations. It will have a simple user interface consisting of a keypad and an LCD display. The user will input the numbers and operations using the keypad, and the result will be displayed on the LCD.
List of Components:
Examples:
Example 1: Addition Operation
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
const byte ROWS = 4; // Number of rows in the keypad
const byte COLS = 4; // Number of columns in the keypad
char keys[ROWS][COLS] = {
{'1','2','3','+'},
{'4','5','6','-'},
{'7','8','9','*'},
{'C','0','=','/'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address, columns, rows
float num1 = 0;
float num2 = 0;
char operation = '+';
void setup() {
lcd.begin(16, 2);
lcd.print("Basic Calculator");
delay(2000);
lcd.clear();
}
void loop() {
char key = keypad.getKey();
if (key) {
if (key >= '0' && key <= '9') {
if (operation == '=') {
num1 = 0;
num2 = 0;
operation = '+';
lcd.clear();
}
num2 = num2 * 10 + (key - '0');
lcd.print(key);
}
else if (key == '+' || key == '-' || key == '*' || key == '/') {
num1 = num2;
num2 = 0;
operation = key;
lcd.print(key);
}
else if (key == '=') {
float result = 0;
switch (operation) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
}
lcd.setCursor(0, 1);
lcd.print(result);
num1 = result;
num2 = 0;
operation = '=';
}
else if (key == 'C') {
num1 = 0;
num2 = 0;
operation = '+';
lcd.clear();
}
}
}
Example 2: Subtraction Operation
// Same code as Example 1, just replace '+' with '-' in the key matrix
Example 3: Multiplication Operation
// Same code as Example 1, just replace '+' with '*' in the key matrix
Example 4: Division Operation
// Same code as Example 1, just replace '+' with '/' in the key matrix