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
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:
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.
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
- An Arduino Uno
- A BreadBoard
- Jumper Cables
- A Keypad
- A PC which has Arduino App Installed
- A Potensiometer
- A Resistor
What do we do?
First thing we gotta do is to wire our Arduino and keypad as shown below:
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.
Comments
Post a Comment