Arduino Calculator Source

download Arduino Calculator Source

of 12

Transcript of Arduino Calculator Source

  • 8/10/2019 Arduino Calculator Source

    1/12

    /*

    Arduino Calculator

    UWB Spring 2012 CSS427 Final Project

    ----

    Jason Woodring

    Alexander Johnson

    Oonagh Geldenhuys

    Joshua Smith

    */

    /* -------------------------------- LCD SETUP ----------------------------

    /*

    Library originally added 18 Apr 2008

    by David A. Mellis

    library modified 5 Jul 2009

    by Limor Fried (http://www.ladyada.net)

    example added 9 Jul 2009

    by Tom Igoe

    modified 22 Nov 2010

    by Tom Igoe

    */

    #include

    /*

    The circuit:

    * LCD RS pin to digital pin 12

    * LCD Enable pin to digital pin 11

    * LCD D4 pin to digital pin 5

    * LCD D5 pin to digital pin 4

    * LCD D6 pin to digital pin 3

    * LCD D7 pin to digital pin 2

    * LCD R/W pin to ground

    * 10K resistor:

    * ends to +5V and ground

    * wiper to LCD VO pin (pin 3)

    */

    // initialize the library with the numbers of the interface pins

    LiquidCrystallcd(12, 11, 5, 4, 3, 2);

    /* -------------------------------- KEYPAD SETUP -------------------------

    /*

    Keypad Library for Arduino

    Author: Mark Stanley, Alexander Brevig

    Contact: [email protected], [email protected]

    */

  • 8/10/2019 Arduino Calculator Source

    2/12

    #include

    #include

    // set up the Keypad

    constbyteROWS = 5;// Four rows

    constbyteCOLS = 4;// Four columns

    // Define the Keymap

    charkeys[ROWS][COLS] =

    {

    {'+','7','8','9'}, // row 1

    {'-','4','5','6'}, // row 2

    {'*','1','2','3'}, // row 3

    {'/','0','.','_'}, // row 4

    {'C','?','?','='}, // row 5

    };

    // Connect keypad ROW1, ROW2, ROW3 and ROW4 to these Arduino pins.

    byterowPins[ROWS] = {47, 45, 43, 41, 39};

    // Connect keypad COL1, COL2, COL3, COL4 to these Arduino pins.

    bytecolPins[COLS] = {24, 26, 28, 30};

    // Create the Keypad

    Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

    /* -------------------------------- FUNCTIONS ----------------------------

    voidsetup()

    {

    Serial.begin(9600);

    lcd.clear();

    // set up the LCD's number of columns and rows:

    lcd.begin(16, 2);

    }

    // performs operation on prevNum and nextNum as indicated by calcState

    voidperformCalc(short calcState,float &prevNum,float &nextNum,shortdec

    {

    switch(calcState)

    {

    case 0:// default

    {

    prevNum = nextNum;

    lcd.clear();

  • 8/10/2019 Arduino Calculator Source

    3/12

    displayHelper(calcState, nextNum, decPlacement);

    //nextNum = 0.0;

    break;

    }

    case 1:// addition

    {

    prevNum += nextNum;

    nextNum = prevNum;

    lcd.clear();

    lcd.print("Result"); //prints result on the top row

    lcd.setCursor(0,1); //sets the cursor to the start of the lower r

    displayHelper(calcState, nextNum, decPlacement);//prints results

    break; //others are similar so no comments are ad

    }

    case 2:// subtraction

    {

    prevNum -= nextNum;

    nextNum = prevNum;

    lcd.clear();

    lcd.print("Result");

    lcd.setCursor(0,1);

    displayHelper(calcState, nextNum, decPlacement);

    break;

    }

    case 3:// multiplication

    {

    prevNum *= nextNum;

    nextNum = prevNum;

    lcd.clear();

    lcd.print("Result");

    lcd.setCursor(0,1);

    displayHelper(calcState, nextNum, decPlacement);

    break;

    }

    case 4:// division

    {

    lcd.clear();

    lcd.print("Result");

    lcd.setCursor(0,1);

    if(nextNum == 0.0) //checks for division by zero and displays erro

    {

    lcd.print("Zero Division!");

    break;

    }

  • 8/10/2019 Arduino Calculator Source

    4/12

    else

    {

    prevNum /= nextNum;

    nextNum = prevNum;

    displayHelper(calcState,nextNum, decPlacement);

    break;

    }

    }

    }

    newEntry =true;

    }

    int i;//so it isnt created each time it is called

    float val;//same as above

    voiddisplayHelper(short calcState,floatprintNumber ,shortdecPlacement

    {

    //value is at the point that the screen acts up so it is the max value t

    if( printNumber > 99999999.0)

    {

    lcd.print("Outside of Range");

    return;

    }

    /*

    // No calculations so it only used the last decimal place to print the nu

    if(calcState == 0)

    {

    if(decPlacement > 4)

    lcd.print(printNumber, 4);

    else

    lcd.print(printNumber, decPlacement);

    return;

    }*/

    /*

    //A calculation was made so it needs to find the smallest decimal number

    else{

    // the val is manipulated to find the smallest dec place needed to prin

    val = printNumber;

    for (i = 0; i < 5; i++)

    {

    //returns the % of the val (have to use this since it is a float)

    val = fmod(val, 1.0);

    if(val == 0)//the last dec place has been reached

    {

    lcd.print(printNumber, i);//print to that point

  • 8/10/2019 Arduino Calculator Source

    5/12

    return;

    }

    val *= 10.0;//moves the dec place over one

    }

    */

    lcd.print(printNumber, 4); //prints the default max range

    // }

    }

    /* -------------------------------- ARDUINO MAIN PROGRAM ENTRY POINT ------

    voidloop()

    {

    // variable setup

    char key ='?'; // the current key being pressed

    float prevNum = 0.0;// last entered number or result of last calculat

    float nextNum = 0.0;// number which is currently being keyed in by us

    float offset = 1.0;// Each number will be multiplied by it to eithe

    shortcalcState = 0;// indicates calculation to perform when equals key

    bool decOffset =false; // Indicates if the offset needs to shift right

    floatdecPlace = 1.0;//the offset that is used for dec placement

    shortdecCounter = 0;//counter so that it can only go to 4 dec places

    bool newEntry =true; // Detects if newly entered number will follow old

    /*

    0 = nextNum gets stored in prevNum (default)

    1 = nextNum gets added to prevNum and the result is stored in prevNum

    2 = nextNum gets subtracted from prevNum and the result is stored in

    3 = nextNum gets multiplied by prevNum and the result is stored in pr

    4 = nextNum gets divided from prevNum and the result is stored in pre

    */

    lcd.print("Calculator Ready");

    // main loop

    while(true)

    {

    if(decOffset){ //if the decimal point has been clicked it loops throug

    if(decCounter == 1)

    decPlace = 0.1;

    elseif(decCounter == 2)

    decPlace = 0.01;

    elseif(decCounter == 3)

    decPlace = 0.001;

    elseif(decCounter == 4)

    decPlace = 0.0001;

    }

  • 8/10/2019 Arduino Calculator Source

    6/12

    key = keypad.getKey();

    switch(key)

    {

    case'0': // zero key has been pressed

    {

    if(newEntry)

    {

    nextNum = 0;

    newEntry =false;

    }

    if(!decOffset)

    nextNum *= 10.0;

    if(decCounter

  • 8/10/2019 Arduino Calculator Source

    7/12

    if(!decOffset)

    nextNum *= 10.0;

    if(decCounter

  • 8/10/2019 Arduino Calculator Source

    8/12

    }

    case'5': // five key has been pressed

    {

    if(newEntry)

    {

    nextNum = 0;

    newEntry =false;

    }

    if(!decOffset)

    nextNum *= 10.0;

    if(decCounter

  • 8/10/2019 Arduino Calculator Source

    9/12

    if(decCounter

  • 8/10/2019 Arduino Calculator Source

    10/12

    {

    if(nextNum == 0.0)

    {

    // current operand has already been cleared, so clear previo

    prevNum = 0.0;

    calcState = 0;

    offset = 1.0;

    }

    else

    {

    // clear the current operand

    nextNum = 0.0;

    offset = 1.0;

    }

    decOffset =false;

    decPlace = 1.0;

    decCounter = 0;

    // update display to reflect cleared state

    lcd.clear();

    lcd.print("Calculator Ready");

    break;

    }

    case'+': // addition key has been pressed

    {

    performCalc(calcState, prevNum, nextNum, decCounter, newEntry)

    calcState = 1;

    decOffset =false;

    decPlace = 1.0;

    nextNum = 0.0;

    offset = 1.0;

    decCounter = 0;

    break;

    }

    case'-': // subtraction key has been pressed

    {

    performCalc(calcState, prevNum, nextNum, decCounter, newEntry)

    calcState = 2;

    decOffset =false;

    decPlace = 1.0;

    nextNum = 0.0;

    offset = 1.0;

    decCounter = 0;

    break;

    }

    case'*': // multiplication key has been pressed

  • 8/10/2019 Arduino Calculator Source

    11/12

    {

    performCalc(calcState, prevNum, nextNum, decCounter, newEntry)

    calcState = 3;

    decOffset =false;

    decPlace = 1.0;

    nextNum = 0.0;

    offset = 1.0;

    decCounter = 0;

    break;

    }

    case'/': // division key has been pressed

    {

    performCalc(calcState, prevNum, nextNum, decCounter, newEntry)

    calcState = 4;

    decOffset =false;

    decPlace = 1.0;

    nextNum = 0.0;

    offset = 1.0;

    decCounter = 0;

    break;

    }

    case'=': // equals key has been pressed

    {

    performCalc(calcState, prevNum, nextNum, decCounter, newEntry)

    calcState = 0;

    decOffset =false;

    decPlace = 1.0;

    offset = 1.0;

    decCounter = 0;

    break;

    }

    case'.': // decimal key has been pressed

    {

    if(newEntry)

    {

    nextNum = 0;

    newEntry =false;

    }

    if(!decOffset)

    {

    decCounter = 1;

    decPlace = 1.0;

    decOffset =true; //sets the decimal offset flag to true, it

    // turned off in the middle of a number

    }

    break;

  • 8/10/2019 Arduino Calculator Source

    12/12

    }

    case'_': // negative key has been pressed

    {

    offset *= -1.0;//changes the offset so the neg key can be presse

    lcd.clear();

    if(nextNum == 0.0) //if there is no number to display then just

    lcd.print("-");

    else{

    if(nextNum < 0)

    nextNum *= -1;

    else

    nextNum *= offset;//sets the current number to the neg offset

    displayHelper(calcState, nextNum, decCounter);

    }

    break;

    }

    }

    }

    }