When using the script from the playground nothing works, except for when I start doing stuff with the resistors. when using this code:
/*
* Keypad Matrix
* Copyright 2009 by Ilya Brutman <ilya@splitreaction.com>
* 8/16/2009
*
* Scans a keypad and outputs which button was pressed. Also lights an LED everytime a button is pressed.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
int cols[] = {6,8,4}; //set the pins that are connected to the columns on the keypad (order important)
int rows[] = {7,2,3,5}; //set the pins that are connected to the rows on the keypad (order important)
char labels[4][3] ={{'1','2','3'},{'4','5','6'},{'7','8','9'},{'*','0','#'}}; //define a label for each button
int ledPin = 13; // choose the pin for the LED
char temp;
void setup() {
Serial.begin(9600);
Serial.println("ready for action");
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(cols[0], INPUT); // declare column 0 pin as input
pinMode(cols[1], INPUT); // declare column 1 pin as input
pinMode(cols[2], INPUT); // declare column 2 pin as input
pinMode(rows[0], OUTPUT); // declare row 0 pin as output
pinMode(rows[1], OUTPUT); // declare row 1 pin as output
pinMode(rows[2], OUTPUT); // declare row 2 pin as output
pinMode(rows[3], OUTPUT); // declare row 3 pin as output
digitalWrite(rows[0], HIGH);
digitalWrite(rows[1], HIGH);
digitalWrite(rows[2], HIGH);
digitalWrite(rows[3], HIGH);
digitalWrite(cols[0], HIGH); //turn on pull up resisitor
digitalWrite(cols[1], HIGH); //turn on pull up resisitor
digitalWrite(cols[2], HIGH); //turn on pull up resisitor
}//setup
void loop(){
temp = getButtonPress(); //check which button is currently pressed
if (temp > 0){ //if a button is pressed
Serial.print(temp); //print it out
Serial.print(" ");
}
}//loop
char getButtonPress(){
int val = 0;
for(int j = 0; j<=2; j++){
for(int i = 0; i<=3; i++){
digitalWrite(rows[0], HIGH); //set all the rows to HIGH
digitalWrite(rows[1], HIGH);
digitalWrite(rows[2], HIGH);
digitalWrite(rows[3], HIGH);
digitalWrite(rows[i], LOW); //set the current row we are looking at to LOW
val = digitalRead(cols[j]); // read state of button at ROW i, Column j
if (val == HIGH) { // check if the input is HIGH (Button not pressed)
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
while(val == LOW){ // wait for button to be released
delay(1);
val = digitalRead(cols[j]); // read input value
}//while
return(labels[i][j]); //return the label of the button pressed
}//if
}//for i
}//for j
return 0; //return 0 if no button was pressed
}//getButtonPress
The serial monitor seems to spit out some random 8’s.
Can someone provide me with clear intructions prefeably using the keypad library and making the keypad with code lik:
[1][2][3]
Etc