本文为原创,转载请注明原文链接及作者。有错误欢迎指出!

目录

一、查看管脚的脉冲计数结果

二、阈值选取

三、给个中断试试


esp32 有touch0-9 一共10个,这里不测试8、9。原因见下表

 其他8个touch分别为:

这里不在赘述硬件设计和连接方式,看手册就可以了。

下面讲一下测试,阈值选取。测试板esp32_Devkitc_v4

、查看管脚的脉冲计数结果

 不同的硬件触摸pad设计回有不同地方计数值,为了方便阈值选取,应当查看计数结果。不废话直接上代码。

// ESP32 Touch Test
// Just test touch pin

void setup()
{
  Serial.begin(115200);
  delay(1000); // give me time to bring up serial monitor
  Serial.println("ESP32 Touch Test");
}

void loop()
{
  
  Serial.println("T0:"+(String)(touchRead(T0)) +" "+"T1:"+(String)(touchRead(T1)) +" "+"T2:"+(String)(touchRead(T2)) +" "+"T3:"+ (String)(touchRead(T3)) +" "+ "T4:"+(String)(touchRead(T4)) +" "+ "T5:"+(String)(touchRead(T5)) +" "+ "T6:"+(String)(touchRead(T6)) +" "+ "T7:"+(String)(touchRead(T7)));  // get value

  delay(1000);
}

使用串口监视器绘图

两个问题:

1.T1长时间显示为1,这显然不对,更换了一块esp32仍然是同样的结果,猜测T1可能有什么问题?那好了,以后我就不用T1就完事了。

2.触摸pad上没有任何操作,但是T0数据有突变,更换连接的pad也没用,这个要注意一下,因为我吧T1和T0都去掉以后,只看T2-7的数据时T2 也出了同样的问题。这就整蒙我了,难度说T0其实也没问题,只是因为第一个调用它所以会出这种尖刺?

那行吧,我先调一下T0 或者T2的值,然后再看T2-7,如果没有尖刺就这样了,以后写代码就先调一下再用咯。

先调T0,T2还有一个尖刺,并且直接到0了!!!这啥情况

 先调一遍T2,再调时候再看数据,还是有尖刺,太难了。那我省掉T2直接T3-7再看

 T3-7,绝了

好了,这情况也是绝了,想不出因为啥,以后注意就是了。可能时用touchRead的锅? 但是用touchAttachInterrupt可能没问题吧?一会注意看一下。

但T4还是有点毛糙,不过问题不大,一个数的波动不能算啥。中断里设置阈值的时候再看结果有没有误触发吧,注意就行了。

二、阈值选取

结合当前硬件,看看按和不按下有多大区别。T0-7,除了T1不看。

T0后来的尖刺不管,阈值上再40几到15以上应该就可以了,不过为了分辨的更好,选个大概的中间值吧,统一用29好了,小于29就认为时按下了,否则没按。 不同的pad设计值会不同,方法就是上边这杨。

三、给个中断试试

/*
This is un example howto use Touch Intrrerupts
The bigger the threshold, the more sensible is the touch
5pin is a led, no matter which pad is touch, it turns on about 1 second. 
*/

int threshold = 29;
bool touch0detected = false;
bool touch2detected = false;
bool touch3detected = false;
bool touch4detected = false;
bool touch5detected = false;
bool touch6detected = false;
bool touch7detected = false;



void gotTouch0(){
 touch0detected = true;
 digitalWrite(5,LOW);
}

void gotTouch2(){
 touch2detected = true;
 digitalWrite(5,LOW);
}

void gotTouch3(){
 touch3detected = true;
 digitalWrite(5,LOW);
}
void gotTouch4(){
 touch4detected = true;
 digitalWrite(5,LOW);
}
void gotTouch5(){
 touch5detected = true;
 digitalWrite(5,LOW);
}

void gotTouch6(){
 touch6detected = true;
 digitalWrite(5,LOW);
}
void gotTouch7(){
 touch7detected = true;
 digitalWrite(5,LOW);
}

void setup() {
  pinMode(5,OUTPUT);//5pin is a led
  digitalWrite(5,HIGH);
  Serial.begin(115200);
  delay(1000); // give me time to bring up serial monitor
  Serial.println("ESP32 Touch Interrupt Test");
  touchAttachInterrupt(T0, gotTouch0, threshold);
  touchAttachInterrupt(T2, gotTouch2, threshold);
  touchAttachInterrupt(T3, gotTouch3, threshold);
  touchAttachInterrupt(T4, gotTouch4, threshold);
  touchAttachInterrupt(T5, gotTouch5, threshold);
  touchAttachInterrupt(T6, gotTouch6, threshold);
  touchAttachInterrupt(T7, gotTouch7, threshold);
}

void loop(){
  if(touch0detected){
    delay(1000);
    touch0detected = false;
    Serial.println("Touch 0 detected");
    digitalWrite(5,HIGH);
  }
  if(touch2detected){
    delay(1000);
    touch2detected = false;
    Serial.println("Touch 2 detected");
    digitalWrite(5,HIGH);
  }
  if(touch3detected){
    delay(1000);
    touch3detected = false;
    Serial.println("Touch 3 detected");
    digitalWrite(5,HIGH);
  }
  if(touch4detected){
    delay(1000);
    touch4detected = false;
    Serial.println("Touch 4 detected");
    digitalWrite(5,HIGH);
  }
  if(touch5detected){
    delay(1000);
    touch5detected = false;
    Serial.println("Touch 5 detected");
    digitalWrite(5,HIGH);
  }
  if(touch6detected){
    delay(1000);
    touch6detected = false;
    Serial.println("Touch 6 detected");
    digitalWrite(5,HIGH);
  }
  if(touch7detected){
    delay(1000);
    touch7detected = false;
    Serial.println("Touch 7 detected");
    digitalWrite(5,HIGH);
  }
}

led就不给你们看了,看看串口的输出好了,

 没问题,多等会,看看会不会有上边那样的数据,导致误触发。结果没有。。。说明是用touchRead的锅,正常应该是没有尖刺的。所处除了touch1,其他几个都可以正常用。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐