Picture of Arduino Scientific Calculator
UPDATED Section 4 on 20130301 to show Analog Pin Read (APR) code and updated Digital Pin Read (DPR) code.

I recently went Googling for examples of calculator programs (sketches) for my Arduino Nano.  I found a number of examples on how to interface an Arduino to the keyboard of an old calculator, but did not get any hits of pre-written sketches for turning an Arduino into a scientific calculator... you know, the ones with Sin, Cos, Tan, and etc. (Excepting one clever implementation of a full version of an HP 45 but it required a massive IBM PS/2 keyboard.)

Why would anyone want such a thing?  The answer to this question is probably why I could not find anything on the Internet!  But I had my reasons and so I was forced to write my own sketch.  I sat down with paper and pencil and wrote down (while looking at my old HP instruction manual from college) and came up with a list of things I wanted: the obvious +, -, *, and / (add, subtract, multiply, and divide) and the more involved ones: Sin, Cos, Tan, SQR, Log, and so on.  Then there were the nice to have power functions.  Then the arcSin, arcCos, and arcTan stuff.  The list got a bit long... too long to assign a single keyboard character to each operation.

Then there was the concern about how to get my input into the Arduino.  Generally, one types in something and presses the Enter Key to confirm that they are finished with that entry.  Then the next bit, and then the next bit... each followed with that Enter key.  I'm not particularly fond of the Enter key so I wanted to send everything to the Arduino at one time:  The operation to perform and one or two numbers as required.  All I wanted the Arduino software to do was spit back the answer!  After finishing the program, the bulk of the code is in the area of sending back things that I really did not want... for testing and such.  So, I created two ways to build the application, one with all the voluminous output and one with just the answer; I call this verbose mode and the obvious non-verbose mode.  Verbose mode is useful if you want the operator prompted for each value and for viewing settings such as Decimals and Degree-Radians.

Remove these adsRemove these ads by Signing Up
how many decimal of pi can you get it to calculate?
RayBurne (author)  goldbar29751 year ago
... In calculations, 355 DIV 113 comes in very close to Pi

RayBurne (author)  goldbar29751 year ago
Pi is a constant in the code:
float Pi = 3.141592654;

Decimals default to 7 places. You can override this in script to execute:
(but remember that floats in Arduino are only 4-byte IEEE numbers, so 10 digits really does not make a lots of sense.)

byte decimals = 7;
case 20: // DEC set decimal places
if (verbose) {Serial.print(F("Prompting for Decimal places 0-10: ")); }
b = Serial.parseInt();
if (b>10) {b = 10; }
decimals = b;
if (verbose) {Serial.print(F(" Decimal Places = "));
Serial.print(b); }
if (verbose) {Serial.println(); }
maewert2 years ago
I love it Ray. I'm thinking about changing verbose to a boolean and set/clear it via VM+ VM- commands. Also maybe storing LastX into a circular array or 255 values and being able to use past values via an ANS -N (or could use a STO to store it at a location). You may want to include functions to set the pins I or O and to enable the pull up resistors. A matrix solver could also be useful as well as unit conversions... the list goes on and on :-)
Best Wishes
RayBurne (author)  maewert2 years ago
Writing this was an educational exercise for me since I wanted to play with the new Arduino 1.0+ streaming commands. The for-loop serial parser really needs a new architecture if many more commands are needed to minimize the loop delay. Also, I should have used a compiler directive instead of the 'verbose' implementation used.

But, all in fun. Another idea would be to use eeprom to implement numerous physical constants instead of inline code.

The IO extension may actually be the most productive use of this concept because it could allow some remote intelligence to be added via RF RS232 comm. In such an implementation, I would op to create a response channel so that the main uC actually knows that the remote did its bidding.

Evolve and repost! Also a thread is open in the Arduino forum:
Arduino Forum ::
Community ::
Exhibition / Gallery ::
Don't Cross The Streams (FP scientific calculator serial co-processor)