-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathGame.ino
109 lines (80 loc) · 2.18 KB
/
MathGame.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Math Game
*
* Edwin Lang
* 04.01.2023
*/
#include "Free_Fonts.h" // Include the header file attached to this sketch
#include "equations.h" // Include the 2nd header file attached to this sketch
#include "SPI.h"
#include "TFT_eSPI.h"
#define BUTTON_PIN 16
// counter
boolean trigger = true;
int counter = 0;
String state;
// Use hardware SPI
TFT_eSPI tft = TFT_eSPI();
unsigned long drawTime = 0;
void setup(void) {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT);
tft.begin();
}
void loop() {
// Set text datum to middle centre
tft.setTextDatum(MC_DATUM);
// Set text colour to white with black background
tft.setTextColor(TFT_WHITE, TFT_BLACK);
// rotate screen
tft.setRotation(2);
// Select the font
tft.setFreeFont(FF8);
// on counter change
if (trigger == true){
int index = int(random(0,8));
// Clear screen
tft.fillScreen(TFT_BLACK);
// get one equation out of the array
String equation = equations[0][index];
// format the String
int index1 = equation.indexOf('\n');
int index2 = equation.lastIndexOf('\n');
int index3 = equation.length();
// draw the String
tft.drawString(equation.substring(0,index1), 120, 100, GFXFF);// Print the string name of the font
tft.fillRect(0, 158, 240, 4, TFT_WHITE);
tft.drawString(equation.substring(index2,index3), 120, 220, GFXFF);// Print the string name of the font
// set the flags
state = equations[1][index];
trigger = false;
}
// push button pressed
if (digitalRead(BUTTON_PIN) == LOW){
if (state == "1"){
tft.fillScreen(TFT_GREEN);
}
if (state == "0"){
tft.fillScreen(TFT_BLUE);
}
delay(2000);
trigger = true;
counter = 0;
}
count();
delay(20);
}
// slower counter for focus point
void count(){
counter++;
if (counter == 250){
trigger = true;
counter = 0;
}
}
#ifndef LOAD_GLCD
//ERROR_Please_enable_LOAD_GLCD_in_User_Setup
#endif
#ifndef LOAD_GFXFF
//ERROR_Please_enable_LOAD_GFXFF_in_User_Setup!
#endif