Pages

Friday, May 23, 2014

Push Button Keypad


The LED would light up only with a secret push button combination. In this case, Press button 1 two times and then press button 2 one time to light up the LED. 





CIRCUIT:

The Circuit for this project is pretty simple, with just 1 LED and 2 push buttons attached to assigned pins in the code (see code below). As you can see I have four buttons on the circuit in the above picture; the two buttons on the right are just to make it more secure. They have nothing to do with the circuit (fake buttons).

You can use this as a keypad by including more push buttons. And, when a certain combination is pressed a task is performed (i.e. lights up an LED, open a lock etc.)

(Of course, you can change the code and set your own password. Contact me if you need any help and I'll be posting the circuit schematic soon. Meanwhile, you can use the above picture for reference)



CODE:


//PUSH BUTTON KEYPAD
//Programmed by, Umar Rao

/* CIRCUIT ---- consists of one LED and two push buttons, with 220 ohm resistors. 
The code clearly tells what pins to connect the LED and the push buttons.
WHAT IT DOES ---- The LED would light up only with a secret push button combination.
Press button 1 two times and then press button 2 one time. This combination would light the LED.
*/

const int buttonPin = 4; // connect push button 1 to pin 4 (with a 220 ohm resistor)
const int buttonPin2 = 5; // connect push button 2 to pin 5 (with a 220 ohm resistor)

const int ledPin = 6;       // connect the led to pin 6 (with a 220 ohm resistor)

// Variables will change:
int buttonPushCounter = 0;// counter for the number of button presses
int buttonPustCounter2 = 0;
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  
  // compare the buttonState to its previous state
  if (buttonState != lastButtonState){
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter += 1;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
  }
  
  lastButtonState = buttonState;

  if (buttonPushCounter == 2) {
    
   buttonState = digitalRead(buttonPin2);
    if (buttonState != lastButtonState) {
    
      if (buttonState == HIGH) {
         digitalWrite(ledPin, HIGH);
      } 
  else {
   digitalWrite(ledPin, LOW);
  }
 }
}
}
  

No comments:

Post a Comment