Home > Compilers & Interpreters, Embedded Systems, Projects > Arduino: Valuation calculator of infix mathematical expressions.

Arduino: Valuation calculator of infix mathematical expressions.

September 18, 2010 Leave a comment Go to comments

This project refers to an Arduino sketch that implements a calculator which valuates infix mathematical expressions using appropriate algorithms and data structures. The mathematical expressions are given through the USB port, while the valuation and presentation of results is done by Arduino. The results are displayed in an appropriate LCD display.

The software is well designed to allow retrieving data from a different input too (eg. elementary keyboard). However, in this application we did not have available pins left over to connect a keyboard as we made in previous projects. The mathematical expressions are given by the user in infix notation and with the help of the algorithm ‘Shunting-yard’ and the appropriate stack we perform its conversion to an equivalent postfix one. Also, the postfix expression is valuated with an efficient algorithm.

Currently the calculator supports operator precedence, associativity operators, parentheses, operators of multiple arguments (unary / binary operators) multi-digit integers, floating point result.

Expression Example: 32 + 111 / (4 – !43) * 33 / (2 + !3) + ((3 – 2) / (2 + 1))

For more information, you can get the project itself:

`infix_calculator_lcd_keypad_shield_serial

Various images of the application.

Valuation calculator of infix mathematical expressions – 1.

Valuation calculator of infix mathematical expressions – 2.

Here is the code to use the calculator in Python via USB.

You need to have the package PySerial installed.

#!/usr/bin/env python

try:
    import time, serial
except ImportError, err:
    print ''.join (["Couldn't load module. ", err])
    exit (1)

PORT = '/dev/ttyUSB0'
BAUD = 9600
TOUT = 3

if __name__ == "__main__":
    ser = serial.Serial (PORT, BAUD, timeout = TOUT)

    while True:
        line = raw_input ("Infix Expression: ")
        if line:
            ser.write (line)
            for line in ser.readlines ():
                print line,
        else:
            break
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: