Wednesday, December 18, 2024

Calculator_Sunil_Singh_Serial_Monitor

#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Keypad configuration
const byte ROWS = 4, COLS = 4;
char keys[ROWS][COLS] = {
  {'1','2','3','+'},
  {'4','5','6','-'},
  {'7','8','9','*'},
  {'C','0','=','/'}
};
byte rowPins[ROWS] = {9,8,7,6};
byte colPins[COLS] = {5,4,3,2};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

// LCD configuration
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Calculator variables
String input = "";
float num1 = 0, num2 = 0;
char operatorSymbol = ' ';
bool resultShown = false;
bool enteringSecondOperand = false;

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("Calculator");
  delay(1000);
  lcd.clear();
  Serial.println("Setup complete — ready.");
}

void loop() {
  char key = keypad.getKey();
  if (key != NO_KEY) {
    Serial.print("Key pressed: ");
    Serial.println(key);

    if (resultShown) {
      lcd.clear();
      resultShown = false;
      Serial.println("LCD cleared (after showing result).");
    }

    if (isdigit(key) || key == '.') {
      input += key;
      Serial.print("Building number: ");
      Serial.println(input);

      if (enteringSecondOperand) {
        lcd.setCursor(String(num1).length() + 1 + input.length() - 1, 0);
      } else {
        lcd.setCursor(input.length() - 1, 0);
      }
      lcd.print(key);
    }
    else if (key == '+' || key == '-' || key == '*' || key == '/') {
      num1 = input.toFloat();
      operatorSymbol = key;
      input = "";
      enteringSecondOperand = true;
      lcd.setCursor(String(num1).length(), 0);
      lcd.print(operatorSymbol);

      Serial.print("First operand: ");
      Serial.println(num1);
      Serial.print("Operator: ");
      Serial.println(operatorSymbol);
    }
    else if (key == '=') {
      num2 = input.toFloat();
      input = "";
      enteringSecondOperand = false;

      Serial.print("Second operand: ");
      Serial.println(num2);

      float result = 0;
      switch (operatorSymbol) {
        case '+': result = num1 + num2; break;
        case '-': result = num1 - num2; break;
        case '*': result = num1 * num2; break;
        case '/': result = num1 / num2; break;
        default:
          lcd.setCursor(0, 1);
          lcd.print("Error");
          delay(1000);
          lcd.clear();
          Serial.println("Error: invalid operator.");
          return;
      }

      lcd.setCursor(0, 1);
      lcd.print("Result: ");
      lcd.print(result);
      resultShown = true;

      Serial.print("Result displayed: ");
      Serial.println(result);
    }
    else if (key == 'C') {
      input = "";
      num1 = num2 = 0;
      operatorSymbol = ' ';
      enteringSecondOperand = false;
      resultShown = false;
      lcd.clear();

      Serial.println("Cleared all inputs.");
    }
  }
}

No comments:

Post a Comment

MUD Three Mode operation Manual Automatic GPS

 Code for three mode operation: /*   3-Mode Headlight Controller   - Manual mode (driver uses a toggle to pick high/low)   - Auto mode (LDR...