i made this code,
#include <Arduino.h>
#include <M5StickCPlus.h>
#include <math.h>
// Define button pins (adjust if needed)
#define BUTTON_A_PIN 35
#define BUTTON_B_PIN 36
// Calculator variables
double operand1 = 0;
double operand2 = 0;
char operation = ' ';
bool newNumber = true;
void setup() {
M5.begin();
M5.Lcd.setRotation(3); // Adjust rotation as needed
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setTextSize(2);
pinMode(BUTTON_A_PIN, INPUT_PULLUP);
pinMode(BUTTON_B_PIN, INPUT_PULLUP);
displayScreen();
}
void displayScreen() {
M5.Lcd.fillRect(0, 0, 240, 135, BLACK); // Clear the screen
M5.Lcd.setCursor(0, 0);
M5.Lcd.print("Value: ");
if (operation != ' ') {
M5.Lcd.print(operand1);
M5.Lcd.print(operation);
M5.Lcd.print(operand2);
} else {
M5.Lcd.print(operand1);
}
}
void calculate() {
switch (operation) {
case '+':
operand1 = operand1 + operand2;
break;
case '-':
operand1 = operand1 - operand2;
break;
case '*':
operand1 = operand1 * operand2;
break;
case '/':
if (operand2 == 0) {
M5.Lcd.setCursor(0, 20);
M5.Lcd.print("Division by 0!");
delay(2000);
operand1 = 0;
operation = ' ';
return;
}
operand1 = operand1 / operand2;
break;
case 's': // Square Root
operand1 = sqrt(operand1);
break;
case '^': // Power
operand1 = pow(operand1, operand2);
break;
default:
break;
}
operation = ' ';
operand2 = 0;
newNumber = true;
}
void loop() {
M5.update();
if (M5.BtnA.wasPressed()) {
if (newNumber) {
operand1 = operand1 * 10 + 1;
} else {
operand2 = operand2 * 10 + 1;
}
displayScreen();
}
if (M5.BtnB.wasPressed()) {
if (operation == ' ') {
operation = '+';
newNumber = false;
} else if (operation == '+') {
operation = '-';
newNumber = false;
}
else if (operation == '-') {
operation = '*';
newNumber = false;
}
else if (operation == '*') {
operation = '/';
newNumber = false;
}
else if (operation == '/'){
operation = 's'; // Square Root
newNumber = false;
}
else if (operation == 's'){
operation = '^'; // Power
newNumber = false;
}
else if (operation == '^') {
calculate();
}
displayScreen();
}
if (M5.BtnC.wasPressed()){
operand1=0;
operand2=0;
operation=' ';
newNumber=true;
displayScreen();
}
}
but for some reason, it did not work at all. please help me.