Skip to content

Commit 22e8e7f

Browse files
Added two new example programs
Two new examples A Simple reaction game and a program to read the analogvalue and display it on the screen like the serial monitor
1 parent ee7416b commit 22e8e7f

2 files changed

Lines changed: 202 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Read an analog sensor attached on the grove connector of the M5 Stick
3+
4+
uses the following library make sure to add it to your libraries before compiling
5+
6+
Universal 8bit Graphics Library (https://github.com/olikraus/u8g2/)
7+
8+
Copyright (c) 2016, olikraus@gmail.com
9+
All rights reserved.
10+
11+
Redistribution and use in source and binary forms, with or without modification,
12+
are permitted provided that the following conditions are met:
13+
14+
* Redistributions of source code must retain the above copyright notice, this list
15+
of conditions and the following disclaimer.
16+
17+
* Redistributions in binary form must reproduce the above copyright notice, this
18+
list of conditions and the following disclaimer in the documentation and/or other
19+
materials provided with the distribution.
20+
21+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
22+
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
23+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
26+
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31+
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33+
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34+
35+
*/
36+
37+
#include <Arduino.h>
38+
#include <U8x8lib.h>
39+
40+
#define SensorPin 13
41+
42+
// U8x8 constructor for your display
43+
U8X8_SH1107_64X128_4W_HW_SPI u8x8(14, /* dc=*/ 27, /* reset=*/ 33);
44+
45+
// Create a U8x8log object
46+
U8X8LOG u8x8log;
47+
48+
// Define the dimension of the U8x8log window
49+
#define U8LOG_WIDTH 16
50+
#define U8LOG_HEIGHT 8
51+
52+
// Allocate static memory for the U8x8log window
53+
uint8_t u8log_buffer[U8LOG_WIDTH*U8LOG_HEIGHT];
54+
55+
void setup() {
56+
// Startup U8x8
57+
u8x8.begin();
58+
59+
// Set a suitable font. This font will be used for U8x8log
60+
u8x8.setFont(u8x8_font_chroma48medium8_r);
61+
62+
// Start U8x8log, connect to U8x8, set the dimension and assign the static memory
63+
u8x8log.begin(u8x8, U8LOG_WIDTH, U8LOG_HEIGHT, u8log_buffer);
64+
65+
// Set the U8x8log redraw mode
66+
u8x8log.setRedrawMode(1); // 0: Update screen with newline, 1: Update screen for every char
67+
Serial.begin(115200);
68+
69+
//Connect the pin set it to output
70+
pinMode(SensorPin, OUTPUT);
71+
ledcSetup(1, 38000, 10);
72+
ledcAttachPin(SensorPin, 1);
73+
}
74+
75+
void loop()
76+
{
77+
analogReadResolution(10);
78+
Serial.println(int(analogRead(SensorPin)));
79+
u8x8log.print(int(analogRead(SensorPin)));
80+
u8x8log.print("\n");
81+
ledcWrite(1, ledcRead(1) ? 0 : 512);
82+
delay(100);
83+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)