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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146 | // ====================== OLED
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// ===================== touch
#define PIN_GREEN 16
#define N_TOUCH 6
#define THRESHOLD 6
// ====================== OLED
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SCREEN_ADDRESS 0x3C // 0x3D or 0x3C depending on brand
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1, 1700000UL, 1700000UL);
// ===================== touch
int touch_pins[N_TOUCH] = { 3, 4, 2, 27, 1, 26 }; //(A,B,Down,Left,Right,Up)
int touch_values[N_TOUCH] = { 0, 0, 0, 0, 0, 0 };
bool pin_touched_now[N_TOUCH] = { false, false, false, false, false, false };
bool pin_touched_past[N_TOUCH] = { false, false, false, false, false, false };
void update_touch() {
int t;
int t_max = 200;
int p;
for (int i = 0; i < N_TOUCH; i++) {
p = touch_pins[i];
// set to low
pinMode(p, OUTPUT);
digitalWriteFast(p, LOW);
// settle
delayMicroseconds(25);
// enable pull-up
pinMode(p, INPUT_PULLUP);
// measure time to rise
t = 0;
while (!digitalReadFast(p) && t < t_max) {
t++;
}
touch_values[i] = t;
// update state
pin_touched_past[i] = pin_touched_now[i];
pin_touched_now[i] = touch_values[i] > THRESHOLD;
}
}
void print_touch() {
char print_buffer[30];
for (int i = 0; i < N_TOUCH; i++) {
sprintf(print_buffer, "%4d ", touch_values[i]);
Serial.print(print_buffer);
}
Serial.println("");
}
// ====================== OLED
void drawBackground() {
display.drawRect(21, 1, 22, 60, 1);
display.drawCircle(80, 31, 12, 1);
display.drawCircle(110, 17, 12, 1);
display.drawRect(2, 20, 60, 22, 1);
display.setTextColor(1);
display.setTextWrap(false);
}
void setup() {
// initialize Serial port
Serial.begin(115200);
// ===================== touch
// initialize LED
pinMode(PIN_GREEN, OUTPUT);
// HIGH = LED off (they're connected to VCC instead of ground)
digitalWrite(PIN_GREEN, HIGH);
// ====================== OLED
// give the screen some time to power up
delay(50);
// initialize display
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
display.clearDisplay();
// text settings
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
drawBackground();
display.display();
}
void loop() {
// ===================== touch
// update the touch sensors
update_touch();
// ====================== OLED
// clear the buffer
display.clearDisplay();
drawBackground();
if (pin_touched_now[0]) {
display.setCursor(78, 28);
display.print("A");
digitalWrite(PIN_GREEN, LOW);
} else {
digitalWrite(PIN_GREEN, HIGH);
}
// Other possisions
// display.setCursor(29, 7);
// display.print("U");
// display.setCursor(29, 48);
// display.print("D");
// display.setCursor(9, 28);
// display.print("L");
// display.setCursor(49, 28);
// display.print("R");
// display.setCursor(78, 28);
// display.print("A");
// display.setCursor(108, 14);
// display.print("B");
// print values to Serial, for debugging
print_touch();
// slow down the loop to not print too fast (optional)
delay(50);
display.display();
}
|