Simple Calculator

Introduction

Simple calculator is a project to allow users to calculate basic four operations in mathematics( addition, subtraction, multiplication, division) by using the uLCD as the screen and two capacitive touch sensors as number pad and operator pad. However, the input has to be in the form "number1 operator1 number2 operator2 number3" (i.e 2+4*10). The input values can be from any integer to even a number with decimals. Moreover, this calculator is smart enough to operate multiplication/division before addition/subtraction, in another word it is implemented with the order of precedence logic. This calculator is implemented with two capacitive sensors MPR121(http://developer.mbed.org/users/4180_1/notebook/mpr121-i2c-capacitive-touch-sensor/) and uLCD (http://developer.mbed.org/users/4180_1/notebook/ulcd-144-g2-128-by-128-color-lcd/) on the Mbed.

Circuit Diagram

/media/uploads/shyguykjh/lab04circuit2.jpg

Components

  • 1. MPR121 Capacitive Touch Sensor
  • 2. uLCD

Pin Layout

/media/uploads/shyguykjh/pin_layout.jpg

Possible future implementation

  • 1. Implementing more inputs(more number/more operations)
  • 2. Implementing more operators (square roots, powers, logarithms)
  • 3. Implementing complex calculation

Code

http://developer.mbed.org/users/shyguykjh/code/PublishingLab04/file/f8d011978c8d/main.cpp

It can be found in the main.cpp on this link above

include the mbed library with this snippet

#include "mbed.h"
#include "uLCD_4DGL.h"
#include <iostream>
#include <math.h>
#include <cmath>
#include <string>
#include <cstring>
#include <list>
#include <vector>
#include <mpr121.h>
Serial pc(USBTX, USBRX);
using namespace std;
//
uLCD_4DGL uLCD(p13, p14, p11);
InterruptIn interrupt(p26);
InterruptIn interrupt2(p25);
I2C i2c(p9, p10);
I2C i2c2(p28, p27);
//
Mpr121 numpad(&i2c, Mpr121::ADD_VSS);
Mpr121 operpad(&i2c2, Mpr121::ADD_VSS);
 
int fallInterrupt() 
{
     int num = numpad.read(0x00);
     num +=numpad.read(0x01)<<8;
     int i=0;
     i = 0;
     for (i=0; i<12; i++) {
        if (num & (1<<i))
        break;
     }
     if(i == 0) {
        uLCD.text_width(2); //4X size text
        uLCD.text_height(2);
        uLCD.color(BLUE);
        uLCD.printf("%d", i);
     }
     else if (i == 1) {
        uLCD.text_width(2); //4X size text
        uLCD.text_height(2);
        uLCD.color(BLUE);
        uLCD.printf("%d", i);
     }
     else if (i == 2) {
        uLCD.text_width(2); //4X size text
        uLCD.text_height(2);
        uLCD.color(BLUE);
        uLCD.printf("%d", i);
         }
     else if (i == 3) {
        uLCD.text_width(2); //4X size text
        uLCD.text_height(2);
        uLCD.color(BLUE);
        uLCD.printf("%d", i);
         }
     else if (i == 4) {
        uLCD.text_width(2); //4X size text
        uLCD.text_height(2);
        uLCD.color(BLUE);
        uLCD.printf("%d", i);
         }
     else if (i == 5) {
        uLCD.text_width(2); //4X size text
        uLCD.text_height(2);
        uLCD.color(BLUE);
        uLCD.printf("%d", i);
         }
     else if (i == 6) {
        uLCD.text_width(2); //4X size text
        uLCD.text_height(2);
        uLCD.color(BLUE);
        uLCD.printf("%d", i);
         }
     else if (i == 7) {
        uLCD.text_width(2); //4X size text
        uLCD.text_height(2);
        uLCD.color(BLUE);
        uLCD.printf("%d", i);
         }
     else if (i == 8) {
        uLCD.text_width(2); //4X size text
        uLCD.text_height(2);
        uLCD.color(BLUE);
        uLCD.printf("%d", i);
         }
     else if (i == 9) {
        uLCD.text_width(2); //4X size text
        uLCD.text_height(2);
        uLCD.color(BLUE);
        uLCD.printf("%d", i);
         }
     else if (i == 10) {
        uLCD.text_width(2); //4X size text
        uLCD.text_height(2);
        uLCD.color(BLUE);
        uLCD.printf(".");
         }
     else if (i == 11) {
        uLCD.text_width(2); //4X size text
        uLCD.text_height(2);
        uLCD.color(BLUE);
        uLCD.printf("-");
         }
//        pc.printf("%d", i);
    return i;
}
 
int fallInterrupt2() 
{
     int num = operpad.read(0x00);
     num +=operpad.read(0x01)<<8;
     int i =0;
     i = 0;
     for (i=0; i<12; i++) {
        if (num & (1<<i))
        break;
     }
     if(i == 1) {
        uLCD.printf("+");
     }
     else if (i == 2) {
        uLCD.printf("-");
     }
     else if (i == 3) {
         uLCD.printf("*");
         }
     else if (i == 4) {
         uLCD.printf("/");
    }
//    else if (i == 4) {
//         uLCD.printf("/");
//         }
     else if (i == 5) {
         uLCD.color(RED);
         uLCD.printf("\n=");
     }
     else if (i == 6) {
         uLCD.cls();
    }
    return i;
}
 
int main() {
    while (1) {
//  C;
//  interrupt2.fall(&fallInterrupt2);
//  interrupt.fall(&fallInterrupt);
//  interrupt.mode(PullUp);
//  interrupt2.mode(PullUp);
  float temp = 0;
  float temp1 = 0;
  int temp2 = 0;
  float temp3 = 0;
  int temp4 = 0;
  float temp5 = 0;
  int temp6 = 0;
  int count = 1;
  int count2 = 1;
  int count3 = 1;
  int fans = 10;
  int fans2 = 10;
  int fans3 = 10;
  int negate = 0;
  while (1) {
         int oper1 = operpad.read(0x00);
         oper1 +=operpad.read(0x01)<<8;
         int num1 = numpad.read(0x00);
         num1 +=numpad.read(0x01)<<8;
//         temp2 = fallInterrupt2();
//         wait(0.1);
         if (oper1){
//             wait(0.3);
            int ope = fallInterrupt2();
            wait(0.2);
            if (ope == 0) {
                uLCD.cls();
                uLCD.printf("Cleared");
                wait(0.5);
                uLCD.cls();
                count = 1;
                temp1 = 0;
                temp2 = 0;
                fans = 10;
            }
            else{
                temp2 = ope;
                break;
             }
         }
         else if (num1){
                int ans = fallInterrupt();
                wait(0.2);
                if (ans == 10) {
                    count = count +1;
//                    pc.printf("hi I am in ans==10");
//                    pc.printf("hi count from ans ==10 is %d\n", count);
                    }
                else if (ans == 11) {
                    negate = 1;
                    }
                else {
                    if (count == 2) {
                        if (negate == 1){
//                        pc.printf("ans = %d\n", ans);
//                        pc.printf("count = %d\n", count);
//                        pc.printf("temp1 before = %0.2f\n", temp1);
                            temp1 = temp1 *10;
                            temp1 = (temp1 - ans)/10;
                            temp1 = temp1;
//                        pc.printf("temp1 after = %0.2f\n", temp1);
                             count = count +1;
                            fans = fans * 10;
                        }
                        else{
                            temp1 = temp1 *10;
                            temp1 = (temp1 + ans)/10;
//                        pc.printf("temp1 after = %0.2f\n", temp1);
                            count = count +1;
                            fans = fans * 10;
                        }
                    }
//                        wait(0.3);
                    else if (count > 2) {
                        if (negate == 1){
                            temp1 = temp1 * fans;
                            temp1 = temp1 - ans;
//                        pc.printf("ans = %d\n", ans);
                            temp1 = temp1/fans;
                            temp1 = temp1;
//                        pc.printf("temp1 after = %0.2f\n", temp1);
                            fans = fans * 10;
                        }
                        else{
                            temp1 = temp1 * fans;
                            temp1 = temp1 + ans;
//                        pc.printf("ans = %d\n", ans);
                            temp1 = temp1/fans;
//                        pc.printf("temp1 after = %0.2f\n", temp1);
                            fans = fans * 10;
                        }    
                    }
                    else if(count==1){
                        if (negate == 1){
                            temp1 = temp1 * 10;
                            temp1 = temp1 - ans;
                            temp1 = temp1;
                        pc.printf("temp1 = %d\n", temp1);
                        pc.printf("count = %d\n", count);
                        }
                        else {
                            temp1 = temp1 * 10;
                            temp1 = temp1 + ans;
                        }
                   }
//                    wait(0.3);
                }
        wait(0.2);
        }
    }
    while (1) {
         int oper2 = operpad.read(0x00);
         oper2 +=operpad.read(0x01)<<8;
         int num2 = numpad.read(0x00);
         num2 +=numpad.read(0x01)<<8;
//         temp4 = fallInterrupt2();
//         wait(0.1);
         if (oper2){
//             wait(0.3);
            int ope = fallInterrupt2();
            wait(0.2);
            if (ope == 0) {
                uLCD.cls();
                uLCD.printf("Cleared");
                wait(0.5);
                uLCD.cls();
                count2 = 1;
                temp3 = 0;
                temp4 = 0;
                fans2 = 10;
            }
            else{
                temp4 = ope;
                break;
             }
         }
         else if (num2){
                int ans = fallInterrupt();
                wait(0.2);
                if (ans == 10) {
                    count2 = count2 +1;
//                    pc.printf("hi I am in ans==10");
//                    pc.printf("hi count from ans ==10 is %d\n", count);
                    }
                else {
                    if (count2 == 2) {
//                        pc.printf("ans = %d\n", ans);
//                        pc.printf("count = %d\n", count2);
//                        pc.printf("temp1 before = %0.2f\n", temp3);
                        temp3 = temp3 *10;
                        temp3 = (temp3+ans)/10;
//                        pc.printf("temp1 after = %0.2f\n", temp3);
                        count2 = count2 +1;
                        fans2 = fans2 * 10;
                        }
//                        wait(0.3);
                    else if (count2 > 2) {
                        temp3 = temp3 * fans2;
                        temp3 = temp3 + ans;
//                        pc.printf("ans = %d\n", ans);
                        temp3 = temp3/fans2;
//                        pc.printf("temp1 after = %0.2f\n", temp1);
                        fans2 = fans2 * 10;
                        }
                    else if(count2==1){
                        temp3 = temp3 * 10;
                        temp3 = temp3 + ans;
//                        pc.printf("temp1 = %d\n", temp1);
//                        pc.printf("count = %d\n", count);
                        }
                    }
//                    wait(0.3);
                }
        wait(0.2);
       }
    while (1) {
        int oper3 = operpad.read(0x00);
         oper3 +=operpad.read(0x01)<<8;
         int num3 = numpad.read(0x00);
         num3 +=numpad.read(0x01)<<8;
//         temp6 = fallInterrupt2();
//         wait(0.1);
         if (oper3){
//             wait(0.3);
            int ope = fallInterrupt2();
            wait(0.2);
            if (ope == 0) {
                uLCD.cls();
                uLCD.printf("Cleared");
                wait(0.5);
                uLCD.cls();
                count3 = 1;
                temp5 = 0;
                temp6 = 0;
                fans3 = 10;
            }
            else{
                temp6 = ope;
                break;
             }
         }
         else if (num3){
                int ans = fallInterrupt();
                wait(0.2);
                if (ans == 10) {
                    count3 = count3 +1;
//                    pc.printf("hi I am in ans==10");
//                    pc.printf("hi count from ans ==10 is %d\n", count);
                    }
                else {
                    if (count3 == 2) {
//                        pc.printf("ans = %d\n", ans);
//                        pc.printf("count = %d\n", count3);
//                        pc.printf("temp1 before = %0.2f\n", temp5);
                        temp5 = temp5 *10;
                        temp5 = (temp5+ans)/10;
//                        pc.printf("temp1 after = %0.2f\n", temp5);
                        count3 = count3 +1;
                        }
//                        wait(0.3);
                    else if (count3 > 2) {
                        temp5 = temp5 * fans3;
                        temp5 = temp5 + ans;
//                        pc.printf("ans = %d\n", ans);
                        temp5 = temp5/fans3;
//                        pc.printf("temp1 after = %0.2f\n", temp1);
                        fans3 = fans3 * 10;
                        }
                    else if(count3==1){
                        temp5 = temp5 * 10;
                        temp5 = temp5 + ans;
//                        pc.printf("temp1 = %d\n", temp1);
//                        pc.printf("count = %d\n", count);
                        }
                    }
//                    wait(0.3);
                }
        wait(0.2);
       }
       if ((temp2 == 1) && (temp4 == 1)){
                temp = temp1 + temp3 + temp5;
                pc.printf("temp is %0.2f\n", temp);
           }
       else if ((temp2 == 1) && (temp4 == 2)){
                temp = temp1 + temp3 - temp5;
           }
       else if ((temp2 == 1) && (temp4 == 3)){
                temp = temp1 + (temp3 * temp5);
           }
       else if ((temp2 == 1) && (temp4 == 4)){
                temp = temp1 + (temp3 / temp5);
           }
       else if ((temp2 == 2) && (temp4 == 1)){
                temp = temp1 - temp3 + temp5;
        }
       else if ((temp2 == 2) && (temp4 == 2)){
                temp = temp1 - temp3 - temp5;
        }
       else if ((temp2 == 2) && (temp4 == 3)){
                temp = temp1 - (temp3 * temp5);
           }
       else if ((temp2 == 2) && (temp4 == 4)){
                temp = temp1 - (temp3 / temp5);
           }
       else if ((temp2 == 3) && (temp4 == 1)){
                temp = temp1 * temp3 + temp5;
        }
       else if ((temp2 == 3) && (temp4 == 2)){
                temp = temp1 * temp3 - temp5;
        }
       else if ((temp2 == 3) && (temp4 == 3)){
                temp = temp1 * temp3 * temp5;
        }
       else if ((temp2 == 3) && (temp4 == 4)){
                temp = temp1 * temp3 / temp5;
        }
       else if ((temp2 == 4) && (temp4 == 1)){
                temp = temp1 / temp3 + temp5;
        }
       else if ((temp2 == 4) && (temp4 == 2)){
                temp = temp1 / temp3 - temp5;
        }
       else if ((temp2 == 4) && (temp4 == 3)){
                temp = temp1 / temp3 * temp5;
        }
       else if ((temp2 == 4) && (temp4 == 4)){
                temp = temp1 / temp3 / temp5;
        }
        else{
            uLCD.printf("Error");
            }
        
 uLCD.color(RED);
 uLCD.printf("%0.2f\n", temp);
 
  }
  }   

Video

  • Make sure to watch it in full-view mode to clearly see the numbers that are typed on the uLCD


Please log in to post comments.