IR Calculator

by xJaagrav in Circuits > Arduino

5206 Views, 25 Favorites, 0 Comments

IR Calculator

SharedScreenshot.jpg
IR Calculator

Hey, welcome to my first instructable where I will be helping you create a calculator. It's basically made up of a 16x2 LCD, with an Arduino UNO that you can use in order to make quick calculations from far distances. I know, it doesn't make sense right, well I did not have a keypad but I did have an IR Remote and Receiver so I thought, why not I should use my remote as my keypad. Now that's not where it ends, the code I have written for this calculator does not include any kind of bugs that I have seen in most Arduino calculators.

Here is a list of features that I have added in this calculator:

  • Make simultaneous calculations
  • Use decimal point
  • Erase the last character
  • Gotten rid of bugs that take place during runtime, like having multiple operators or decimal points, etc.

Now of course you can edit the code and make it work with the help of a keypad, and use the Arduino Nano to create your whole calculator in a small package, but since this is my first instructable, I decided to keep this as raw as possible. I hope you like this instructable, all the necessary stuff required are listed down below so without any further ado, let's make an RC calculator XD.

Components

519-y7vsUrL._AC_.jpg
1447-04.jpg
61UtCkyM55L._SL1200_.jpg
1k-ohm-resistor-350x350.jpg
220-ohm-resistor-350x350.jpg

The first thing to do when beginning with any project is gathering the required components. The required components for this project are as follows:

  1. Arduino UNO R3
  2. 16x2 LCD RG1602A
  3. IR Receiver
  4. 220 Ohm resistor
  5. 2x1k Ohm resistor

And of course, do not forget your IR Remote Control, if you don't have one like the one I do in the video, you can simply use your TV or AC remote.

Libraries:

  1. IRremote by shirriff, z3t0, ArminJo - v2.5.0
  2. LiquidCrystal by Arduino, Adafruit - v1.0.7

Make sure you are using these packages and have installed the same version, in some future versions some of the functionality of the packages used in this project like IRremote or LiquidCrystal might be deprecated, which might lead to unexpected errors.

Schematics

t725.png
d7f3fbb1-a318-48f5-8581-520f24f6f4e6.png

Now here is the circuit diagram for the hardware we are building for the RC calculator. Make sure that you have an IR remote with you or else it may not work at all.

Here is a table representing where to connect which pin to where on the Arduino board and the components, take a look at it carefully:

https://github.com/Jaagrav/IRcalculator/

Coding

Screenshot (177).png

Here's the code you're going to need in order to make your IR Calculator work. Simply just copy and paste the code to your arduino, now I have left some comments in the code that you might want to take a look at before blindly copying the pasting the code. I'll explain that anyway in the next step.

/*
This is an IR Remote Controlled Calculator.
I have tried to get rid of all the bugs that I have seen
in other calculators made using an arduino. This calculator
can be used like a normal calculator, you can make 
simultaneous calculations with this, add points, etc.
The LCD Display used here is a RG1602A,
you may refer to this article to understand how to use the display,
https://create.arduino.cc/projecthub/najad/interfacing-lcd1602-with-arduino-764ec4
All you need to do is connect the wires in the corresponding
pins written down below and edit the IR Codes:
|------#------Arduino Pins------LCD RG1602A--------------------|
|----- 1 ----     GND     ----- VSS,V0 (With 2K ohm),RW,K -----|
|----- 2 ----     5V      ----- VDD, A (With 220 ohm)     -----|
|----- 3 ----     12      ----- RS                        -----|
|----- 4 ----     11      ----- E                         -----|
|----- 5 ----     5       ----- D4                        -----|
|----- 6 ----     4       ----- D5      	          -----|
|----- 7 ----     3       ----- D6                        -----|
|----- 8 ----     2       ----- D7                        -----|     
|--------------------------------------------------------------|
|------#------Arduino Pins------IR Transmitter-----------------|
|----- 1 ----     10      ----- Out                       -----|
|----- 2 ----     GND     ----- GND                       -----|
|----- 3 ----     5V      ----- Power                     -----|
|--------------------------------------------------------------|
Check it out on Github: https://github.com/Jaagrav/IRcalculator/
*/

#include <IRremote.h>
#include <LiquidCrystal.h>
#include <math.h>

// Digital Pin Connections to your LCD
const int 
  rs = 12, 
  en = 11, 
  d4 = 5, 
  d5 = 4, 
  d6 = 3, 
  d7 = 2;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// Digital Pin Connection to your IR Receiver
IRrecv irrecv(10);
decode_results results;

String 
  number1 = "0",
  number2 = "0",
  optr = "=",
  sixteenString = "                ";

/*
In the below switch-case replace the numbers with the IR codes
from your remote. Make sure you write the code that gets printed
in your serial monitor from line 142.
*/

void acceptInput(int character) {
  Serial.println(character);
  switch(character) {
  	case 2222: 
      concatNumbers("1");
    break;
    case -31092:
      concatNumbers("2");
    break;
    case 18888:
      concatNumbers("3");
    break;
    case 10000:
      concatNumbers("4");
    break;
    case -22203:
      concatNumbers("5");
    break;
    case 26666:
      concatNumbers("6");
    break;
    case 6333:
      concatNumbers("7");
    break;
    case -25537:
      concatNumbers("8");
    break;
    case 22222:
      concatNumbers("9");
    break;
    case 12222:
      concatNumbers("0");
    break;
    case 28888:
      concatNumbers(".");
    break;
    case 255:
      number1 = "0";
      number2 = "0";
      optr = "=";
    break;
    case 32222:
      function("+");
    break;
    case -28870:
      function("-");
    break;
    case 24444:
      function("/");
    break;
    case 8444:
      function("x");
    break;
    case 45555:
      if(optr != "=")
        calculate("=");
    break;
    case 4333:
      backSpace();
    break;
    default: 
    Serial.println("Invalid Input");
  }
}

void setup() {
  Serial.begin(9600);
  
  lcd.begin(16, 2);
  irrecv.enableIRIn();
}

void loop() {
  if (irrecv.decode(&results)) {
    unsigned int result = results.value;
    String val = String(result);
    acceptInput(val.toInt());
    irrecv.resume(); 
  } 
  lcd.setCursor(0,0);
  lcd.print(optr + "  " + sixteenString.substring(number1.length() + 3) + number1);
  lcd.setCursor(0,1);
  lcd.print(sixteenString.substring(number2.length()) + number2);
}

void calculate(String op) {
  double no1 = number1.toDouble();
  double no2 = number2.toDouble();
  double calcVal = 0.0;
  
  if(optr == "+")
    calcVal = (no1 + no2);
  else if(optr == "-")
    calcVal = (no1 - no2);
  else if(optr == "x")
    calcVal = (no1 * no2);
  else if(optr == "/")
    calcVal = (no1 / no2);
  
  number1 = toString(calcVal);
  number2 = "0";
  optr = op;
}

String toString(double num) {
  return String(num);
}

void function(String e) { 
  if(number1 != "0" && number2 != "0") {
    calculate(e);
  }
  else if(number1 == "0") {  
    number1 = number2;
    number2 = "0";
  }
  optr = e;
}

void concatNumbers(String num) {
  if(optr == "=")
    number1 = "0";
  if(num != "."){
    if(number2.length() == 1 && number2 == "0")
       number2 = num;
     else
       number2 += num;  
  }
  else {
    if(number2.charAt(number2.length()-1) != '.' && number2.indexOf('.') == -1)
      number2 += num;
  }
}

void backSpace() {
  number2 = number2.substring(0, number2.length() - 1);
    if(number2 == "")
      number2 = "0";
}<br>

Downloads

Getting Your Remote's IR Codes and Replacing Them With the Default Ones

Screenshot (178).png

Now as you can see in the image, you need to replace those numbers with the IR codes of the buttons from your remote that you get from your serial monitor. How, you ask? It's simple but it takes a bit of time.

Once your circuit is ready and the code has been uploaded successfully to your serial monitor, fire up your serial monitor, that's where you'll find the codes for your remote once you press any button on the remote.

Follow these steps to get your button codes from your remote,

  • Build the circuit, make sure every component is connected to the right pin.
  • Upload the code to your Arduino (CTRL + U).
  • Open up your serial monitor (CTRL + SHIFT + M)
  • Now press the buttons that you want to use as the function buttons (such as 0,1,2,+,-,etc.) on your remote, you'll notice there are certain codes popping up in your serial monitor everytime you press a button on your remote.
  • Open up your notepad and store these codes.
  • Replace the codes with the previously written codes respective of their buttons in the Arduino code file.

And that's it!

And Here We Go!

IR Calculator

Make sure all your connections to every pin is correct, the code is uploaded, and the remote codes has been replaced with the ones that are emitted by your remote. If you have followed all the previous steps properly, then there shouldn't be any issue and everything should run. Hence your calculator is ready!

It should work just the way I showed in the video above. If it doesn't, feel free to reach out to me on Twitter I'll help you out in case you face any kind of problem.

Now you can simply press the buttons that you have assigned a function to in the switch-case in the code and your can simply make standard calculations. You can test for bugs and in case if you wish to make changes/improvise the code then you can simply create an issue or make a pull request to the code repository.

If it works perfectly, then congrats! You have made your own remote control calculator successfully, great job! Hope to see you make something better and improvise this project!

Thank You!