OK, I’m sorry about the title, but I’ll blame it on too much coffee, the real, non-decaf stuff.

Most probably think of a co-processor as one of those math chips like the 8087 that were plugged into the IBM PC and clones back in the dark ages. They essentially allowed coprocessor-aware software such as Lotus 1-2-3 to calculate faster because software-only calculations were soooo slow. But, essentially a co-processor can be any processor that is programmed for a somewhat dedicated function: math, I/O, or even scientific calculations. I wrote this sketch for two reasons, one to learn more about using Streams and the many functions available which help to minimize code that I need to write and second because I wanted to experiment with off loading floating point from an Integer Only uC and not bother with dealing with integer arithmetic and then scaling to decimal.

The goal: An Arduino sketch to perform basic math and some trigonometric functions using the single precision FP library, 4-bytes. The Arduino must accept keyboard input on an element by element basis AND must accept an entire calculation sequence as a stream with flexible use of delimiters. The Sketch should be flexible in output format so that I could easily test (verbose) and should simply stream the answer out the serial port otherwise.

This is NOT a tutorial on correct use of String or Print objects. This is NOT a tutorial on parsing commands from an input stream, although the sketch does perform a sequential validation and extraction of tokens. This sketch could be written better, but a lots of duplication in echoing status information to the screen in interactive mode was desired.

Please accept this sketch as a fun exercise. It is public domain stuff, so bend it, shake it, hack it… but please post enhancements for all. I have not implemented a second uC to use as a command chip, but I am hopeful to complete that effort in a week or so. If this concept works adequately, anyone should be able to host a GPS and off-load the calculations of bearing and distance to the co-processor. With 328P chips being $2 in 25 Qty, the idea of a very cheap calculator that does not impact the main uC should be workable. One idea that is likely to need implementing is a oPin on the Arduino to signify that an answer is ready. This would allow the host uC to use an interrupt routine to snatch the return RS232 answer stream without crudely waiting on an answer.

Test environment: 328 Nano hosted via USB

Have fun. Due to size, the FULL CODE is an attachment… sorry.

If anyone would like to recommend a decent and tested double-precision library for FP, I believe there is enough Free RAM to provide for such an exercise.

Ray

**Commands (NOT case sensitive):**

“DIV”,“MUL”,“ADD”,“SUB”,“Y^X”,“LOG”,“NLG”,“10X”,“1/X”,“e^X”,

“SQR”,“X^2”,“SIN”,“COS”,“TAN”,“ASN”,“ACS”,“ATN”,“DEG”,“RAD”,

“DEC”

**Note:**DEC sets the number of decimals output from 0 to 9

**Examples:** (all RS232 serial to PC over USB using Arduino terminal)

Input: sin 45

**Output:**

Enter Instruction: SIN Found at location 12

Prompting for X: 45.00 D–>R = 0.79 Sin(X) = 0.7071068

Milliseconds = 18 Free RAM = 1452

**Input:** add 2.2 3.0 sub 3.1 4.5 mul 113 3.1 div 355 113

**Output:**

Enter Instruction: ADD Found at location 2

Enter first number secoond number: 2.20 3.00

a + b = 5.1999998

Milliseconds = 39 Free RAM = 1452

Enter Instruction: SUB Found at location 3

Enter minuend subtrahend: 3.10 4.50

a - b = -1.3999998

Milliseconds = 73 Free RAM = 1452

Enter Instruction: MUL Found at location 1

Enter multiplicand multiplier: 113.00 3.10

a * b = 350.3000183

Milliseconds = 82 Free RAM = 1452

Enter Instruction: DIV Found at location 0

Enter dividend divisor: 355.00 113.00

a / b = 3.1415929

Milliseconds = 75 Free RAM = 1452

**Compiled with verbose mode off (0):**

**Input:** add 2.2 3.0 sub 3.1 4.5 mul 113 3.1 div 355 113

**Output:**

5.1999998

-1.3999998

350.3000183

3.1415929

**Input:** dec 3 add 2.2 3.0 sub 3.1 4.5 mul 113 3.1 div 355 113

**Output:**

5.200

-1.400

350.300

3.142

Calculator.ino (10.3 KB)