Você está na página 1de 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 <LiquidCrystal.h>
/*
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
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

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


Keypad Library for Arduino
Author: Mark Stanley, Alexander Brevig
Contact: mstanley@technologist.com, alexanderbrevig@gmail.com
*/

#include <Keypad.h>
#include <Servo.h>
// set up the Keypad
const byte ROWS = 5;// Four rows
const byte COLS = 4;// Four columns
// Define the Keymap
char keys[ROWS][COLS]
{
{'+','7','8','9'},
{'-','4','5','6'},
{'*','1','2','3'},
{'/','0','.','_'},
{'C','?','?','='},
};

=
//
//
//
//
//

row
row
row
row
row

1
2
3
4
5

// Connect keypad ROW1, ROW2, ROW3 and ROW4 to these Arduino pins.
byte rowPins[ROWS] = {47, 45, 43, 41, 39};
// Connect keypad COL1, COL2, COL3, COL4 to these Arduino pins.
byte colPins[COLS] = {24, 26, 28, 30};
// Create the Keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

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


void setup()
{
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


void performCalc(short calcState,float &prevNum,float &nextNum,short decPlace
{
switch(calcState)
{
case 0:// default
{
prevNum = nextNum;
lcd.clear();

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 row
displayHelper(calcState, nextNum, decPlacement);//prints results in th
break;
//others are similar so no comments are added
}
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 error mes
{
lcd.print("Zero Division!");
break;
}

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
void displayHelper(short calcState,float printNumber ,short decPlacement)
{

//value is at the point that the screen acts up so it is the max value that c
if( printNumber > 99999999.0)
{
lcd.print("Outside of Range");
return;
}
/*
// No calculations so it only used the last decimal place to print the number
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 plac
else{
// the val is manipulated to find the smallest dec place needed to print
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

return;
}
val *= 10.0;//moves the dec place over one
}
*/
lcd.print(printNumber, 4); //prints the default max range
// }
}

/* -------------------------------- ARDUINO MAIN PROGRAM ENTRY POINT -----------void loop()


{
// variable setup
char key ='?';
// the current key being pressed
float prevNum = 0.0;// last entered number or result of last calculation
float nextNum = 0.0;// number which is currently being keyed in by user
float offset = 1.0;// Each number will be multiplied by it to either set
short calcState = 0;// indicates calculation to perform when equals key is p
bool decOffset =false; // Indicates if the offset needs to shift right each
float decPlace = 1.0;//the offset that is used for dec placement
short decCounter = 0;//counter so that it can only go to 4 dec places
bool newEntry =true; // Detects if newly entered number will follow old calc
/*
0 = nextNum gets stored in prevNum (default)
1 = nextNum gets added to prevNum and the result is stored in prevNum (add
2 = nextNum gets subtracted from prevNum and the result is stored in prevNu
3 = nextNum gets multiplied by prevNum and the result is stored in prevNum
4 = nextNum gets divided from prevNum and the result is stored in prevNum
*/
lcd.print("Calculator Ready");
// main loop
while(true)
{

if(decOffset){ //if the decimal point has been clicked it loops through 4 t
if(decCounter == 1)
decPlace = 0.1;
else if(decCounter == 2)
decPlace = 0.01;
else if(decCounter == 3)
decPlace = 0.001;
else if(decCounter == 4)
decPlace = 0.0001;
}

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 <= 4){
lcd.clear();
displayHelper(calcState, nextNum, decCounter);
}
if(decOffset)
decCounter++;
break;
}
case '1': // one key has been pressed
{
if(newEntry)
{
nextNum = 0;
newEntry =false;
}
if (!decOffset)//moves the number over by one to make room for next
nextNum *= 10.0;
if(decCounter <= 4){ //Will add numbers greater than 1 but less
nextNum += (1.0 * offset * decPlace);//addes the value times a 1
lcd.clear();
//All other number cases ar
displayHelper(calcState, nextNum, decCounter);
}
if(decOffset) //if the decimal point has been clicked then it incre
decCounter++;
break;
}
case '2': // two key has been pressed
{
if(newEntry)
{
nextNum = 0;
newEntry =false;
}

if(!decOffset)
nextNum *= 10.0;
if(decCounter <= 4){
nextNum += (2.0 * offset * decPlace);
lcd.clear();
displayHelper(calcState,nextNum, decCounter);
}
if(decOffset)
decCounter++;
break;
}
case '3': // three key has been pressed
{
if(newEntry)
{
nextNum = 0;
newEntry =false;
}
if(!decOffset)
nextNum *= 10.0;
if(decCounter <= 4){
nextNum += (3.0 * offset * decPlace);
lcd.clear();
displayHelper(calcState,nextNum, decCounter);
}
if(decOffset)
decCounter++;
break;
}
case '4': // four key has been pressed
{
if(newEntry)
{
nextNum = 0;
newEntry =false;
}
if(!decOffset)
nextNum *= 10.0;
if(decCounter <= 4){
nextNum += (4.0 * offset * decPlace);
lcd.clear();
displayHelper(calcState,nextNum, decCounter);
}
if(decOffset)
decCounter++;
break;

}
case '5': // five key has been pressed
{
if(newEntry)
{
nextNum = 0;
newEntry =false;
}
if(!decOffset)
nextNum *= 10.0;
if(decCounter <= 4){
nextNum += (5.0 * offset * decPlace);
lcd.clear();
displayHelper(calcState,nextNum, decCounter);
}
if(decOffset)
decCounter++;
break;
}
case '6': // six key has been pressed
{
if(newEntry)
{
nextNum = 0;
newEntry =false;
}
if(!decOffset)
nextNum *= 10.0;
if(decCounter <= 4){
nextNum += (6.0 * offset * decPlace);
lcd.clear();
displayHelper(calcState,nextNum, decCounter);
}
if(decOffset)
decCounter++;
break;
}
case '7': // seven key has been pressed
{
if(newEntry)
{
nextNum = 0;
newEntry =false;
}
if(!decOffset)
nextNum *= 10.0;

if(decCounter <= 4){


nextNum += (7.0 * offset * decPlace);
lcd.clear();
displayHelper(calcState,nextNum, decCounter);
}
if(decOffset)
decCounter++;
break;
}
case '8': // eight key has been pressed
{
if(newEntry)
{
nextNum = 0;
newEntry =false;
}
if(!decOffset)
nextNum *= 10.0;
if(decCounter <= 4){
nextNum += (8.0 * offset * decPlace);
lcd.clear();
displayHelper(calcState,nextNum, decCounter);
}
if(decOffset)
decCounter++;
break;
}
case '9': // nine key has been pressed
{
if(newEntry)
{
nextNum = 0;
newEntry =false;
}
if(!decOffset)
nextNum *= 10.0;
if(decCounter <= 4){
nextNum += (9.0 * offset * decPlace);
lcd.clear();
displayHelper(calcState,nextNum, decCounter);
}
if(decOffset)
decCounter++;
break;
}
case 'C': // clear key has been pressed

if(nextNum == 0.0)
{
// current operand has already been cleared, so clear previous op
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

{
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 is set
// turned off in the middle of a number
}
break;

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


{
offset *= -1.0;//changes the offset so the neg key can be pressed mul
lcd.clear();
if(nextNum == 0.0) //if there is no number to display then just show t
lcd.print("-");
else{
if(nextNum < 0)
nextNum *= -1;
else
nextNum *= offset;//sets the current number to the neg offset
displayHelper(calcState, nextNum, decCounter);
}
break;
}
}
}
}

Você também pode gostar