loading

Nowadays, doing calculations isn't a problem at all. Powerful calculators can be bought for few Euro or easily installed on phones and computers. However, we decided to turn our Arduino into a calculator because we think it could be a really interesting experiment.

It's actually a very simple calculator, in fact it can just do the four basic calculations accepting only integer positive numbers. Despite this fact, the sketch is quite complex - and you'll figure it out on next steps -, but the circuit is pretty easy instead and requires few and common components.

Here is the list of the components needed to follow all* the steps of this tutorial:

  • Arduino MEGA
  • MB-102 Breadboard
  • 12 push-buttons
  • 12 10kOhm resistors
  • LCD 16x2 display
  • 10kOhm linear potentiometer
  • some Dupont cables

As you can see, we didn't use a pre-made keypad but our own Techrm's, made of single push-buttons.

*if you don't own a LCD display, by printing outputs on serial monitor you'll be able to do this tutorial anyway; just swap Serial.print() functions with lcd.print() and the problem is solved.

Step 1: User's Guide...in Other Words, How to Use Techrm's Calculator.

This calculator does the four basic calculations: addition, subtraction, multiplication and division. It only accepts two integer positive numbers and each number as well as the result can't be greater than 99999.

Getting a negative value is only possible when doing a subtraction whose the second number is greater than the first one.

NOTE: keeping a button pressed for long will insert a number twice or more!

How it works

The first screen allows users to choose one of the four above-named basic calculations. The selection takes place pressing:

  • 0, which stands for ADDition
  • 1 for SUBtraction
  • 2 for MULtiplication
  • and 3 for DIVision

Pressing a different key makes the calculator show an alert asking user to choose a proper operation.

Once selected a task, the calculator allows user to type the first digit of the first number. When finished, the second number has to be typed. Since the calculator accepts 99999 as greatest number, you only have the possibility to type 5 digits per number. Typing a 5 digit number makes the calculator switch to the next step. If you want to type a number whose digits are lesser than 5, press OK to insert the second number or to confirm the operation if you are typing the second one.

The result of a calculation will remain visualized on the LCD display (or serial monitor) unless the user won't press DEL.

Anytime it's possible to press the DEL key in order to interrupt the current operation and come back to the opening screen.

Deleting the last number or digit is not allowed.

Step 2: The Sketch

Surely, the first step is connecting the push-buttons in order to make a keypad. Their connection is pretty easy and you'll only need to have a look at the Fritzing scheme attached.

NOTE: Remember that the push-buttons have to be placed between the two middle sections of the breadboard to avoid short circuits. So, we won't be able to place our push-buttons like a real rectangular-shaped keypad but like a line-shaped one.

Something really useful is to write keypad names on a piece of paper.

Linking push-buttons to numbers and control keys

The keypad has 12 buttons: 10 are needed to represent numbers from 0 to 9 and two control the operation flow. Thanks to some #define we are able to link each button to a specific PIN.

Push-buttons are normally open. That means their default state is set to LOW. Pushing them make their state become HIGH. Just when their state is set to HIGH

The act of switching the state of a button from LOW to HIGH determines the insertion of a digit (if you are pressing a key-button number) or an "event" (if you are pressing one of the two control buttons). We implemented it in the readKey () function.

The two above-named states are caused by the circuital solution used for this scheme. Thanks to the pull-down resistors connected to the ground, the output level is 0 when buttons are released (open switch) and 1 (5 Volts) when pressed (switch closed).

The keypad is the tool that allows us to give commands to Arduino, that's why push-button PINs are set as INPUT into the void setup () function.

As is known, the two essential functions of an Arduino sketch are void loop () and void setup (). The first function is only run once and basically does some controls and/or initializes PINs and components. Into the second one there is the code that runs repeatedly. So, to halt indefinitely the code execution we made the wait() function. When Arduino executes this function goes into a sort of "wait state"; indeed, this while loop is empty and true until no button is pressed. Any key pressed makes Arduino go out from this loop and execute the next instruction.

The insertNumb () function

The most complex part of the code is surely the one included into the void insertNumb () function. Except from a lot of conditional statements, the main idea behind this function are the two for loops. The first loop scans digits from first numbers while the second one scans digits from seconds.

As said above, the function looks very complex because of many conditional statements. These statements are needed to manage the data flow capture.

Basically, they force the function to interrupt itself if the user presses DEL or go on if presses OK.

For example, if one presses OK while the first loop is running, a conditional statement increases the value of "i" variable from x to 5. So, the if statement which precedes the following for loop becomes true allowing the data capture of the second number digits.

Reading the source code it's possible to see a lot of delay, apparently spread in random manner. Actually they are very important because synchronize keypad operations. For instance, the first delay into the void insertNumb () is necessary to avoid the interpretation of selection keys as the first digit of the first number.

void loop ()

The code into this function could look a bit complex at a glance but what happens is actually quite simple.

The main variable is called operation. It only assumes four values: 0, 1, 2, and 3 corresponding to the previously named basic calculations.

Once chosen an operation, thanks to some conditional statements the algorithm goes to the right part of the code and does the calculation selected by the user. Since the maximum allowed value is 99999, those conditionals statements also check if this number gets overtaken.

Step 3: Converting a Part of the Sketch Into a Library

Some parts of the sketch have to be repeated more than once because of the "nature" of the keypad. That's why we thought it would have been advantageous to write a library. Furthermore, working with a library is the simplest way to add or modify functions because you only have to adjust one file instead a lot.

We are not going to explain how to write a library for Arduino both because an excellent *official guide already exists and because we don't want the tutorial become uselessly long. What we are going to do is a list of its functions and which are the parameters they require. And also, we won't explain what the methods do because we did it in the previous step.

NOTE: the library only works with a 16x2 LCD display, so it can't be used if you chose to print outputs on serial monitor unless you decide to modify it.

How to use the library

TechrmKeypad is the name of the class.

The first thing we are going to do is to instance an object of the class passing the push-button parameters to it.

Example:

TechrmKeypad keypad (key0, key1, key2, key3, key4, key5, key6, key7, key8, key9, DEL, ENTER);

The following instructions are the methods available:

keypad.wait (): is a void method and doesn't require any parameter.

keypad.readkey (): as the previous one, this is a void method and doesn't require any parameter.

keypad.insertNumb (&lcd, &control, &error, &number_1, &number_2, &MAXVALUE): this is also a void method but it requires some parameters. First, it gets a pointer of an instance of the LCD and then the pointers of the following global variables:

  • control_
  • error_
  • number_1_
  • number_2_
  • MAXVALUE

In addition to these methods you could implement another one to wrap the following lines which are repeated many times:

void printOpenmesg (char* myString1, char* myString2) {
	lcd_.clear();
	lcd_.setCursor(0, 0);
	lcd_.print(myString1);
	lcd_.setCursor(0, 1);
	lcd_.print(myString2);
} 

Step 4: Final Considerations

The calculator can currently do very basic calculations but adding some buttons it is possible to make it do more complex ones. For example, only two buttons - a button for the "-" sign and one for the decimal point - would make the calculator much more versatile. One could also take advantage of the math.h library in order to add different kind of algebraic calculations such as logarithms, exponentiations, square roots, etc etc.

However, such choices would dramatically increase the complexity of the sketch. As seen in the previous steps, just for this "simple" calculator we needed about 300 lines of code (500 without any library!)

So, if you are brave enough, feel free to modify our library adding new features or simply making it better!

<p>You can try using 74HC165 Parallel In, Serial Out Shift Register, it provides you 8 input pins with only 3 control pins. You can have a look here, <a href="https://iamzxlee.wordpress.com/2014/05/13/74hc165-8-bit-parallel-inserial-out-shift-register/" rel="nofollow">https://iamzxlee.wordpress.com/2014/05/13/74hc165-...</a></p><p>Best of all, these thing can be daisy chain for a lot of pins but still using the 3 control pins.</p>
<p>Hi zx lee, we decided not use any additional device in order to keep the circuit and the material required as simple as possible. In fact, one only needs few push-buttons and resistors (because the LCD display isn't mandatory) to follow the tutorial.</p><p>We think this tutorial could be seen as an interesting &quot;programming exercise&quot; rather than a standard Arduino project.</p><p>Anyway, thanks a lot for you comment!</p><p><a href="https://www.instructables.com/member/zx+lee/" rel="nofollow"><br></a></p>
<p>Cool instructable, I once did an arduino calculator using an UNO which you could control using an IR remote.</p>
<p>Hi mero55, thank you for your comment</p>
Great job - but you must use a shift register for this job...
<p>Hi Deadly shadow, thanks for your suggestion!</p>

About This Instructable

7,214views

55favorites

License:

More by techrm:A Simple Hydroponic System With MediaTek LinkIt™ ONE Getting started with BTE13-010 - Arduino Mini clone Simple calculator based on Arduino MEGA 
Add instructable to: