Advertisement
RealHero

Arduino 4

Jun 5th, 2021
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. // Подключаем библиотеку.
  2.  
  3. #include <LiquidCrystal.h>
  4.  
  5. // Пины кнопок.
  6.  
  7. #define KEY_COUNT 2
  8.  
  9. #define LEFT_BUT 2
  10.  
  11. #define RIGHT_BUT 3
  12.  
  13. byte prevOneState = HIGH;
  14.  
  15. byte prevTwoState = HIGH;
  16.  
  17. int leftKlickCount = 0;
  18.  
  19. int rightKlickCount = 0;
  20.  
  21. // Массив значений для левой и правой кнопки.
  22.  
  23. int score[2] = { 7, 8 };
  24.  
  25. char symb = '*';
  26.  
  27. // Инициализируем пины, по которым будет производиться обмен данными с дисплеем
  28.  
  29. LiquidCrystal lcd(4, 6, 10, 11, 12, 13);
  30.  
  31. void setup(){
  32.  
  33. pinMode(8, OUTPUT);
  34.  
  35. pinMode(2, INPUT_PULLUP);
  36.  
  37. pinMode(3, INPUT_PULLUP);
  38.  
  39. // Выставляем количество колонок и строк
  40.  
  41. lcd.begin(16, 2);
  42.  
  43. lcd.setCursor(7, 0);
  44.  
  45. lcd.print(symb);
  46.  
  47. lcd.print(symb);
  48.  
  49. }
  50.  
  51. void loop(){
  52.  
  53. byte oneState = digitalRead(LEFT_BUT);
  54.  
  55. byte twoState = digitalRead(RIGHT_BUT);
  56.  
  57. if (oneState == LOW && prevOneState == HIGH) {
  58.  
  59. rightKlickCount++;
  60.  
  61. if(rightKlickCount == 4){
  62.  
  63. rightKlickCount = 0;
  64.  
  65. score[0] = constrain(score[0] - 1, 0, 7);
  66.  
  67. score[1] = constrain(score[1] - 1, 8, 15);
  68.  
  69. SetCursor(0);
  70.  
  71. if(score[0] == 0){
  72.  
  73. lcd.setCursor(0, 1);
  74.  
  75. lcd.print("Left Win!");
  76.  
  77. }
  78.  
  79. }
  80.  
  81. }
  82.  
  83. if (twoState == LOW && prevTwoState == HIGH){
  84.  
  85. leftKlickCount++;
  86.  
  87. if(leftKlickCount == 4){
  88.  
  89. leftKlickCount = 0;
  90.  
  91. score[1] = constrain(score[1] + 1, 8, 15);
  92.  
  93. score[0] = constrain(score[0] + 1, 0, 7);
  94.  
  95. SetCursor(1);
  96.  
  97. if(score[1] == 15){
  98.  
  99. lcd.setCursor(6, 1);
  100.  
  101. lcd.print("Right Win!");
  102.  
  103. }
  104.  
  105. }
  106.  
  107. }
  108.  
  109. // При чей-либо победе бесконечно мигают светодиоды и программа "замораживается".
  110.  
  111. if(score[0] == 0 || score[1] == 15)
  112.  
  113. while(true){
  114.  
  115. digitalWrite(8, true);
  116.  
  117. delay(250);
  118.  
  119. digitalWrite(8, false);
  120.  
  121. delay(250);
  122.  
  123. }
  124.  
  125. prevOneState = oneState;
  126.   prevTwoState = twoState;
  127.  
  128. }
  129.  
  130. // Функция перенесения символа.
  131.  
  132. void SetCursor(int side){
  133.  
  134. if(side == 0){
  135.  
  136. lcd.setCursor(score[0], 0);
  137.  
  138. lcd.print(symb);
  139.  
  140. lcd.setCursor(score[1] + 1, 0);
  141.  
  142. lcd.print(" ");
  143.  
  144. }
  145.  
  146. else{
  147.  
  148. lcd.setCursor(score[1], 0);
  149.  
  150. lcd.print(symb);
  151.  
  152. lcd.setCursor(score[0] - 1, 0);
  153.  
  154. lcd.print(" ");
  155.  
  156. }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement