Homework #2

Screen Shot 2014-09-26 at 9.07.17 AM

For the combination lock it was used two push buttons. 

It starts with the red light on. If you push only one button – doesn’t matter the order – nothing changes. If the two buttons are pressed at the same time, the red light will turn off, green light will turn on and a piezo buzzer will be triggered showing that you unlocked the system.

completed with Camila Nakamura

CODE:
const int button1Pin = 2; // pushbutton 1 pin
const int button2Pin = 3; // pushbutton 2 pin
const int ledPin = 13; // LED PIN
const int ledPin2 = 12; // LED pin
const int buzzPin = 7;
void setup()
{
// Set up the pushbutton pins to be an input:
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);

// Set up the LED pin to be an output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT);

//Set up the PIEZZO pin to be an output
pinMode(buzzPin, OUTPUT);

}
void loop()
{
int button1State, button2State; // variables to hold the pushbutton states
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);

// Asks for the two pushbotton to be pressed at the same time

if (((button1State == LOW) && (button2State == LOW)))

{
digitalWrite(ledPin, LOW); // turn the green LED off
digitalWrite(ledPin2, HIGH); // turn the LED on
tone(buzzPin, 16, 1000); // piezo buzzes
delay(1000);
}

else

{
digitalWrite(ledPin, HIGH); // turn the LED on
digitalWrite(ledPin2, LOW); // turn the LED off
}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s