Skip to main content

[Arduino Project -- 5] -- Using Keypad and LCD Screen at Once to make simple Calculator

Good day.
This is my fifth times of my post about Arduino. We've learned about how to create something using Arduino from the very beginning that was just make a simple push button, till we made something more difficult which was creating a temperature censor and displaying the result in the LCD screen.
And now, i'm gonna show you how to use a keypad and LCD Screen at once.
With these two devices are combined with our Arduino, we are able to make something like a little
application.
And now, i'm gonna make a simple calculator using them.

Stuffs:
These are stuffs you are gonna need to prepare

  1. An Arduino Uno
  2. A BreadBoard
  3. Jumper Cables
  4. A Keypad
  5. A PC which has Arduino App Installed
  6. A Potensiometer
  7. A Resistor


What do we do?
First thing we gotta do is to wire our Arduino and keypad as shown below:
Actually we dont need the speaker and the led. But I put them when i wired them.
And here is my work:
It was a little bit confusing i thought.

And after we wire our components, we are gonna write the codes. Type these lines of codes:
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const byte ROWS = 2; //four rows
const byte COLS = 16; //four columns

const int numRows = 4; // number of rows in the keypad
const int numCols = 4; // number of columns
const int debounceTime = 20; // number of milliseconds for switch to be stable

const int keymap[numRows][numCols] = {
{ 1, 2, 3, -2} ,  // -2: penambahan, -3: pengurangan, -4: perkalian, -5: pembagian, -6: =
{ 4, 5, 6, -3} ,
{ 7, 8, 9, -4} ,
{ 999, 0, -6, -5}
};

const int rowPins[numRows] = { 9,8,7,6}; // Rows 0 through 3
const int colPins[numCols] = { 14,15,17,18}; // Columns 0 through 2

void setup()
{
  Serial.begin(9600);
  lcd.begin(COLS, ROWS);
  //lcd.print("Horray");
 
  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
    digitalWrite(colPins[column],HIGH); // Make all columns inactive
  }
}

int angka1 = 0;
int angka2 = 0;
char operation;
boolean isOpSet = false;
void loop(){
  int key = getKey();
  if( key != -1) { // if the character is not 0 then
      if(key != -2){
        if(key != -3){
          if(key != -4){
            if(key != -5){
              if(key != -6){
                if(angka1 == 0){  //check apakah angka pertama sudah di isi belum. jika belum, mengisi angka pertama
                  angka1 = key;
                  Serial.print(angka1);
                  lcd.print(angka1);
                  //Serial.print("angka pertama kepencet");
                }else{
                  if(isOpSet == false){
                    angka1 = concatenate(angka1,key);
                    Serial.print(key);
                    lcd.print(key);
                  }else{
                    if(angka2 == 0){
                      angka2 = key;
                      Serial.print(angka2);
                      lcd.print(angka2);
                      //Serial.println("angka kedua kepencet");
                    }else if(key != -6){
                      angka2 = concatenate(angka2,key);
                      Serial.print(key);
                      lcd.print(key);
                    }else{
                    }
                  }
                }
              }else{
                Serial.print(" = ");
                lcd.print(" = ");
                Serial.println(hitung(angka1, operation, angka2));
                lcd.println(hitung(angka1, operation, angka2));
                lcd.setCursor(0, 1);
                angka1 = 0;
                angka2=0;
                isOpSet = false;
              }
            }else{
              operation = ':';
              Serial.print(operation);
              lcd.print(operation);
              isOpSet = true;
            }
           }else{
             operation = '*';
              Serial.print(operation);
              lcd.print(operation);
              isOpSet = true;
          }
        }else{
          operation = '-';
          Serial.print(operation);
          lcd.print(operation);
          isOpSet = true;
        }
      }else{
        operation = '+';
        Serial.print(operation);
        lcd.print(operation);
        isOpSet = true;
      }
  }
}

int hitung(int a1, char op, int a2){
  if(op == '+'){
    return a1+a2;
  }else if(op == '-'){
    return a1-a2;
  }else if(op == '*'){
    return a1*a2;
  }else if(op == ':'){
    return a1/a2;
  }
}

unsigned concatenate(unsigned x, unsigned y) {
    unsigned pow = 10;
    while(y >= pow)
        pow *= 10;
    return x * pow + y;        
}

// returns with the key pressed, or 0 if no key is pressed
int getKey(){
  int key = -1; // 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++){
      if(digitalRead(rowPins[row]) == LOW){
        delay(debounceTime); // debounce
        while(digitalRead(rowPins[row]) == LOW); // wait for key to be released
        key = keymap[row][column]; // Remember which key
        }
    }
    
    digitalWrite(colPins[column],HIGH); // De-activate the current column.
  }
  return key; // returns the key pressed or 0 if none
}


Now, compile and upload the code after we connect our arduino to our PC.
And here is my result:


I was ever failed?
Yes. I've failed a lot of times while doing this stuff.
The things were, i've failed to make the LCD worked because some of the reasons:
First, I didn't get the potensiometer. Potensiometer is used to adjust the brightness of the writings light (not the backlight). I've already tried to look for the way to replace the potensiometer with resistors but I still can't get the result.
Second, The keypad (well, maybe it's a little keyboard instead of keypad) that I used was not the same keypad from the book (Arduino Cook Book) which is the 4x4 matrix and has more pins than the one in the book has. This makes no enough pins were left to connect the LCD. As you know, the keyboard has 8 pins, even the one in the book has 7 pins.
So, instead of using LCD, I used the Serial on PC's screen first just to test whether the keypad was working or not.




Testing video to check wheter the keypad was working or not
Yes, it was working. But the thing was, the numbers shown in the screen were different with the numbers that I pressed. What was wrong?
The main problem was to set the pin number for the rowPin and the colPin of the keypad. As I used the 4x4 keypad, i can't use the numbers inside the CookBook. It was annoying....
After I did set the correct numbers for the column and the rows of the keypad. The next step was create the code for calculator. And I was still working with the PC screen too.


Calculator app I made and displayed in the PC screen

Documentations
Here lies the documentations of my work:
My first try


What i did to set the pin numbers.


 


What i got for the first time i tried to use LCD: Black LCD



Comments

Popular posts from this blog

Derita Laptop Ganti: Sahabat Lama Menjadi Asing Bagiku

Sekitar satu tahun yang lalu, aku dan kakakku Mba Lala , tukeran laptop karena ada sedikit masalah dengan OS laptop mba lala, jadi aku memutuskan untuk membawanya buat dibenerin di comlabs aja. Satu tahun berlalu, hari demi hari aku lalui bersama laptop mba lala itu. Susah dan duka (susah dan duka ?) aku lalui bersama laptop toshiba hitam berkulit berudu. Ada kalanya seneng, ada kalanya juga engga. Ada kalanya dia responsif, tapi ada kalanya juga bebel ngambek engga nurut gerakan jari jemari yang bikin hati kesel. Tapi lebih dari itu, tanpa terasa ikatan batin antara aku dan laptop yang engga bisa aktifin bluetooth itu terjalin begitu kuat. Aku seolah tahu dimana letak file movie hanya mengklik ini dan itu dengan mata terpejam bahkan sambil tidur dan mimpi lelap. Aku sudah terbiasa dengan tata letak, environment, dan workspace yang terbangun didalamnya. Dan yang paling penting, laptop tipe satelite itu telah membantuku dalam menyelesaikan berbagai badai tubes dan tucil, membantuku di...

Visiting Animals with My Family

Big day, big day, big day.... /XD Yup, this is a very big day, for me. Why? Because, my parents, and my brother, are coming. I'm pretty excited, but also nervous. You know how it feels when you have a very messy room, and you got your parents are coming? Or you don't know what to do with your family as you have a tiny sized room? Arrhg /shock, it's complicated. But very exciting too. X-D First time of our journey, we were arriving at Sabuga. One of the lovely building style in here (ITB) is the classical stone that covers almost in all the part of the building... Oho, what is this? :P I was not using a professional camera, and I'm not a photographer too, so I couldn't make a good one. hehe. Bye the way, this is under the ground pipe.. Some places in ITB are unique (people said that), and one of them are 'echo point'. This place, if you speak up loud enough, will be able to spread out into all around of ITB and everyone's gonna...

Ramadhan 1435H & Silaturahmi keluarga

Baju baru alhamdulillah, tuk dipakai di hari raya, Tak punya pun tak apa-apa, masih ada baju yang lama. Sepatu baru alhamdulillah, tuk dipakai di hari raya, Tak punya pun tak apa-apa, masih ada sepatu yang lama. Kue baru alhamdulillah, tuk dimakan di hari raya, Tak punya pun tak apa-apa, masih ada kue yang lama. /wahaha Alhamdulillah, tidak terasa ramadhan telah berlalu. Rasa syukur ku kepada Allah SWT. karena telah diberi kesempatan untuk bisa bertemu ramadhan tahun ini sampai selesai. Meskipun Ramadhan udah selesai, semoga hati dan jiwa tetap fitri dan selalu terjaga. Ramadhan kali ini sangat menyenangkan meskipun setengahnya aku jalanin tanpa keluarga, tanpa sanak saudara yang menemani jerih payah puasa. /sweat Tapi ngga papa, yang penting inti dari ibadah bulan ramadhan tetep didapat. #intinya apa ya? Seperti ramadhan-ramadhan sebelumnya, setiap satu minggu sebelum lebaran ibu (dan aku) pasti bikin kue-kue penghias meja. Kali ini pun sama. Aksi bikin kue pun terjadi...