Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////////////////////////////
- // Author: RSP @KMUTNB
- // Date: 23-Oct-2013
- // Target Board: Arduino Uno (ATmega328P, 5V, 16MHz)
- // Arduino IDE: version 1.0.5
- // This sketch demonstrates how to do a keypad scan (a 4x4 keypad used)
- // (without use of the Keypad library).
- // uncomment this line if you use Arduino Leonardo.
- //#define USE_LEONARDO
- // constant values
- const byte BUZZER_PIN = 13;
- const byte ROWS = 4;
- const byte COLS = 4;
- char keys[ROWS][COLS] = {
- {'1','2','3','A'},
- {'4','5','6','B'},
- {'7','8','9','C'},
- {'*','0','#','D'}
- };
- byte rowPins[ROWS] = {2,3,4,5}; // connect D2,D3,D4,D5 to Rows 1-4 (the left four pins)
- byte colPins[COLS] = {8,9,10,11}; // connect D8,D9,D10,D11 to Column 1-4 (the right four pins)
- void setup(){
- Serial.begin( 115200 );
- for (int i=0; i < ROWS; i++) {
- pinMode( rowPins[i], OUTPUT );
- }
- for (int i=0; i < COLS; i++) {
- pinMode( colPins[i], INPUT );
- digitalWrite( colPins[i], HIGH ); // enable internal pull-up
- }
- pinMode( BUZZER_PIN, OUTPUT );
- digitalWrite( BUZZER_PIN, LOW );
- #ifdef USE_LEONARDO
- delay(1000);
- Keyboard.begin();
- #endif
- }
- char getKey( ) {
- char key_pressed = 0;
- for (int j=0; j < ROWS; j++) { // scan the j-th row (j=0,1,2,3)
- for (int i=0; i < ROWS; i++) {
- // output HIGH to all rows, except the j-th row
- digitalWrite( rowPins[i], (i==j) ? LOW : HIGH );
- }
- for (int i=0; i < COLS; i++) {
- if ( digitalRead( colPins[i] ) == LOW ) { // Button at (R,C)=(j,i) is pressed
- // wait until the button is released.
- while ( digitalRead( colPins[i] ) != HIGH ) ; // blocking
- key_pressed = keys[j][i]; // get the associated key for that button
- break;
- }
- }
- digitalWrite( rowPins[j], HIGH );
- if ( key_pressed != 0 ) {
- return key_pressed;
- }
- }
- return 0; // no key pressed
- }
- void loop(){
- char key = getKey();
- if ( key != 0 ) {
- digitalWrite( BUZZER_PIN, HIGH );
- #ifdef USE_LEONARDO
- Keyboard.press( key );
- delayMicroseconds( 10 );
- Keyboard.releaseAll();
- #endif
- Serial.println( key );
- delay(50);
- digitalWrite( BUZZER_PIN, LOW);
- }
- delay(10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement