Combination lock on Arduino

Recently, flipping through a book Willy Sommer, came across an interesting project code lock based on Arduino.

кодовый замок

For this lock using two buttons. To enter you need to press this button a number of times in sequence. Pressing the button is confirmed using a flash of red LED and beep buzzer. If the code is entered with an error, you can clear data input by holding down the 2. That was reset signal blinking red LED and alarm buzzer.
In turn, to the scheme I added the servo. So it works as follows:
After five clicks the button 1 and three clicks on the button 2 seconds on five blue LED lights up and the servo rotates at a specific angle. After five seconds, the servo returns to its original position. To work must be connected heading file Servo.h

lock_arduino

arduino_servo_button_buzer_led_lock



Program code for lock:

#include

Servo myservo;

int val=60;
int val2=-60;
int LED_rot=4;
int LED_gruen=7;
int SW1=2;
int SW2=3;
int Buzzer=9;
int x, y, code1, code2, resetTimer=0;

void setup ()
{
myservo.attach(10);
pinMode(LED_rot, OUTPUT);
pinMode(Buzzer, OUTPUT);

pinMode(SW1, INPUT);
digitalWrite(SW1, HIGH);

pinMode(SW2, INPUT);
digitalWrite(SW2, HIGH);
Clr_Code();

}

void loop()
{

// Button1 = 5 presses
if (!digitalRead(SW1))
{
delay(50);
if (!digitalRead(SW1))
{
Blink();
x++;
if(x==5)
{
code1=true;
}else code1=false;

do{
}while (!digitalRead(SW1));
}
}

// Button2 = 3 presses
if (!digitalRead(SW2))
{
delay(50);
if (!digitalRead(SW2))
{
Blink();
y++;
if(y==3)
{
code2=true;
}else code2=false;

do{
delay(50);
resetTimer++;

if (resetTimer>50)
{
Toggle_Flash();
Clr_Code();
break;
}
}while (!digitalRead(SW2));
resetTimer=0;
}
}

if (code1 == true && code2 == true)
{
digitalWrite(LED_gruen, HIGH);
myservo.write(val);
//delay(15);
delay(5000);

Clr_Code();
digitalWrite(LED_gruen, LOW);
}
else
{
digitalWrite(LED_gruen, LOW);
}
}
void Blink(void)
{
digitalWrite(LED_rot, HIGH);
tone(Buzzer, 500, 150);
delay(200);
digitalWrite(LED_rot, LOW);
}

void Toggle_Flash(void)
{
int tog=0;
for (x=0; x<6; x++)
{
if (tog==0) tog=1; else tog=0;
digitalWrite(LED_rot, tog);
tone(Buzzer, 500, 250);
delay(300);
}
}

void Clr_Code (void)
{
x=0;
y=0;
code1=0;
code2=0;
resetTimer=0;
myservo.write(val2);
delay(1000);
}

Оставить ответ

Обязательные поля помечены*

Этот сайт использует Akismet для борьбы со спамом. Узнайте как обрабатываются ваши данные комментариев.