I Wrote this code using LCD 5110 Library and Ps2keyboar Library
Is a simple calculator (+ - * / operations) , uses RPN notation
its display the user input like a desk calculator
(the digits roll to left, and last number digit appear on the right)
//Simple Calculator By Endrio Lázaro
// This program requires a Nokia 5110 LCD module.
//
// It is assumed that the LCD module is connected to
// the following pins using a levelshifter to get the
// correct voltage to the module.
// SCK - Pin 8
// MOSI - Pin 9
// DC - Pin 10
// RST - Pin 11
// CS - Pin 12
//
//PS2 Keyboard (for arduino uno, irq pin must be 2 or 3)
//DataPin = 4;
// IRQpin = 3;
//
// this program use LCD5110_NumberFonts (C)2013 Henning Karlsen
// see more: http://www.henningkarlsen.com/electronics
//
#include <String.h>;
#include <LCD5110_Basic.h>
#include <PS2Keyboard.h>
const int DataPin = 4;
const int IRQpin = 3;
PS2Keyboard keyboard;
LCD5110 myGLCD(8,9,10,11,12);
extern uint8_t SmallFont[];
extern uint8_t MediumNumbers[];
extern uint8_t BigNumbers[];
char c='1';
//mathematic operations
float Stack;
//Total
float Input;
//new number entered by user
String Stacks;
// to manipulate the stack
String Inputs;
// to manipulate new input
int Size;
//control if user has entered a new number
char lastOper;
// last number in the stack
float lastNum;
void printLcd()
{
beep(50);
myGLCD.clrScr();
myGLCD.setFont(MediumNumbers);
myGLCD.printNumF(Stack/100, 2, RIGHT, 24);
//display total bellow display
if (Size!=0)
{
Input = Inputs.toInt();
myGLCD.printNumF(Input/100, 2, RIGHT, 9);
//display user input above
}
else
{
myGLCD.printNumF(lastNum/100, 2, RIGHT, 9);
}
myGLCD.setFont(SmallFont);
myGLCD.print("total", LEFT, 45);
}
void printLcdTotal()
{
myGLCD.clrScr();
myGLCD.setFont(MediumNumbers);
myGLCD.printNumF(Stack/100, 2, CENTER, 24);
myGLCD.setFont(SmallFont);
myGLCD.print("TOTAL", LEFT, 0);
beep(600);
}
void beep(unsigned char delayms)
{
analogWrite(6, 80);
// Almost any value can be used except 0 and 255
// experiment to get the best tone
delay(delayms);
// wait for a delayms ms
analogWrite(6, 0 );
// 0 turns it off
delay(delayms);
// wait for a delayms ms
}
void setup()
{
delay(1000);
pinMode(6, OUTPUT);
keyboard.begin(DataPin, IRQpin);
Serial.begin(9600);
Serial.println("Keyboard Test:");
myGLCD.InitLCD();
printLcd();
myGLCD.invert(true);
}
void loop()
{
if (keyboard.available())
{
// read the next key
char c = keyboard.read();
int t = c;
Serial.print(t);
//if key is a number, put in the last position of input string
if ((c=='1')||(c=='2')||(c=='3')||(c=='4')||(c=='5')||(c=='6')||(c=='7')||(c=='8')||(c=='9')||(c=='0'))
{
if (Size <=5 )
{
//max size of string
Inputs= Inputs+c;
Size++;
}
}
// if ENTER is pressed do ADDICTION
if (c==13)
{
Serial.print(Size);
if(Size==0)
{
//if no number entered by user, uses last entered number
Stack += lastNum;
}
else
{
Stack += Inputs.toInt();
lastNum = Inputs.toInt();
Inputs = "";
//reset user input
Size = 0;
}
myGLCD.invert(false);
//invert screen if minus operator is used
}
// If + is pressed do subtraction
if (c=='+')
{
Serial.print(Size);
//debug
if(Size==0)
{
//if no number entered by user, uses last entered number
Stack += lastNum;
//if no number entered use last number entered
}
else
{
//if no number entered use last number entered
Stack += Inputs.toInt();
lastNum = Inputs.toInt();
Inputs = "";
//reset user input
Size = 0;
}
myGLCD.invert(true);
//invert screen if minus operator is used
}
// do * operation
if (c=='*')
{
//multiply Stack by the number entered by user;
Stack *= Inputs.toInt();
Inputs = "";
//reset user input
Size = 0;
}
// do * operarion (not complete, may give a by 0 division)
if (c=='/')
{
//divide Stack by the number entered by user;
Stack /= Inputs.toInt();
Inputs = "";
//reset user input
Size = 0;
}
printLcd();
// print to lcd
//if comma is pressed, give total;
if (c==46)
{
printLcdTotal();
Inputs = "";
//reset user input
Size = 0;
}
}
}
calcularora.ino (4.51 KB)