Wednesday, February 25, 2015

Keypad Program

Hello!
On Sunday I wrote a program for the display and keypad. We had to try several times to get the program right, and on one try, we installed pulldown resistors, which if a button is not pushed then it sets it to a known state. First we had to write out simple instructions on paper, then we had to translate it into code that the Arduino could understand. Here is a picture of the paper code:

Here is a picture of the wiring up process, well, really two:

                                                                         (Close up)
Here is the code:


// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int keypad_columns[]={0,A0,A1,6,7};
const int keypad_rows[]={8,9,10,13};
 //arrays start with zero
 int col;
 int row;
void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 4);
  Serial.begin(9600);
  // Print a message to the LCD.


  //set columns to output
  pinMode(keypad_columns[1], OUTPUT);
  pinMode(keypad_columns[2], OUTPUT);
  pinMode(keypad_columns[3], OUTPUT);
  pinMode(keypad_columns[4], OUTPUT);
 
//set columns to low so in a know state
  digitalWrite(keypad_columns[1], LOW);
  digitalWrite(keypad_columns[2], LOW);
  digitalWrite(keypad_columns[3], LOW);
  digitalWrite(keypad_columns[4], LOW);

  //set rows to inout
  pinMode(keypad_rows[0], INPUT);
  pinMode(keypad_rows[1], INPUT);
  pinMode(keypad_rows[2], INPUT);
  pinMode(keypad_rows[3], INPUT);


}

void loop() {
  //loop through # of colunms.
  for(col=1; col <= 4; col++) {
    digitalWrite(keypad_columns[col], HIGH);
    for(row=0; row <= 3; row++){
    if (digitalRead(keypad_rows[row])==HIGH){
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("button  ");
      lcd.print((4*row)+col);
      }
    }// end of row
    digitalWrite(keypad_columns[col], LOW);
  }//end of col

}//end of void loop





Over & Out!
Elise

No comments:

Post a Comment