🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    calc software is not working

    Scheduled Pinned Locked Moved M5 Stick/StickC
    1 Posts 1 Posters 827 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J Offline
      JasonBeBrilliant
      last edited by

      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.

      1 Reply Last reply Reply Quote 0

      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

      With your input, this post could be even better 💗

      Register Login
      • First post
        Last post