touchpad

  1. 示例代码

示例-touchpad.ino
 1#include <Arduino.h>
 2#include <driver/touch_pad.h>
 3#include <esp32-hal-touch.h>
 4
 5bool p_touched = false;
 6bool y_touched = false;
 7bool t_touched = false;
 8bool h_touched = false;
 9bool o_touched = false;
10bool n_touched = false;
11void gotTouch_p(){
12  p_touched = true;
13}
14void gotTouch_y(){
15  y_touched = true;
16}
17void gotTouch_t(){
18  t_touched = true;
19}
20void gotTouch_h(){
21  h_touched = true;
22}
23void gotTouch_o(){
24  o_touched = true;
25}
26void gotTouch_n(){
27  n_touched = true;
28}
29void setup() {
30  delay(1000);
31  Serial.begin(9600);
32  touchAttachInterrupt(P, gotTouch_p, 4500);
33  touchAttachInterrupt(Y, gotTouch_y, 4500);
34  touchAttachInterrupt(T, gotTouch_t, 4500);
35  touchAttachInterrupt(H, gotTouch_h, 4500);
36  touchAttachInterrupt(O, gotTouch_o, 4500);
37  touchAttachInterrupt(N, gotTouch_n, 4500);
38}
39void loop() {
40  // Serial.println(touchRead(9)); 
41  if(p_touched){
42    Serial.println("pad P touched.");
43    p_touched = false;
44  }
45  if(y_touched){
46    Serial.println("pad Y touched.");
47    y_touched = false;
48  }
49  if(t_touched){
50    Serial.println("pad T touched.");
51    t_touched = false;
52  }
53  if(h_touched){
54    Serial.println("pad H touched.");
55    h_touched = false;
56  }
57  if(o_touched){
58    Serial.println("pad O touched.");
59    o_touched = false;
60  }
61  if(n_touched){
62    Serial.println("pad N touched.");
63    n_touched = false;
64  }
65  delay(200);
66}

  1. 运行效果

触摸按键时,串口打印对应触摸按键信息。