//This is an example sketch to operate a four function //calculator with the PmodKYPD, PmodCDC1, and PmodOLED #include #define OLED OledNewClass //pins 1-4 and 7-10 are as follows: GND, 11, NC, 13; 39, 10, 40, 38 OLED OLED; //button related globals int keypad[8] = {26, 27, 28, 29, 30, 31, 32, 33}; int pwrBtn = 34; //BTN 0 int clrBtn = 35; //BTN 1 int backspaceBtn = 36; //BTN 2 int negativeBtn = 37; //BTN 3 int buttonPressed[2]; boolean newBtnPress = false; boolean onOff = true; //character related globals boolean openingScreen = true; boolean firstNumValue = true; boolean finalAnsScreen = false; boolean negativeSign1 = false; boolean negativeSign2 = false; boolean overflowError = false; boolean decimalPoint1Exist = false; boolean decimalPoint2Exist = false; char charValue; char oldOperatorValue; uint8_t numSize1 = 0; uint8_t numSize2 = 0; uint8_t subNum1 = 0; uint8_t subNum2 = 0; double input1Value = 0.0; double input2Value = 0.0; double answerValue = 0.0; void setup(){ for(int i=0; i<4; i++){ pinMode(keypad[i], OUTPUT); digitalWrite(keypad[i], HIGH); } for(int j=4; j<8; j++){ pinMode(keypad[j], INPUT); } pinMode(pwrBtn, INPUT); pinMode(clrBtn, INPUT); pinMode(backspaceBtn, INPUT); pinMode(negativeBtn, INPUT); displayOpeningScreen(); }//END of setup void loop(){ checkForKYPDBtnPress(); if(digitalRead(pwrBtn)==HIGH){ powerOnOff(); } if(digitalRead(clrBtn)==HIGH){ clearTheScreen(); } if(digitalRead(backspaceBtn)==HIGH){ backspace(); } if(digitalRead(negativeBtn)==HIGH){ toggleNegative(); } }//END of loop /////////////////////////////////////////////////////////////////////////////////////// /********************** **********************/ /********************** User Defined Functions **********************/ /********************** **********************/ /////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////// // // // powerOnOff // // // ///////////////////////////////////////////// void powerOnOff(){ if(onOff == true){ int row; int col; OLED.getCursor(&row, &col); OLED.setCursor(0,2); OLED.putString(" Power off? "); boolean timing = true; int powerTime = millis(); if(digitalRead(pwrBtn)==HIGH){ while(digitalRead(pwrBtn)==HIGH && (millis() - powerTime) < 3000){ //wait } if(digitalRead(pwrBtn)==HIGH && (millis() - powerTime)>= 3000){ OLED.clear(); OLED.end(); onOff = false; timing = false; while(digitalRead(pwrBtn)==HIGH){ delay(1); } } }//END of if the power button was being pressed the whole time int startTime = millis(); while(timing == true && (millis() - startTime)<3000){ if(digitalRead(clrBtn)==HIGH || digitalRead(backspaceBtn)==HIGH || digitalRead(negativeBtn)==HIGH){ while(digitalRead(clrBtn)==HIGH || digitalRead(backspaceBtn)==HIGH || digitalRead(negativeBtn)==HIGH){ //wait for other button to stop being pressed } timing = false; } else if(digitalRead(pwrBtn)==HIGH){ OLED.clear(); OLED.end(); onOff = false; timing = false; while(digitalRead(pwrBtn)==HIGH){ //wait }//end of waiting }//END of if the power button was pressed again }//end of while loop delay(5); if(timing == true && (millis() - startTime) >3000){ OLED.clear(); OLED.end(); onOff = false; } else if(onOff == true){ OLED.setCursor(0,2); OLED.putString(" "); OLED.setCursor(row, col); } }//END of if the OLED was already on else{ displayOpeningScreen(); onOff = true; }//END of if the OLED was off }//END of powerOnOff function ///////////////////////////////////////////// // // // displayOpeningScreen // // // ///////////////////////////////////////////// void displayOpeningScreen(){ openingScreen = true; OLED.begin(); delay(100); OLED.setCharUpdate(1); OLED.clear(); numSize1 = 0; numSize2 = 0; subNum1 = 0; subNum2 = 0; input1Value = 0.0; input2Value = 0.0; answerValue = 0.0; newBtnPress = false; onOff = true; firstNumValue = true; finalAnsScreen = false; negativeSign1 = false; negativeSign2 = false; overflowError = false; decimalPoint1Exist = false; decimalPoint2Exist = false; OLED.setCursor(0,0); OLED.putString("Start typing on");//15 OLED.setCursor(0,1); OLED.putString(" the keypad to ");//15 OLED.setCursor(0,2); OLED.putString(" start ");//15 OLED.setCursor(0,3); OLED.putString(" calculating! ");//16 OLED.setCursor(1,0); //anticipating typing at the beginning of the screen } ///////////////////////////////////////////// // // // clearTheScreen // // // ///////////////////////////////////////////// void clearTheScreen(){ while(digitalRead(clrBtn) == HIGH){ //wait } OLED.clear(); numSize1 = 0; numSize2 = 0; subNum1 = 0; subNum2 = 0; input1Value = 0.0; input2Value = 0.0; answerValue = 0.0; newBtnPress = false; onOff = true; openingScreen = false; firstNumValue = true; finalAnsScreen = false; negativeSign1 = false; negativeSign2 = false; overflowError = false; decimalPoint1Exist = false; decimalPoint2Exist = false; OLED.setCursor(1,0); }//END of clearing the Screen ///////////////////////////////////////////// // // // toggleNegative // // // ///////////////////////////////////////////// void toggleNegative(){ while(digitalRead(negativeBtn) == HIGH){ //wait } delay(1); int row; int col; OLED.getCursor(&row, &col); if(firstNumValue == true){ //these functions will flip the current sign for each value, if they are not zero to begin with if(input1Value != 0 && negativeSign1 == true){ OLED.setCursor(0,0); OLED.putChar(' '); input1Value = input1Value * -1; OLED.setCursor(row, col); negativeSign1 = false; } else if(input1Value != 0 && negativeSign1 == false){ OLED.setCursor(0,0); OLED.putChar('-'); input1Value = input1Value * -1; OLED.setCursor(row, col); negativeSign1 = true; } }//END of input1Value != 0 else if(firstNumValue == false){ if(input2Value != 0 && negativeSign2 == true){ OLED.setCursor(0,1); OLED.putChar(' '); input2Value = input2Value * -1; OLED.setCursor(row, col); negativeSign2 = false; } else if(input2Value != 0 && negativeSign2 == false){ OLED.setCursor(0,1); OLED.putChar('-'); input2Value = input2Value * -1; OLED.setCursor(row, col); negativeSign2 = true; } }//END of input2Value != 0; }//END of toggling the negative sign ///////////////////////////////////////////// // // // backspace // // // ///////////////////////////////////////////// void backspace(){ while(digitalRead(backspaceBtn) == HIGH){ //wait } int row; int col; OLED.getCursor(&row, &col); if(firstNumValue == true){ if(numSize1 > 0){ numSize1--; OLED.setCursor(row-1, col); OLED.putChar(' '); OLED.setCursor(row-1, col); if(subNum1 == 0){ input1Value = (int)input1Value/10; //this accounts for all non-decimal places } else if(subNum1 == 1){ subNum1--; // this accounts for the actual decimal place } else if(subNum1 == 2){ subNum1--; input1Value = input1Value*10; input1Value = (int)input1Value/10; }//end of getting rid of the single decimal place else if(subNum1 > 2){ subNum1--; int divisor = 1; for(int scalar = subNum1; scalar > 1; scalar--){ divisor = divisor * 10; }//end of scaling the divisor input1Value = input1Value*divisor; //multipling it so that there are no decimal places input1Value = int(input1Value)/10; //dividing and getting rid of the last decimal place input1Value = input1Value/double(divisor/10.0); //keeping the rest of the decimal places and getting the number back to normal }//END of if there are decimal places }//end of decreasing actual number size }//END of firstNumValue == true if(firstNumValue == false){ if(numSize2 > 0){ numSize2--; OLED.setCursor(row-1, col); OLED.putChar(' '); OLED.setCursor(row-1, col); if(subNum2 == 0){ input2Value = (int)input2Value/10; //this accounts for all non-decimal places } else if(subNum2 == 1){ subNum2--; // this accounts for the actual decimal place } else if(subNum2 == 2){ subNum2--; input2Value = input2Value*10; input2Value = (int)input2Value/10; }//end of getting rid of the single decimal place else if(subNum2 > 2){ subNum2--; int divisor = 1; for(int scalar = subNum2; scalar > 1; scalar--){ divisor = divisor * 10; }//end of scaling the divisor input2Value = input2Value*divisor; //multipling it so that there are no decimal places input2Value = int(input2Value)/10; //dividing and getting rid of the last decimal place input2Value = input2Value/double(divisor/10.0); //keeping the rest of the decimal places and getting the number back to normal }//END of if there are decimal places }//end of decreasing actual number size }//END of firstNumValue == false }//END of backspace ///////////////////////////////////////////// // // // checkForKYPDBtnPress // // // ///////////////////////////////////////////// void checkForKYPDBtnPress(){ for(int i=3; i>=0; i--){ digitalWrite(keypad[i], LOW); for(int j=7; j>3; j--){ delay(1); if(digitalRead(keypad[j])==LOW){ delay(300); //one more delay buttonPressed[0]=i; buttonPressed[1]=j; if(openingScreen == true){ clearTheScreen(); openingScreen = false; }//end of clearing openingScreen, if it is required if(finalAnsScreen == true){ clearTheScreen(); finalAnsScreen = false; } newBtnPress = true; }//end of if a button happens to be pressed }//end of reading each row digitalWrite(keypad[i], HIGH); }//end of enabling each column low if(newBtnPress == true){ switch(buttonPressed[0]){ case 3: switch(buttonPressed[1]){ case 7: charValue = '1'; break; case 6: charValue = '4'; break; case 5: charValue = '7'; break; case 4: charValue = '0'; break; default: //do nothing break; } break;//end of first column case 2: switch(buttonPressed[1]){ case 7: charValue = '2'; break; case 6: charValue = '5'; break; case 5: charValue = '8'; break; case 4: charValue = '.'; break; default: //do nothing break; } break;//end of second column case 1: switch(buttonPressed[1]){ case 7: charValue = '3'; break; case 6: charValue = '6'; break; case 5: charValue = '9'; break; case 4: charValue = '='; break; default: //do nothing break; } break; //end of third column case 0: switch(buttonPressed[1]){ case 7: charValue = '+'; break; case 6: charValue = '-'; break; case 5: charValue = 'x'; break; case 4: charValue = '/'; break; default: //do nothing break; } break;//end of fourth column default: //do nothing break; }//end of figuring out the appropriate char displaying(); newBtnPress = false; }//END of if the newBtnPress == true statement }//END of checkForKYPDBtnPress ///////////////////////////////////////////// // // // calculatePartialAnswer // // // ///////////////////////////////////////////// void calculatePartialAnswer(){ if(oldOperatorValue == '+'){ input1Value = input1Value + input2Value; if(input1Value >= 1000000000 || input1Value <= -1000000000){ OLED.setCursor(0,3); OLED.putString(" Overflow Error "); overflowError = true; numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; finalAnsScreen = true; } else{ int place; int firstPlace = 0; numSize1=0; OLED.clear(); if(input1Value < 0.00){ negativeSign1 = true; OLED.setCursor(0,0); OLED.putChar('-'); input1Value = input1Value * -1; //this makes the answer postive for calculation/display purposes //it will be switched back to a negative version at the end of the if '+' statement (if appropriate) } else if(input1Value >= 0.00){ negativeSign1 = false; OLED.setCursor(0,0); OLED.putChar(' '); } OLED.setCursor(1,0); if(input1Value >=1){ // if the answer is greater than 1 for(int firstNumTest = 0; firstNumTest < 9; firstNumTest++){ // looping through this test 9 times int divisor=1; for(int scaler = firstNumTest; scaler>0; scaler--){ divisor = divisor*10; // Scaling divisor from 1 up to 1 billion } // END of getting the correct divisor scaler place = input1Value/divisor; // dividing the answer by the scaled divisor if(place < 10 && place>=1){ // if the place value is now single digit firstPlace = firstNumTest; // marking which place value is the largest of that number firstNumTest = 11; // forcibly ending the test loop } } place = 0; int tempPlace = 0; // Starting the value to be subtracted as a zero for(firstPlace; firstPlace>=0; firstPlace--){ // Looping through as many times as there were place digits int divisor=1; for(int scaler = firstPlace; scaler>0; scaler--){ divisor = divisor*10; // Scaling the divisor from 1 to however large it needs to be } // END of getting the correct divisor scaler place = input1Value/divisor; // dividing the answer by the scaled divisor OLED.putChar(place+0x30-tempPlace); // Taking the number value and adding the appropriate value // (and subtracting off 10's place and larger values) to // get the appropriate character value in the ASCII table tempPlace = place*10; // getting the value to be subtracted on the next loop cycle numSize1++; // a count to make sure everything fits on the screen } //code to check if there are any relevant decimal places-- also need to check if there's room to put the decimal places double decimalPlaceTest = input1Value - int(input1Value); // Working with just the decimal places boolean decimalPoint = false; // A boolean to mark whether or not the actual decimal place has been printed int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ // while the decimal place is a certain size and a check to make sure all the // numbers and decimal place will fit on the screen if(decimalPoint == false){ // if there is no decimal place yet OLED.putChar('.'); // put a decimal place numSize1++; // take up a spot on the screen decimalPoint = true; // state there is decimal place restriction = 0; // allow more room for numbers after ensuring there was room for at least the // decimal place and one number } OLED.putChar(int(decimalPlaceTest*10)+0x30); // multiply the decimal place number value by 10 and add 0x30 to make it the // appropriate character numSize1++; // increase the count of how many spots on the screen have been taken up decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); // subtract off the freshly placed single digit so that the next decimal // place can be printed without any issue } //END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is greater than 1.00 //Check to see if our answer just 0 (basically zero anyway) else if(input1Value > -0.000001 && input1Value < 0.000001){ numSize1++; OLED.putChar(0x30); } else if(input1Value < 1){ double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); //OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is only a decimal to begin with if(negativeSign1 == true){ input1Value = input1Value*-1; } OLED.setCursor(15, 0); OLED.putChar(charValue); numSize2 = 0; subNum2 = 0; input2Value = 0.0; decimalPoint2Exist = false; }//END of non-overflow error }//END of + else if(oldOperatorValue == '-'){ input1Value = input1Value - input2Value; if(input1Value >= 1000000000 || input1Value <= -1000000000){ OLED.setCursor(0,3); OLED.putString(" Overflow Error "); overflowError = true; numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; finalAnsScreen = true; } else{ int place; int firstPlace = 0; numSize1=0; OLED.clear(); if(input1Value < 0.00){ negativeSign1 = true; OLED.setCursor(0,0); OLED.putChar('-'); input1Value = input1Value * -1; //this makes the answer postive for calculation/display purposes //it will be switched back to a negative version at the end of the if '+' statement (if appropriate) } else if(input1Value >= 0.00){ negativeSign1 = false; OLED.setCursor(0,0); OLED.putChar(' '); } OLED.setCursor(1,0); if(input1Value >=1){ for(int firstNumTest = 0; firstNumTest < 9; firstNumTest++){ int divisor=1; for(int scaler = firstNumTest; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; if(place < 10 && place>=1){ // if the place value is now single digit firstPlace = firstNumTest; firstNumTest = 11; } // END of if place value is now single digit (but not decimal) } place = 0; int tempPlace = 0; for(firstPlace; firstPlace>=0; firstPlace--){ int divisor=1; for(int scaler = firstPlace; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; OLED.putChar(place+0x30-tempPlace); tempPlace = place*10; numSize1++; } //code to check if there are any relevant decimal places-- also need to check if there's room to put the decimal places double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is greater than 1.00 //Check to see if our answer just 0 (basically zero anyway) else if(input1Value > -0.000001 && input1Value < 0.000001){ numSize1++; OLED.putChar(0x30); } else if(input1Value < 1){ double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); //OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is only a decimal to begin with if(negativeSign1 == true){ input1Value = input1Value*-1; } OLED.setCursor(15, 0); OLED.putChar(charValue); numSize2 = 0; subNum2 = 0; input2Value = 0.0; decimalPoint2Exist = false; }//END of non-overflow error }//END of - else if(oldOperatorValue == 'x'){ input1Value = input1Value*input2Value; if(input1Value >= 1000000000 || input1Value <= -1000000000){ OLED.setCursor(0,3); OLED.putString(" Overflow Error "); overflowError = true; numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; finalAnsScreen = true; } else{ int place; int firstPlace = 0; numSize1=0; OLED.clear(); if(input1Value < 0.00){ negativeSign1 = true; OLED.setCursor(0,0); OLED.putChar('-'); input1Value = input1Value * -1; //this makes the answer postive for calculation/display purposes //it will be switched back to a negative version at the end of the if '+' statement (if appropriate) } else if(input1Value >= 0.00){ negativeSign1 = false; OLED.setCursor(0,0); OLED.putChar(' '); } OLED.setCursor(1,0); if(input1Value >=1){ for(int firstNumTest = 0; firstNumTest < 9; firstNumTest++){ int divisor=1; for(int scaler = firstNumTest; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; if(place < 10 && place>=1){ // if the place value is now single digit firstPlace = firstNumTest; firstNumTest = 11; } // END of if place value is now single digit (but not decimal) } place = 0; int tempPlace = 0; for(firstPlace; firstPlace>=0; firstPlace--){ int divisor=1; for(int scaler = firstPlace; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; OLED.putChar(place+0x30-tempPlace); tempPlace = place*10; numSize1++; } //code to check if there are any relevant decimal places-- also need to check if there's room to put the decimal places double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is greater than 1.00 //Check to see if our answer just 0 (basically zero anyway) else if(input1Value > -0.000001 && input1Value < 0.000001){ numSize1++; OLED.putChar(0x30); } else if(input1Value < 1){ double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); //OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is only a decimal to begin with if(negativeSign1 == true){ input1Value = input1Value*-1; } OLED.setCursor(15, 0); OLED.putChar(charValue); numSize2 = 0; subNum2 = 0; input2Value = 0.0; decimalPoint2Exist = false; }//END of non-overflow error }//END of x else if(oldOperatorValue == '/'){ if(input2Value == 0){ OLED.clear(); numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; firstNumValue = true; OLED.setCursor(0,2); OLED.putString("Divide by 0 Err."); openingScreen = true; } else{ input1Value = input1Value / input2Value; if(input1Value >= 1000000000 || input1Value <= -1000000000){ OLED.setCursor(0,3); OLED.putString(" Overflow Error "); overflowError = true; numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; finalAnsScreen = true; } else{ int place; int firstPlace = 0; numSize1=0; OLED.clear(); if(input1Value < 0.00){ negativeSign1 = true; OLED.setCursor(0,0); OLED.putChar('-'); input1Value = input1Value * -1; //this makes the answer postive for calculation/display purposes //it will be switched back to a negative version at the end of the if '+' statement (if appropriate) } else if(input1Value >= 0.00){ negativeSign1 = false; OLED.setCursor(0,0); OLED.putChar(' '); } OLED.setCursor(1,0); if(input1Value >=1){ for(int firstNumTest = 0; firstNumTest < 9; firstNumTest++){ int divisor=1; for(int scaler = firstNumTest; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; if(place < 10 && place>=1){ // if the place value is now single digit firstPlace = firstNumTest; firstNumTest = 11; } // END of if place value is now single digit (but not decimal) } place = 0; int tempPlace = 0; for(firstPlace; firstPlace>=0; firstPlace--){ int divisor=1; for(int scaler = firstPlace; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; OLED.putChar(place+0x30-tempPlace); tempPlace = place*10; numSize1++; } //code to check if there are any relevant decimal places-- also need to check if there's room to put the decimal places double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is greater than 1.00 //Check to see if our answer just 0 (basically zero anyway) else if(input1Value > -0.000001 && input1Value < 0.000001){ numSize1++; OLED.putChar(0x30); } else if(input1Value < 1){ double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is only a decimal to begin with if(negativeSign1 == true){ input1Value = input1Value*-1; } OLED.setCursor(15, 0); OLED.putChar(charValue); numSize2 = 0; subNum2 = 0; input2Value = 0.0; decimalPoint2Exist = false; }//END of non-overflow error }//end of normal division }//END of / }//END of calculatePartialAnswer ///////////////////////////////////////////// // // // calculateFinalAnswer // // // ///////////////////////////////////////////// void calculateFinalAnswer(){ if(oldOperatorValue == '+'){ input1Value = input1Value + input2Value; if(input1Value >= 1000000000 || input1Value <= -1000000000){ OLED.setCursor(0,3); OLED.putString(" Overflow Error "); overflowError = true; numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; finalAnsScreen = true; } else{ int place; int firstPlace = 0; numSize1=0; OLED.clear(); if(input1Value < 0.00){ negativeSign1 = true; OLED.setCursor(0,3); OLED.putChar('-'); input1Value = input1Value * -1; //this makes the answer postive for calculation/display purposes //it will be switched back to a negative version at the end of the if '+' statement (if appropriate) } else if(input1Value >= 0.00){ negativeSign1 = false; OLED.setCursor(0,3); OLED.putChar(' '); } OLED.setCursor(15, 2); OLED.putChar('='); OLED.setCursor(1,3); if(input1Value >=1){ for(int firstNumTest = 0; firstNumTest < 9; firstNumTest++){ int divisor=1; for(int scaler = firstNumTest; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; if(place < 10 && place>=1){ // if the place value is now single digit firstPlace = firstNumTest; firstNumTest = 11; } // END of if place value is now single digit (but not decimal) } place = 0; int tempPlace = 0; for(firstPlace; firstPlace>=0; firstPlace--){ int divisor=1; for(int scaler = firstPlace; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; OLED.putChar(place+0x30-tempPlace); tempPlace = place*10; numSize1++; } //code to check if there are any relevant decimal places-- also need to check if there's room to put the decimal places double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is greater than 1.00 //Check to see if our answer just 0 (basically zero anyway) else if(input1Value > -0.000001 && input1Value < 0.000001){ numSize1++; OLED.putChar(0x30); } else if(input1Value < 1){ double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is only a decimal to begin with if(negativeSign1 == true){ input1Value = input1Value*-1; } numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; finalAnsScreen = true; }//END of non-overflow error }//END of + else if(oldOperatorValue == '-'){ input1Value = input1Value - input2Value; if(input1Value >= 1000000000 || input1Value <= -1000000000){ OLED.setCursor(0,3); OLED.putString(" Overflow Error "); overflowError = true; numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; finalAnsScreen = true; } else{ int place; int firstPlace = 0; numSize1=0; OLED.clear(); if(input1Value < 0.00){ negativeSign1 = true; OLED.setCursor(0,3); OLED.putChar('-'); input1Value = input1Value * -1; //this makes the answer postive for calculation/display purposes //it will be switched back to a negative version at the end of the if '+' statement (if appropriate) } else if(input1Value >= 0.00){ negativeSign1 = false; OLED.setCursor(0,3); OLED.putChar(' '); } OLED.setCursor(15, 2); OLED.putChar('='); OLED.setCursor(1,3); if(input1Value >=1){ for(int firstNumTest = 0; firstNumTest < 9; firstNumTest++){ int divisor=1; for(int scaler = firstNumTest; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; if(place < 10 && place>=1){ // if the place value is now single digit firstPlace = firstNumTest; firstNumTest = 11; } // END of if place value is now single digit (but not decimal) } place = 0; int tempPlace = 0; for(firstPlace; firstPlace>=0; firstPlace--){ int divisor=1; for(int scaler = firstPlace; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; OLED.putChar(place+0x30-tempPlace); tempPlace = place*10; numSize1++; } //code to check if there are any relevant decimal places-- also need to check if there's room to put the decimal places double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is greater than 1.00 //Check to see if our answer just 0 (basically zero anyway) else if(input1Value > -0.000001 && input1Value < 0.000001){ numSize1++; OLED.putChar(0x30); } else if(input1Value < 1){ double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is only a decimal to begin with if(negativeSign1 == true){ input1Value = input1Value*-1; } numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; finalAnsScreen = true; }//END of non-overflow error }//END of - else if(oldOperatorValue == 'x'){ input1Value = input1Value*input2Value; if(input1Value >= 1000000000 || input1Value <= -1000000000){ OLED.setCursor(0,3); OLED.putString(" Overflow Error "); overflowError = true; numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; finalAnsScreen = true; } else{ int place; int firstPlace = 0; numSize1=0; OLED.clear(); if(input1Value < 0.00){ negativeSign1 = true; OLED.setCursor(0,3); OLED.putChar('-'); input1Value = input1Value * -1; //this makes the answer postive for calculation/display purposes //it will be switched back to a negative version at the end of the if '+' statement (if appropriate) } else if(input1Value >= 0.00){ negativeSign1 = false; OLED.setCursor(0,3); OLED.putChar(' '); } OLED.setCursor(15, 2); OLED.putChar('='); OLED.setCursor(1,3); if(input1Value >=1){ for(int firstNumTest = 0; firstNumTest < 9; firstNumTest++){ int divisor=1; for(int scaler = firstNumTest; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; if(place < 10 && place>=1){ // if the place value is now single digit firstPlace = firstNumTest; firstNumTest = 11; } // END of if place value is now single digit (but not decimal) } place = 0; int tempPlace = 0; for(firstPlace; firstPlace>=0; firstPlace--){ int divisor=1; for(int scaler = firstPlace; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; OLED.putChar(place+0x30-tempPlace); tempPlace = place*10; numSize1++; } //code to check if there are any relevant decimal places-- also need to check if there's room to put the decimal places double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is greater than 1.00 //Check to see if our answer just 0 (basically zero anyway) else if(input1Value > -0.000001 && input1Value < 0.000001){ numSize1++; OLED.putChar(0x30); } else if(input1Value < 1){ double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is only a decimal to begin with if(negativeSign1 == true){ input1Value = input1Value*-1; } numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; finalAnsScreen = true; }//END of non-overflow error }//END of x else if(oldOperatorValue == '/'){ if(input2Value == 0){ OLED.clear(); numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; OLED.setCursor(0,2); OLED.putString("Divide by 0 Err."); finalAnsScreen = true; }//end of preventing divide by zero error else{ input1Value = input1Value / input2Value; if(input1Value >= 1000000000 || input1Value <= -1000000000){ OLED.setCursor(0,3); OLED.putString(" Overflow Error "); overflowError = true; numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; finalAnsScreen = true; } else{ int place; int firstPlace = 0; numSize1=0; subNum1 = 0; OLED.clear(); if(input1Value < 0.00){ negativeSign1 = true; OLED.setCursor(0,3); OLED.putChar('-'); input1Value = input1Value * -1; //this makes the answer postive for calculation/display purposes //it will be switched back to a negative version at the end of the if '+' statement (if appropriate) } else if(input1Value >= 0.00){ negativeSign1 = false; OLED.setCursor(0,3); OLED.putChar(' '); } OLED.setCursor(15, 2); OLED.putChar('='); OLED.setCursor(1,3); if(input1Value >=1){ for(int firstNumTest = 0; firstNumTest < 9; firstNumTest++){ int divisor=1; for(int scaler = firstNumTest; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; if(place < 10 && place>=1){ // if the place value is now single digit firstPlace = firstNumTest; firstNumTest = 11; } // END of if place value is now single digit (but not decimal) } place = 0; int tempPlace = 0; for(firstPlace; firstPlace>=0; firstPlace--){ int divisor=1; for(int scaler = firstPlace; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; OLED.putChar(place+0x30-tempPlace); tempPlace = place*10; numSize1++; } //code to check if there are any relevant decimal places-- also need to check if there's room to put the decimal places double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); //oldDecimalPlace = (int(decimalPlaceTest*multiplier)*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is greater than 1.00 //Check to see if our answer just 0 (basically zero anyway) else if(input1Value > -0.000001 && input1Value < 0.000001){ numSize1++; OLED.putChar(0x30); } else if(input1Value < 1){ double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); //OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is only a decimal to begin with if(negativeSign1 == true){ input1Value = input1Value*-1; } }//END of non-overflow error }//end of normal division numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; finalAnsScreen = true; }//END of / else{ //if there is no old operator value if(input1Value >= 1000000000 || input1Value <= -1000000000){ OLED.setCursor(0,3); OLED.putString(" Overflow Error "); overflowError = true; numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; finalAnsScreen = true; } else{ //if not an overflow error int place; int firstPlace = 0; numSize1=0; OLED.clear(); if(input1Value < 0.00){ negativeSign1 = true; OLED.setCursor(0,3); OLED.putChar('-'); input1Value = input1Value * -1; //this makes the answer postive for calculation/display purposes //it will be switched back to a negative version at the end of the if '+' statement (if appropriate) } else if(input1Value >= 0.00){ negativeSign1 = false; OLED.setCursor(0,3); OLED.putChar(' '); } OLED.setCursor(15, 2); OLED.putChar('='); OLED.setCursor(1,3); if(input1Value >=1){ for(int firstNumTest = 0; firstNumTest < 9; firstNumTest++){ int divisor=1; for(int scaler = firstNumTest; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; if(place < 10 && place>=1){ // if the place value is now single digit firstPlace = firstNumTest; firstNumTest = 11; } // END of if place value is now single digit (but not decimal) } place = 0; int tempPlace = 0; for(firstPlace; firstPlace>=0; firstPlace--){ int divisor=1; for(int scaler = firstPlace; scaler>0; scaler--){ divisor = divisor*10; }//END of getting the correct divisor scaler place = input1Value/divisor; OLED.putChar(place+0x30-tempPlace); tempPlace = place*10; numSize1++; } //code to check if there are any relevant decimal places-- also need to check if there's room to put the decimal places double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is greater than 1.00 //Check to see if our answer just 0 (basically zero anyway) else if(input1Value > -0.000001 && input1Value < 0.000001){ numSize1++; OLED.putChar(0x30); } else if(input1Value < 1){ double decimalPlaceTest = input1Value - int(input1Value); boolean decimalPoint = false; int restriction = 1; while(decimalPlaceTest > 0.000001 && numSize1 < (9-restriction)){ if(decimalPoint == false){ OLED.putChar('.'); numSize1++; decimalPoint = true; restriction = 0; } OLED.putChar(int(decimalPlaceTest*10)+0x30); numSize1++; decimalPlaceTest = decimalPlaceTest*10 - int(decimalPlaceTest*10); }//END of first check to see if there is room / reason to put a decimal place }//END of if calculated value is only a decimal to begin with if(negativeSign1 == true){ input1Value = input1Value*-1; } numSize1 = 0; subNum1 = 0; input1Value = 0; numSize2 = 0; subNum2 = 0; input2Value = 0.0; negativeSign1 = false; negativeSign2 = false; firstNumValue = true; finalAnsScreen = true; }//END of non-overflow error }//END of no old operator }//END OF calculateFinalAnswer ///////////////////////////////////////////// // // // displaying // // // ///////////////////////////////////////////// void displaying(){ if(charValue=='='){ OLED.setCursor(15,1); OLED.putChar(charValue); calculateFinalAnswer(); } else if(charValue=='+' || charValue=='-' || charValue=='x' || charValue=='/'){ if(numSize2 != 0){ calculatePartialAnswer(); if(overflowError == false){ oldOperatorValue = charValue; OLED.setCursor(1,1); firstNumValue = false; } }//END of checking if the operator is the second operator else{ oldOperatorValue = charValue; OLED.setCursor(15, 0); OLED.putChar(charValue); OLED.setCursor(1,1); firstNumValue = false; }//END of placing the first operator }//END of if charValue is an operator else if(charValue == '.'){ if(firstNumValue == true && decimalPoint1Exist == false && numSize1 < 8){ OLED.putChar(charValue); subNum1++; numSize1++; decimalPoint1Exist = true; } else if(firstNumValue == false && decimalPoint2Exist == false && numSize2 < 8){ OLED.putChar(charValue); subNum2++; numSize2++; decimalPoint2Exist == true; } }//END of if charValue is a decimal point else{ //Entering in the first value if(firstNumValue == true && numSize1 < 9 && subNum1 < 7){ OLED.putChar(charValue); if(subNum1 == 0){ input1Value = input1Value*10; //doing some necessary scaling input1Value = input1Value + double(charValue - 0x30); // adding the input to the value } else if(subNum1 != 0){ double decimalValue = double(charValue - 0x30); for(int scaling = 0; scaling 6 || numSize1 > 8)){ //do nothing } //Entering in the second value else if(firstNumValue == false && numSize2 < 9 && subNum2 < 7){ OLED.putChar(charValue); if(subNum2 == 0){ input2Value = input2Value*10; //doing some necessary scaling input2Value = input2Value + double(charValue - 0x30); // adding the input to the value } else if(subNum2 != 0){ double decimalValue = double(charValue - 0x30); for(int scaling = 0; scaling 6 || numSize2 > 8)){ //do nothing } }//END of entering the rest of the numbers }//END of displaying()