IR remote calculator

Hey there!
My last project on an arduino was a calculator which was controlled by a keypad,and i was only able to do this using a code i found online and modified it a bit just to suit my hardware, and in an effort to enhance my arduino skills, i'm trying to make a calculator that can be controlled by the keypad and an IR remote simultaneously.But I seriously don't know where to begin.

I know I should use switch() and all,but how do i get two numbers to become one piece of data(5 and 9 become 59), and how do i perform actions on them?
Please DO NOT post/share the full code for achieving this,I only want guidelines.

Any help would be appreciated

This is my code so far(I only added the definitions and the receiver part(end of the code) onto the previous keypad -only calculator).

/* 
 || @version 1.0
 || @author Andrew Mascolo
 || @date May 15, 2013
 ||
 || @description
 || Simple use of keypad and LCD
*/
#include <Keypad.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <IRRemote.h>

#define FF6897 0
#define FF30CF 1
#define FF18E7 2
#define FF7A85 3
#define FF10EF 4
#define FF38C7 5
#define FF5AA5 6
#define FF42BD 7
#define FF4AB5 8
#define FF52AD 9

int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

long first = 0;
long second = 0;
double total = 0;

char customKey;
const byte ROWS = 4;
const byte COLS = 4;

char keys[ROWS][COLS] = {
  {'1','2','3','+'},
  {'4','5','6','-'},
  {'7','8','9','*'},
  {'C','0','=','/'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); 

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

void loop()
{
  customKey = customKeypad.getKey();
  switch(customKey) 
  {
  case '0' ... '9': // This keeps collecting the first value until a operator is pressed "+-*/"
    lcd.setCursor(0,0);
    first = first * 10 + (customKey - '0');
    lcd.print(first);
    break;

  case '+':
    first = (total != 0 ? total : first);
    lcd.setCursor(0,1);
    lcd.print("+");
    second = SecondNumber(); // get the collected the second number
    total = first + second;
    lcd.setCursor(0,3);
    lcd.print(total);
    first = 0, second = 0; // reset values back to zero for next use
    break;

  case '-':
    first = (total != 0 ? total : first);
    lcd.setCursor(0,1);
    lcd.print("-");
    second = SecondNumber();
    total = first - second;
    lcd.setCursor(0,3);
    lcd.print(total);
    first = 0, second = 0;
    break;

  case '*':
    first = (total != 0 ? total : first);
    lcd.setCursor(0,1);
    lcd.print("*");
    second = SecondNumber();
    total = first * second;
    lcd.setCursor(0,3);
    lcd.print(total);
    first = 0, second = 0;
    break;

  case '/':
    first = (total != 0 ? total : first);
    lcd.setCursor(0,1);
    lcd.print("/");
    second = SecondNumber();
    lcd.setCursor(0,3);

    second == 0 ? lcd.print("Invalid") : total = (float)first / (float)second;

    lcd.print(total);
    first = 0, second = 0;
    break;

  case 'C':
    total = 0;
    lcd.clear();
    break;
  }
}

long SecondNumber()
{
  while( 1 )
  {
    customKey = customKeypad.getKey();
    if(customKey >= '0' && customKey <= '9')
    {
      second = second * 10 + (customKey - '0');
      lcd.setCursor(0,2);
      lcd.print(second);
    }

    if(customKey == '=') break;  //return second;
  }
 return second; 


if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);
    irrecv.resume(); // Receive the next value
  }
  delay(100);
   
}

Note that I changed the library name from 'IRremote' to 'IRRemote' because a library with that name already exists.

Happy New Year :wink:

I know I should use switch() and all,but how do i get two numbers to become one piece of data(5 and 9 become 59), and how do i perform actions on them?

As long as you are receiving 'integer' values....

value = value*10+newValue; //for decimal values

Presumably, you have some logic to detect when you are no longer receiving values, like after +-*/= etc., after which you 'may' reset value to 0 (depending on context).

Note: a wee bit more complicated if decimal places or fractions are involved.

mero55:
Hey there!
My last project on an arduino was a calculator which was controlled by a keypad,and i was only able to do this using a code i found online and modified it a bit just to suit my hardware, and in an effort to enhance my arduino skills, i'm trying to make a calculator that can be controlled by the keypad and an IR remote simultaneously.But I seriously don't know where to begin.

I know I should use switch() and all,but how do i get two numbers to become one piece of data(5 and 9 become 59), and how do i perform actions on them?
Please DO NOT post/share the full code for achieving this,I only want guidelines.

Any help would be appreciated

::::SNIP::::

mero55,
I worked it all out, but at you requested - I'm not posting my code.

GUIDELINES)

  1. Separate you input from your output. Your logic should be:
  • capture input
  • evaluate character
  • take action on the character
  • print appropriate output
    ALSO, #2 is the key, and you MAY want to use some boolean (logic) flags, instead of trying to figure out the program flow. This will help make things modular.
  1. Don't worry about future features. I see that thinking in your function SecondNumber(). This is a trap, fight featuritis.
  2. Decide on your output format. By waiting to put all your output at the end of the loop, this makes it so you can control the output format. Whereas if you intersperse your "print" statement, it becomes difficult to change or fix any format mistakes. It also separates two problems (#1 what to do with the numbers, #2 how to show the numbers)
  3. Putting a while( 1 ) is equivalent to writing goto. AVOID goto at all costs. THINK MORE ABOUT AVOIDING THIS.

There is more, but this is a good start. And Lastly, I have saved the code I wrote, if you are ever interested in looking at it.

Jesse

You'll notice that as you shift a digit towards the left in a number, it's value gets multiplied by 10 each time.

So this is how it's done.

You start with a running total of zero.
Every time you recieve a new digit, you multiply your running total by 10
Then add on the new digits value.

KenF:
You'll notice that as you shift a digit towards the left in a number, it's value gets multiplied by 10 each time.

So that's how counting works.... :wink:

(I was trying to decipher a long Roman numeral the other day, and wondered how their Empire lived so long if that's how they did their numbers. Must have been a nightmare, but they did some pretty serious engineering all the same.)

JimboZA:
(I was trying to decipher a long Roman numeral the other day, and wondered how their Empire lived so long if that's how they did their numbers. Must have been a nightmare, but they did some pretty serious engineering all the same.)

The Romans were hopeless with electronics though. V=IXR Just didn't work.

But apart from the sanitation, the aquaducts and the roads, what have the Romans ever done for us?