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