Created
January 5, 2012 21:54
-
-
Save saippuakauppias/1567534 to your computer and use it in GitHub Desktop.
Arduino door opener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Библиотеки для работы с дисплеем | |
#include <LiquidCrystal.h> | |
#include <LiquidCrystalRus.h> | |
// Цифровые PINы - кнопки | |
const int button1 = 10; | |
const int button2 = 9; | |
const int button3 = 8; | |
const int button4 = 7; | |
const int button5 = 6; | |
const int button6 = 0; | |
// Цифровые PINы - дисплей | |
const int lcdRS = 12; | |
const int lcdE = 11; | |
const int lcdDB4 = 5; | |
const int lcdDB5 = 4; | |
const int lcdDB6 = 3; | |
const int lcdDB7 = 2; | |
// Инициализация драйвера дисплея | |
LiquidCrystalRus lcd(lcdRS, lcdE, lcdDB4, lcdDB5, lcdDB6, lcdDB7); | |
// Аналоговый PIN - управление тразистором | |
const int transistor = A0; | |
// Секретный код для открытия двери (транзистора) | |
char password[6] = "32145"; | |
// Буффер для ввода кода и счётчик введённых символов | |
char inputpassword[6] = "00000"; | |
int counter = 0; | |
// Нажатая кнопка | |
char pressedbtn = '0'; | |
// Время после нажатия на кнопку | |
int pressedbtnsecs = 0; | |
// Установка значений при включении | |
void setup() | |
{ | |
// Устанавливаем кнопки для ввода | |
pinMode(button1, INPUT); | |
pinMode(button2, INPUT); | |
pinMode(button3, INPUT); | |
pinMode(button4, INPUT); | |
pinMode(button5, INPUT); | |
pinMode(button6, INPUT); | |
// Включение внутренних подтягивающих резисторов | |
digitalWrite(button1, HIGH); | |
digitalWrite(button2, HIGH); | |
digitalWrite(button3, HIGH); | |
digitalWrite(button4, HIGH); | |
digitalWrite(button5, HIGH); | |
digitalWrite(button6, HIGH); | |
// Инициализация дисплея | |
lcd.begin(16, 2); | |
// Вывод фразы на первой строке | |
lcd.print("Введите код:"); | |
// Устанавливаем ввод на 2 строку дисплея | |
lcd.setCursor(0, 1); | |
// Обнулить вводимый пароль | |
pwdtonull(); | |
// Чтобы по затвору-стоку не проходил ток при включении | |
analogWrite(transistor, 0); | |
} | |
// Бесконечный цикл | |
void loop() | |
{ | |
// если больше 8 секунд назад была нажата кнопка | |
if ((pressedbtnsecs > 1) && ((seconds() - pressedbtnsecs) > 8)) | |
{ | |
// стираем введённые значения и обнуляем счётчик | |
pwdtonull(); | |
writeLcdPassword(); | |
pressedbtnsecs = 0; | |
} | |
// Проверяем пароль | |
if (checkPassword()) | |
{ | |
// выводим текст на дисплей | |
lcd.setCursor(0,1); | |
lcd.print("Дверь открыта!"); | |
// если сходится, то открываем транзистор | |
analogWrite(transistor, 1023); | |
delay(500); | |
analogWrite(transistor, 0); | |
// если сходится, то открываем транзистор | |
analogWrite(transistor, 1023); | |
delay(1000); | |
analogWrite(transistor, 0); | |
// потом зануляем пароль | |
pwdtonull(); | |
// сохраняем сколько секунд прошло после запуска Arduino | |
pressedbtnsecs = seconds(); | |
} | |
// Смотрим какая кнопка нажата | |
pressedbtn = whatButton(); | |
if (pressedbtn > '0') | |
{ | |
// Чтобы не выйти за границу массива пароля | |
if (counter >= 5) | |
{ | |
counter = 0; | |
// и заодно стираем все символы, чтоб на дисплее не оставались старые значения | |
for(int i=0; i < 5; i++) | |
{ | |
inputpassword[i] = '0'; | |
} | |
} | |
// сохраняем в массив введённых символов нажатую кнопку | |
inputpassword[counter] = pressedbtn; | |
counter++; | |
// Выводим на дисплей | |
writeLcdPassword(); | |
delay(300); | |
// сохраняем сколько секунд прошло после запуска Arduino | |
pressedbtnsecs = seconds(); | |
} | |
// если нажата кнопка "стереть" - стираем | |
if ((pressedbtn == '-') && (counter > 0)) | |
{ | |
counter--; | |
inputpassword[counter] = '0'; | |
// чтобы не выйти за границу массива-пароля | |
if (counter < 0) | |
{ | |
counter = 0; | |
} | |
// очищаем введённые символы на дисплее | |
writeLcdPassword(); | |
delay(300); | |
// сохраняем сколько секунд прошло после запуска Arduino | |
pressedbtnsecs = seconds(); | |
} | |
} | |
// Функция для определения какая кнопка нажата | |
char whatButton() | |
{ | |
if (digitalRead(button1) == LOW) | |
return '1'; | |
if (digitalRead(button2) == LOW) | |
return '2'; | |
if (digitalRead(button3) == LOW) | |
return '3'; | |
if (digitalRead(button4) == LOW) | |
return '4'; | |
if (digitalRead(button5) == LOW) | |
return '5'; | |
if (digitalRead(button6) == LOW) | |
return '-'; // кнопка "стереть" | |
return '0'; // ничего не нажато | |
} | |
// Функция для проверки пароля | |
boolean checkPassword() | |
{ | |
for (int i = 0; i < 5; i++) | |
{ | |
if (password[i] != inputpassword[i]) | |
return false; // пароль не сошёлся | |
} | |
return true; // пароли одинаковы | |
} | |
// Функция для обнуления вводимого кода | |
void pwdtonull() | |
{ | |
counter = 0; | |
for(int i = 0; i < 5; i++) | |
{ | |
inputpassword[i] = '0'; | |
} | |
} | |
// Вывод введённого кода на дисплей | |
void writeLcdPassword() | |
{ | |
for (int i = 0; i < 5; i++) | |
{ | |
if (inputpassword[i] == '0') | |
inputpassword[i] = ' '; // нули заменяем на пустоту | |
} | |
// Затираем всё что было до этого на дисплее | |
lcd.setCursor(0,1); | |
lcd.print(" "); | |
// И выводим введённые символы | |
lcd.setCursor(0,1); | |
lcd.print(inputpassword); | |
} | |
// Cекунды после запуска Arduino | |
int seconds() | |
{ | |
return millis() / 1000; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment