ReactionGame.ino 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Simple reaction game for M5 Stick
  3. Requires the library below
  4. Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
  5. Copyright (c) 2016, olikraus@gmail.com
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. * Redistributions of source code must retain the above copyright notice, this list
  10. of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this
  12. list of conditions and the following disclaimer in the documentation and/or other
  13. materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  15. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  16. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  17. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  26. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #include <Arduino.h>
  29. #include <U8x8lib.h>
  30. #define BtnPin 35
  31. #define BuzzerPin 26
  32. int pos = 0; //balls position
  33. int score = 0; //score counter
  34. int pace = 100; //the speed of the game
  35. U8X8_SH1107_64X128_4W_HW_SPI u8x8(14, /* dc=*/ 27, /* reset=*/ 33);
  36. void setup() {
  37. u8x8.begin();
  38. pinMode(BtnPin, INPUT_PULLUP);
  39. pinMode(BuzzerPin, OUTPUT);
  40. u8x8.setFont(u8x8_font_chroma48medium8_r);
  41. //set buzzer low at start
  42. digitalWrite(BuzzerPin, LOW);
  43. u8x8.drawString(0,0,"Catch");
  44. u8x8.drawString(0,1,"the ball");
  45. u8x8.drawString(0,2,"in the");
  46. u8x8.drawString(0,3,"middle");
  47. u8x8.drawString(0,4,"to score");
  48. delay(2000);
  49. }
  50. void lines(){
  51. //draw the central columns
  52. uint8_t Lline[16] = { 0xf0, 0x0f, 1, 0xf0, 1, 0x0f, 1, 0xf0,1,0x0f,1,1,1,1,0xf0,1};
  53. uint8_t Rline[16] = { 0xf0, 0x0f, 1, 0xf0, 1, 0x0f, 1, 0xf0,1,0x0f,1,1,1,1,0xf0,1};
  54. u8x8.drawTile(0, 7, 1, Lline);
  55. u8x8.drawTile(7, 7, 1, Rline);
  56. u8x8.setCursor(0, 0);
  57. u8x8.print(score);
  58. }
  59. void scoreCheck(){
  60. if(digitalRead(BtnPin) == 0 && pos == 7){
  61. score = score + 1;
  62. }
  63. //bounce sound
  64. if (pos == 0 || pos == 15) {
  65. for(int f=0;f<100;f++){
  66. digitalWrite(BuzzerPin,HIGH);
  67. delay(1);
  68. digitalWrite(BuzzerPin,LOW);
  69. delay(1);
  70. }
  71. }
  72. }
  73. void loop()
  74. {
  75. lines();
  76. scoreCheck();
  77. for(pos=0;pos<=15;pos++){
  78. uint8_t square[16] = { 255, 255, 255,255, 255, 255, 255, 255};
  79. u8x8.drawTile(4, pos, 2, square);
  80. lines();
  81. delay(pace);
  82. scoreCheck();
  83. u8x8.clearDisplay();
  84. }
  85. for(pos=15;pos>=0;pos--){
  86. uint8_t square[16] = { 255, 255, 255,255, 255, 255, 255, 255};
  87. u8x8.drawTile(4, pos, 2, square);
  88. lines();
  89. delay(pace);
  90. scoreCheck();
  91. u8x8.clearDisplay();
  92. }
  93. //increase speed on each pass
  94. pace = pace - 5;
  95. }