Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Подключаем библиотеку.
- #include <LiquidCrystal.h>
- // Пины кнопок.
- #define KEY_COUNT 2
- #define LEFT_BUT 2
- #define RIGHT_BUT 3
- byte prevOneState = HIGH;
- byte prevTwoState = HIGH;
- int leftKlickCount = 0;
- int rightKlickCount = 0;
- // Массив значений для левой и правой кнопки.
- int score[2] = { 7, 8 };
- char symb = '*';
- // Инициализируем пины, по которым будет производиться обмен данными с дисплеем
- LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
- void setup(){
- pinMode(8, OUTPUT);
- pinMode(2, INPUT_PULLUP);
- pinMode(3, INPUT_PULLUP);
- // Выставляем количество колонок и строк
- lcd.begin(16, 2);
- lcd.setCursor(7, 0);
- lcd.print(symb);
- lcd.print(symb);
- }
- void loop(){
- byte oneState = digitalRead(LEFT_BUT);
- byte twoState = digitalRead(RIGHT_BUT);
- if (oneState == LOW && prevOneState == HIGH) {
- rightKlickCount++;
- if(rightKlickCount == 4){
- rightKlickCount = 0;
- score[0] = constrain(score[0] - 1, 0, 7);
- score[1] = constrain(score[1] - 1, 8, 15);
- SetCursor(0);
- if(score[0] == 0){
- lcd.setCursor(0, 1);
- lcd.print("Left Win!");
- }
- }
- }
- if (twoState == LOW && prevTwoState == HIGH){
- leftKlickCount++;
- if(leftKlickCount == 4){
- leftKlickCount = 0;
- score[1] = constrain(score[1] + 1, 8, 15);
- score[0] = constrain(score[0] + 1, 0, 7);
- SetCursor(1);
- if(score[1] == 15){
- lcd.setCursor(6, 1);
- lcd.print("Right Win!");
- }
- }
- }
- // При чей-либо победе бесконечно мигают светодиоды и программа "замораживается".
- if(score[0] == 0 || score[1] == 15)
- while(true){
- digitalWrite(8, true);
- delay(250);
- digitalWrite(8, false);
- delay(250);
- }
- prevOneState = oneState;
- prevTwoState = twoState;
- }
- // Функция перенесения символа.
- void SetCursor(int side){
- if(side == 0){
- lcd.setCursor(score[0], 0);
- lcd.print(symb);
- lcd.setCursor(score[1] + 1, 0);
- lcd.print(" ");
- }
- else{
- lcd.setCursor(score[1], 0);
- lcd.print(symb);
- lcd.setCursor(score[0] - 1, 0);
- lcd.print(" ");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement