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

Korps Dai Mahasiswa (KDM) Salman ITB

Dai Corps Students, or shortened to KDM Salman ITB , is a recitation of activities held at Salman Mosque and intended for ITB students and also public. Located in the South Corridor Salman Mosque, KDM Salman ITB is trying to bring teaching activities with the concept of traditional pesantren (traditional boarding school). With meetings every day after dhuhur, the students who take part in this Salman KDM get the study material by using the yellow books (kitab kuning) as a handle, as in the traditional pesantren. Teachers who bring the study also vary depending on the items of its study, they are ustadzs from Salman mosque itself. KDM Salman ITB appears to address concerns some students, as well as some ustadzs in mosques Salman, who feel that it is necessary to foster student containers in religious matters. It is motivated by the proliferation of such activities 'mentoring' on campus. But the mentor who brought the material (ie students) were deemed less or even not ...

My Blog's Template (Is it beautiful enough?)

Choosing template for our blog is a little bit difficult because we need to make sure it has a nice structure, easily used and perhaps fit in our style or our soul or our wish. Some blogs I found have sooo good in arranging their view so that I can easily browse its contents and also have an eye catching style which makes my eyes keep open. But some blogs also have (if I can say) terrible design which I won't be able to see it twice. It has a lot of ads here and there, it has awful structures like: "I don't know where to begin with" and "I don't know what function is this button for" or "it has bad color choosing", it has so many pictures and some gifs and our cursor is changed into something gross, and so on. Choosing template or theme or our blog's design no needs to be very beautiful (even I don't say that mine is), but at least it has a 'good' design that allows reader to read it easily. I've tried to create template...

[Arduino Project-1] -- PushButton Lamp

Apa sih Arduino? Salam, Pada posting kali ini akan menjelaskan tentang tugas kuliah Interaksi Manusia dengan Komputer dan Antar Muka, yaitu membuat lampu push button menggunakan Arduino. Saya anggap pembaca telah mengetahui apa itu arduino. Jika belum tahu, akan saya jelaskan sedikit. Arduino bisa dikatakan sebagai alat untuk membuat sebuah prototye. Kita bisa  membuat apapun (hampir apapun) dengan menggunakan arduino. Seperti membuat sensor panas/dingin,bahkan hingga membuat robot. Arduino sebenarnya diperuntukkan bagi para artist maupun designer yang memerlukan kemudahan dalam merancang produk yang hendak mereka buat. Jadi, untuk menggunakannya tidak diperlukan keterampilan khusus yang tinggi karena di design untuk kemudahan (tentunya setelah tahu dasar-dasarnya). Seperti apa sih Arduino itu? Di bawah ini gambarnya Image Source: https://dlnmh9ip6v2uc.cloudfront.net//images/products/1/1/0/2/1/11021-01a.jpg Arduino ada banyak jenisnya yang bisa dilihat...