Project 4 - Calculator

Arduino Project 4

This time, the project is about prototyping a calculator using keypad and LCD. During project development, we have some obstacles. The processes will be shown below.

The first thing to do was gathering the components, which consists of:
  • Arduino UNO
  • Breadboard
  • LCD
  • Keypad
  • Potensiometer
  • Resistor 220 Ohm
  • Jumpers
We were testing the keypad first...
and we succeeded testing the keypad in one go!


Then, we combine the components into a circuit that looks like this.



After finish designed the circuit, we start to code and upload the sketch to the arduino board.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int inPin = 0;const int numRows = 4;
const int numCols = 4;
const int debounceTime = 15;const char keymap[numRows][numCols] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};const int rowPins[numRows] = { 0, 1, 6, 7 }; // Rows 0 through 3
const int colPins[numCols] = { 8, 9, 10, 13 };void setup() {
  lcd.begin(16,2);
for (int row = 0; row < numRows; row++)
{
pinMode(rowPins[row],INPUT); // Set row pins as input
digitalWrite(rowPins[row],HIGH); // turn on Pull-ups
}
for (int column = 0; column < numCols; column++) 
{
pinMode(colPins[column],OUTPUT); // Set column pins as outputs
// for writing
digitalWrite(colPins[column],HIGH); // Make all columns inactive
}
lcd.setCursor(1,0);
lcd.print("Hasil:");
}void loop() {
  char key1=0;  key1 = getKey();
  if( key1 != 0) { // if the character is not 0 then
  //it’s a valid key press
  if (key1 == '*'){
    lcd.clear();
    lcd.setCursor(1,0);
    lcd.print("Hasil:");
  }  else{
char key2 = 0;
char key3 = 0;
lcd.setCursor(0,1);
int a = key1 - '0';
lcd.print(a);
while (key3 == 0){
key3 = getKey();
if (key3 != 0 ){
char op = convert(key3);
lcd.print(op); } }
while (key2 == 0){
key2 = getKey();
if (key2 != 0 ){
int b = key2 - '0';
lcd.print(b);
double c = calculate(key3,a,b);
lcd.print('=');
delay(1000);
lcd.print(c); } } } }}int calculate (char key3, int a, int b)
{
if (key3=='A'){
return(a+b);
} else
if (key3=='B'){
return(a-b);
} else
if (key3=='C'){
return(a*b);
}
if (key3=='D'){
return(a/b); } }char convert (char key3)
{
if (key3=='A'){
return('+');
} else
if (key3=='B'){
return('-');
} else
if (key3=='C'){
return('x');
}
if (key3=='D'){
return(':'); } }char getKey()
{
char key = 0; // 0 indicates no key pressed
for(int column = 0; column < numCols; column++)
{
digitalWrite(colPins[column],LOW); // Activate the current column.
for(int row = 0; row < numRows; row++) // Scan all rows for
// a key press.
{
if(digitalRead(rowPins[row]) == LOW) // Is a key pressed?
{ 
delay(debounceTime); // debounce
while(digitalRead(rowPins[row]) == LOW); // wait for key to be released
key = keymap[row][column]; // Remember which key
// was pressed} }
digitalWrite(colPins[column],HIGH); // De-activate the current column.
}
return key; // returns the key pressed or 0 if none
}

 As long as we keep trying, the LCD won't show any letter... Then, we try again using different LCD, and after working hard for about one hour...

FINALLY, we succeded! :D




This project consists of three people (Edria, Jessy, and Katriel). Oh no, we forgot to take our picture and record a video because we were such in a rush for another appointment.

That's it for this project. See you :))

source : http://www.microcontroller-project.com/calculator-using-arduino.html

Comments