Skip to content

Raspberry Pi Calculator Project

27 Nov 2023
Raspberry Pi Calculator Project

Electronics Calculator

Hey there!

In the exciting world of technology, innovation is constantly pushing the limits of what we thought was possible. And now, we have something truly amazing to share with you: the Raspberry Pi Calculator! This groundbreaking project is about to revolutionize the way we approach math problem-solving. Picture this: you're sitting at your computer, faced with a tricky math problem. But instead of scratching your head and feeling overwhelmed, all you have to do is connect the Picobricks Calculator to your computer. And just like magic, a message pops up saying, "Picobricks Calculator Ready to Solve." It's as simple as that! No more struggling or stressing over math problems. With the Picobricks Calculator, you can embark on a journey of effortless problem-solving. So get ready to say goodbye to those frustrating moments and start coding a calculator!

Details And Algorithms

The process is incredibly simple! With the Electronics Calculator, you'll have all the essential operations right at your fingertips. It's not your ordinary calculator; it's a game-changer! No more tedious keypad inputs. Instead, you'll navigate through your mathematical journey with the precision and ease of an experienced explorer.

Adding numbers together? Just give the up arrow a gentle push. Need to subtract? The down arrow is there to help. For multiplication, use the right arrow, and for division, the left arrow is ready to assist.

Once you've entered your equation, a single press of the square button reveals your solution. It's quick, accurate, and, most importantly, user-friendly.

This project is a huge step forward in simplifying math problem-solving for everyone. Whether you're a student tackling tough coursework, a professional dealing with complex calculations, or simply someone who loves the beauty of math, the Picobricks Calculator is here to make your life easier.

Come join us as we delve into the fascinating world of this amazing calculator project! We'll take a closer look at the technology that powers it, its numerous applications across different fields, and the incredible impact it's going to have on how we tackle math problems. Get ready to witness a new era in mathematics, where a simple remote control and a few carefully selected arrows will effortlessly lead us to solutions. Welcome to the future of math, one Picobricks at a time!

Components

1 x PicoBricks

Wiring Diagram 

Calculator project wiring

MicroPython Code of Calculator

from machine import Pin,Timer,I2C
import utime
from picobricks import SSD1306_I2C
import framebuf
 
debug=True
 
i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=200000)
oled = SSD1306_I2C(128, 64, i2c)
 
keyName = [['1','2','3','+'],
           ['4','5','6','-'],
           ['7','8','9','*'],
           ['c','0','=','/']]
keypadRowPins = [16,15,14,13]
keypadColPins = [9,8,3,2]
 
row = []
col = []
keypadState = [];
for i in keypadRowPins:
    row.append(Pin(i,Pin.IN,Pin.PULL_UP))
    keypadState.append([0,0,0,0])
for i in keypadColPins:
    col.append(Pin(i,Pin.OUT))
 
def solve(oprt, oprdA, oprdB):
    if(oprt == "+"):
        return oprdA + oprdB
    elif(oprt == "-"):
        return oprdA - oprdB
    elif(oprt == "*"):
        return oprdA * oprdB
    elif(oprt == "/"):
        return round(oprdA / oprdB , 6)
 
def calc(lst):
    operand = []
    operator = []
   
    for i in lst:
        if(debug):
            print(i)
        if(i=='+'):
            while (len(operator)!=0 and (operator[-1] == '*' or operator[-1] == '/' or operator[-1] == '-' or operator[-1] == '+')):
                b = operand.pop(-1)
                a = operand.pop(-1)
                c = operator.pop(-1)
                operand.append(solve(c,a,b))
            operator.append(i)
        elif(i=='-'):
            while (len(operator)!=0 and (operator[-1] == '*' or operator[-1] == '/' or operator[-1] == '-' or operator[-1] == '+')):
                b = operand.pop(-1)
                a = operand.pop(-1)
                c = operator.pop(-1)
                operand.append(solve(c,a,b))
            operator.append(i)
        elif(i=='*'):
            while (len(operator)!=0 and (operator[-1] == '*' or operator[-1] == '/')):
                b = operand.pop(-1)
                a = operand.pop(-1)
                c = operator.pop(-1)
                operand.append(solve(c,a,b))
            operator.append(i)
        elif(i=='/'):
            while (len(operator)!=0 and (operator[-1] == '*' or operator[-1] == '/')):
                b = operand.pop(-1)
                a = operand.pop(-1)
                c = operator.pop(-1)
                operand.append(solve(c,a,b))
            operator.append(i)
 
        elif(i=='('):
            operator.append(i)
 
        elif(i==')'):
            while(operator[-1] !='('):
                b = operand.pop(-1)
                a = operand.pop(-1)
                c = operator.pop(-1)
                operand.append(solve(c,a,b))
            operator.pop(-1)
        else:
            operand.append(i)
           
    while(len(operator) != 0):
        b = operand.pop(-1)
        a = operand.pop(-1)
        c = operator.pop(-1)
        operand.append(solve(c,a,b))
 
    return operand[0]
 
def keypadRead():
    global row
    j_ifPressed = -1
    i_ifPressed = -1
    for i in range(0,len(col)):
        col[i].low()
        utime.sleep(0.005) #settling time
        for j in range(0,len(row)):
            pressed = not row[j].value()
            if(pressed and (keypadState[j][i] != pressed)): #state changed to high
                keypadState[j][i] = pressed
            elif(not pressed and (keypadState[j][i] != pressed)): # state changed to low
                keypadState[j][i] = pressed
                j_ifPressed = j
                i_ifPressed = i
        col[i].high()
    if(j_ifPressed != -1 and i_ifPressed != -1):
        return keyName[j_ifPressed][i_ifPressed]
    else:
        return -1
 
def printOled(lst):
    oledPos = {
            "x" : 0,
            "y" : 0
          }
   
    oled.fill(0)
    string = ''
    for i in lst:
        string += str(i)
    l = 0
    while(l='0') or key == '.'):
                inputList[-1] = inputList[-1] + key            
            elif(key == '+' or key == '-' or key == '*' or key == '/'):
                if(inputList != ['']):
                    if(inputList[-1] == '' and (inputList[-2] == '+' or inputList[-2] == '-' or inputList[-2] == '*' or inputList[-2] == '/')):
                        inputList[-2] = key
                    elif(inputList[-1]==''):
                        inputList[-1]=key
                        inputList.append('')
                    else:
                        inputList[-1] = float(inputList[-1])
                        inputList.append(key)
                        inputList.append('')
                   
            elif(key == 's'):
                shiftFlag = not shiftFlag
            elif(key == 'a'):
                if(shiftFlag):      
                    if(inputList[-1] != ''):
                        inputList[-1] = float(inputList[-1])
                        inputList.append(')')
                        inputList.append('')
                    else:
                        inputList[-1] = ')'
                        inputList.append('')
                    shiftFlag = False
                else:              
                    signFlag = not signFlag
                    if(inputList[-1] == ''):
                        inputList[-1] = '-'
                    else:
                        if(inputList[-1][0] == '-'):
                           inputList[-1]  = inputList[-1][1:]
                        else:
                            inputList[-1] = '-' + inputList[-1]
               
            elif(key == 'b'):
                if(shiftFlag):      
                    if(inputList[-1] == ''):
                        inputList[-1] = '('
                    else:
                        inputList.append('(')
                    inputList.append('')
                    shiftFlag = False
                else:              
                    if(inputList[-1] == ''):
                        inputList[-1] = 3.14
                    else:
                        inputList.append(3.14)
                    inputList.append('')
            elif(key == 'c'):
                if(shiftFlag):      
                    inputList = ['']
                    shiftFlag = False
                else:              
                    if(inputList == ["error"]):
                        inputList = ['']
                    if(inputList != ['']):  
                        if(inputList[-1] == ''):
                            inputList.pop()
                            inputList[-1] = str(inputList[-1])[:-1]
                        else:
                            inputList[-1] = str(inputList[-1])[:-1]
            elif(key == '='):
                if(inputList[-1] == ''):
                    inputList.pop(-1)
                elif(inputList[-1] != ')'):
                    inputList[-1] = float(inputList[-1])
                try:
                    ans = calc(inputList)
                    inputList = [str(ans)]
                except:
                    ans = ''
                    inputList = []
                    inputList.append("error")
               
            printOled(inputList)
            print(inputList)

PicoBricks Code of Raspberry Pi Calculator

Because the code is long, we prepared a download button for you. Maybe you can try it with your PicoBrick. You can download it from the button below. 

DOWNLOAD

 

Arduino Code of Electronics Calculator

#include 
#include “ACROBOTIC_SSD1306.h”
#include 
#include 
#define BUTTON_PIN 10
#define IR_RECEIVE_PIN 0

bool secondNumber = false;
bool firstNumber = true ;
bool calculated = false;
String number1 = ” “;
String number2 = ” “;
char operation;
String calculation;
String a;

void writeScreen() {
  if (!secondNumber && !firstNumber) {
    oled.clearDisplay();
    oled.setTextXY(0, 0);
    oled.putString(number1);
    oled.setTextXY(0, (number1.length() + 1));
    oled.putString(String(operation));
  } else if (secondNumber && calculated) {
    Serial.print(“Calculation Should be printed: “);
    Serial.println(calculation);
    oled.setTextXY(3, 5);
    oled.putString(calculation);
  } else if (firstNumber && !secondNumber) {
    oled.clearDisplay();
    oled.setTextXY(0, 0);
    oled.putString(number1);
    oled.setTextXY(0, (number1.length() + 3));
    oled.putString(number2);
  } else if (!firstNumber && secondNumber) {
    oled.clearDisplay();
    oled.setTextXY(0, 0);
    oled.putString(number1);
    oled.setTextXY(0, (number1.length() + 1));
    oled.putString(String(operation));
    oled.setTextXY(0, (number1.length() + 3));
    oled.putString(number2);
  }
}

void writeChc(char character) {
  if (firstNumber && !secondNumber) {
    // The first number will be entered, nothing else will be entered!
    if (isDigit(character)) {
      number1 += character;
    } else if (!(number1 == ” “)) {
      operation = character;
      firstNumber = false;
    }
  } else if (!firstNumber && ! secondNumber) {
    // Only the operation will be entered, if a new one is entered, the old operation will be deleted and updated.
    // If the number is entered, number2 will be assigned to the second number and will be set to true.
    if (isDigit(character)) {
      number2 += character;
      secondNumber = true;
    } else if (!(number1 == ” “)) {
      operation = character;
    }
  } else if (!firstNumber && secondNumber) {
    if (character == ‘c’) {
      long int n1 = number1.toInt();
      long int n2 = number2.toInt();
      calculated = true;
      if (operation == ‘*’) {
        calculation = String (n1 * n2);
      } else if (operation == ‘/’) {
        calculation = String (n1 / n2);
      } else if (operation == ‘+’) {
        calculation = String (n1 + n2);
      } else if (operation == ‘-‘) {
        calculation = String (n1 – n2);
      }
    } else {
      number2 += character;
    }
  }
  writeScreen();
}

void setup() {
  Serial.begin(9600);
  Wire.begin();
  oled.init();
  oled.clearDisplay();
  pinMode(0, INPUT);
  pinMode(BUTTON_PIN, INPUT);
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
}

void loop() {
  delay(500);
  if (IrReceiver.decode()) {
    int a = (IrReceiver.decodedIRData.decodedRawData);
    Serial.println(a);
    IrReceiver.resume(); // Receive the next value
    if (a == -417792256) {
      writeChc(‘*’);
    } else if (a == -1387069696) {
      writeChc(‘+’);
    } else if (a == -150405376) {
      writeChc(‘/’);
    } else if (a == -1520763136) {
      writeChc(‘-‘);
    } else if (a == -484638976) {
      writeChc(‘C’);
    } else if (a == -1169817856) {

      writeChc(‘1’);
    } else if (a == 1186529536) {
      writeChc(‘2’);
    } else if (a == -1203241216) {
      writeChc(‘3’);
    } else if (a == -1153106176) {
      writeChc(‘4’);
    } else if (a == -1086259456) {
      writeChc(‘5’);
    } else if (a == -1136394496) {
      writeChc(‘6’);
    } else if (a == -1153106176) {
      writeChc(‘7’);
    } else if (a == -367657216) {
      writeChc(‘8’);
    } else if (a == -167117056) {
      writeChc(‘9’);
    } else if (a == -434503936) {
      writeChc(‘0’);
    }
  }
  while (calculated) {
    writeScreen();
    oled.setTextXY(5, 0);
    oled.putString(“Press picobricks button to new start”);
    if (digitalRead(BUTTON_PIN)) {
      number1 = ” “;
      number2 = ” “;
      operation = ‘ ‘;
      calculation = ” “;
      calculated = false;
      secondNumber = false;
    }
  }
}
Prev Post
Next Post

Thanks for subscribing!

This email has been registered!

Shop the look
Choose Options

Edit Option

Back In Stock Notification

Compare

Product SKURatingDescription Collection Availability Product Type Other Details
this is just a warning
Login
Shopping Cart
0 items
Same Day Shipping No Extra Costs
Easy Returns Guarantee Return with Ease
Secure Checkout Secure Payment