Arduino Project: Keypad

Keypad Calculator Project:

About this Project:
This is the step by step process on how to set up the Arduino Keypad and then using it to perform the basic mathematical computations. I decided that I wanted to do this project because I knew I wanted to try something new with programming. The Arduino was an excellent way to to do this because it used a software that I was not accustomed, making me take hours to learn and understand this program(this was the fun part). I wanted to use my creativity to make something that was not made before, and I came up with this. I will be explaining how I set up my keypad using all of material listed below, along with the software and code required to accomplish this. The code will provide the computational properties of this device, and this is a fun way to incorporate a calculator into an Arduino project. I hope you enjoy!! 

Materials Needed:

  • Wires
  • Arduino UNO board
  • USB cable
  • Keypad




A few pairs of wires which come with the Arduino kit. The pins are on both sides. 





An Arduino UNO Board: an open micro controllable board which allows for multiple circuits and more to be connected.





An USB cable to connect the UNO board to the computer.



Lastly, you will need the keypad that is compatible with the UNO and the software with which the program can be run. 

**It is important to understand how the keypad works so here is just a brief overview:
The eight connection coming off of the keypad are connected to the rows and columns of the keypad. The first four(on the left) are associated with the rows and the last four are associated with the columns. To determine what key was pressed, the rows and columns are evaluated separately to pin point the exact location which was pressed and what the key was. 



Set Up: Step by Step:

1) It is important to locate where the pins will go on the UNO board. Connect the eight wires to the top of the board after skipping two from the right. Use the pins 2, 3, 4, 5, 6, 7, 8, and 9. There is a picture below that provides an example of what is looks like.
2)Using the wires that are already connect to the UNO board, connect them to their corresponding position on the Keypad. For example, if the green wire is in pin 9, connect the green wire to the leftmost slot on the keypad. The yellow wire in pin 8 would go connect adjacent to the green. There is a picture below to guide you.

3)Plug the USB into the computer and into a socket on the left side of the UNO board. 

Software Needed:

In order to complete this project, there need to be a few software elements taken care of. First, you have to download the Arduino Software(IDE) on your device. There are many websites that offer step by step processes to accomplish this like the official Arduino website. 

Specifically for a Windows PC:

Specifically for a Mac (OS X):

*There are many other ways to download the software which are not listed above, and they are only one search away*

Once you have downloaded this software, go ahead and create a new page for this project. This can be done by going to file and scanning down until you get to new. Now, after doing this, you are ready to use the code I have provided below to run your new creation.

Code:


#include <Keypad.h>

const int TOTAL_R = 4; //number of rows in the keypad
const int TOTAL_C = 4; //number of columns in the keypad

//layout of the keys on the keypad
char pos[TOTAL_R][TOTAL_C] = {{'1', '2', '3', 'A'},{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

byte rows[TOTAL_R] = {9, 8, 7, 6};// pins for the columns 
byte column[TOTAL_C] = {5, 4, 3, 2}; //pins for the rows

//making the keypad object from the class
Keypad fromKeypad = Keypad( makeKeymap(pos), rows, column, TOTAL_R, TOTAL_C);

//defining variables
String second;
long first;

//establishing a connection between the Arduino and and computer
void setup() {
  Serial.begin(9600); //this line is needed to start connection with the Arduino
  second.reserve(8); //maximum number of digits

//defning a variable in order to print the welcome message
int ask = 1;

//allows the keys to be accounted for as long as the user is pressing the keys
void loop() {
  char input = fromKeypad.getKey();
  //makes it so that the welcome message is only printed once
  if (ask == 1){
    Serial.println("Welcome to this new calculator! To begin just type in your first number and then hit '#':");
    ask = 0;
  }
  if (input) {
    Serial.println(input);
    if (input >= '0' && input <= '9') {     
      second = second + input;              
    } 
    //stores the data in another variable while converting it from a string
    else if (input == '#') {
      Serial.println("second number(when done hit '*'):");
      if (second.length() > 0) {
        first = second.toInt();
        second = "";               
        }
    }
    else if (input == '*'){
      Serial.println("What operation would you like?");
      Serial.println("A = +");
      Serial.println("B = -");
      Serial.println("C = *");
      Serial.println("D = /");
      }
    //code for the calculations based on the operations indicated  
    else if (input == 'A') {
          Serial.print(String(first) + "+" + String(second) + "=");
          Serial.println(first+second.toInt());            
    }
    else if (input == 'B') {
          Serial.print(String(first) + "-" + String(second) + "=");
          Serial.println(first-second.toInt());            
    }
    else if (input == 'C') {
          Serial.print(String(first) + "*" + String(second) + "=");
          Serial.println(first*second.toInt());            
    }
    else if (input == 'D') {
          Serial.print(String(first) + "/" + String(second) + "=");
          Serial.println(first/second.toInt());            
    }
  }
}


In this code, the keys A, B, C, and D are all associated with an operation which is shown to the user. There are also specific keys they have to hit after entering each number and this is visible to the users on the serial monitor. An example of what the user may see on the screen is shown below.

Output on the Serial Monitor:

Thank you so much for being here! Let me know in the comments if you have any questions! 


Comments

Post a Comment

Popular posts from this blog