How to build and control an LED on an Arduino using a keypad membrane:
by J.B. WylzanROW1 - Arduino pin 4
ROW2 - Arduino pin 5
ROW3 - Arduino pin 6
ROW4 - Arduino pin 7
COL1 - Arduino pin 8
COL2 - Arduino pin 9
COL3 - Arduino pin 10
*Note: These rows and columns can be changed anytime.
Block Diagram:
Hardware:
1 Keypad; 4x3 Matrix
4 leds
4 resistors
connecting wires
breadboard
Arduino R3 UNO board
Code # 18:
/*
iHacklab Leds Switch Keypad
powered by Arduino
modified by Lawsinium
Note: Keypad Library for Arduino
Authored by Mark Stanley, Alexander Brevig
Downloaded from http://playground.arduino.cc/Code/Keypad
Turns On and Off leds using a keypad
The example code is public domain.
*/
#include <Keypad.h>
const byte ROWS = 4; // Keypad has four rows
const byte COLS = 3; // Keypad has three columns
char key = 0; // Initializes a variable
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
// Connect keypad’s pins to Arduino pins.
byte rowPins[ROWS] = { 4, 5, 6, 7 };
byte colPins[COLS] = { 8, 9, 10 };
// Keypad syntax
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
//setup the pins' inputs and outputs void setup() {
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}
void loop()
{
key = keypad.getKey(); // get key
pressKey('1', '2', 2); // On = '1', Off = '2', pin = 2
pressKey('3', '4', 3); // On = '1', Off = '2', pin = 3
pressKey('5', '6', 11); // On = '1', Off = '2', pin = 11
pressKey('7', '8', 12); // On = '1', Off = '2', pin = 12
}
unsigned int pressKey(char on, char off, int output) {
if(key) {
if (key == on) {
digitalWrite(output, HIGH);
}
if (key == off) {
digitalWrite(output, LOW);
}
}
}
// end of the sketch
Challenge: Use Keypad to light an LED using a numerical password. If the password is invalid a red led lights up. If the password is valid, a green led lights up.
Procedure:
1. Build the prototype as shown above
2. Run the Arduino Interface
3. Select File > New
4. Copy Code #18 above
5. Paste Code #18
6. Click File > Save
7. Click Verify
8. Click Upload
9. Press any key to turn On and Off an Led.
Disclaimer: We shall not be liable for any loss or damage of whatever nature - direct, indirect, consequential, or otherwise - which may arise as a result of your use of any information on this website. However, if you are interested in using any of the projects for personal or educational purposes, please inform the author by email.
Public Domain Notice: Copyright (c) 2000. All rights reserved. This article is part of a book entitled iHackRobot. Copies are welcome to be shared or distributed publicly as long proper citations are observed. Please cite as follows: Biotronics: The Silver Species, Joey Lawsin, 1988, USA.
==================================================================
Patent Pending. 2000 © ®
A L.A.W.S.I.N. Educational Production