极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 35859|回复: 7

关于Arduino readCapacitivePin()函数,读取电容值。

[复制链接]
发表于 2015-12-1 16:50:11 | 显示全部楼层 |阅读模式
最近在搞一点小玩意,利用Arduino做电容按键的处理,按键用铜箔(2cm*2cm)贴在不透明的亚克力背面,焊接上导线连接到Arduino。
但出现的问题是,直接接触铜箔,数值可以大于2,但是经过亚克力以后,手触摸亚克力,数值只能2。增加到3个按键后,其他两个按键的值一直是2,第一个按键触摸后变为2,偶尔还会受到干扰,自己跳变。我知道直接触摸导线或铜箔数值会增大到十几,但还是想隔着亚克力进行操作。
希望高手们指教一下!
先附上参考代码:
[pre lang="arduino" line="1" file="Capacity"]int ledPin = 13;
int capval0,capval1,capval2;
void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Touch senser");
}
void loop ()
{
  digitalWrite(ledPin,LOW);
  capval0 = readCapacitivePin(8);
  capval1 = readCapacitivePin(9);
  capval2 = readCapacitivePin(10);
  Serial.println("Touch senser");
  Serial.println(capval0, DEC);
  Serial.println(capval1, DEC);
  Serial.println(capval2, DEC);
  if (capval0 > 1) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    delay(10);
  }
  delay(300);
}

uint8_t readCapacitivePin(int pinToMeasure) {
  // Variables used to translate from Arduino to AVR pin naming
  volatile uint8_t* port;
  volatile uint8_t* ddr;
  volatile uint8_t* pin;
  // Here we translate the input pin number from
  // Arduino pin number to the AVR PORT, PIN, DDR,
  // and which bit of those registers we care about.
  byte bitmask;
  port = portOutputRegister(digitalPinToPort(pinToMeasure));
  ddr = portModeRegister(digitalPinToPort(pinToMeasure));
  bitmask = digitalPinToBitMask(pinToMeasure);
  pin = portInputRegister(digitalPinToPort(pinToMeasure));
  // Discharge the pin first by setting it low and output
  *port &= ~(bitmask);
  *ddr |= bitmask;
  //delay(1);
  // Make the pin an input with the internal pull-up on
  *ddr &= ~(bitmask);
  *port |= bitmask;
  
  // Now see how long the pin to get pulled up. This manual unrolling of the loop
  // decreases the number of hardware cycles between each read of the pin,
  // thus increasing sensitivity.
  uint8_t cycles = 17;
  if (*pin & bitmask) { cycles = 0;}
  else if (*pin & bitmask) { cycles = 1;}
  else if (*pin & bitmask) { cycles = 2;}
  else if (*pin & bitmask) { cycles = 3;}
  else if (*pin & bitmask) { cycles = 4;}
  else if (*pin & bitmask) { cycles = 5;}
  else if (*pin & bitmask) { cycles = 6;}
  else if (*pin & bitmask) { cycles = 7;}
  else if (*pin & bitmask) { cycles = 8;}
  else if (*pin & bitmask) { cycles = 9;}
  else if (*pin & bitmask) { cycles = 10;}
  else if (*pin & bitmask) { cycles = 11;}
  else if (*pin & bitmask) { cycles = 12;}
  else if (*pin & bitmask) { cycles = 13;}
  else if (*pin & bitmask) { cycles = 14;}
  else if (*pin & bitmask) { cycles = 15;}
  else if (*pin & bitmask) { cycles = 16;}
  
  // Discharge the pin again by setting it low and output
  // It's important to leave the pins low if you want to
  // be able to touch more than 1 sensor at a time - if
  // the sensor is left pulled high, when you touch
  // two sensors, your body will transfer the charge between
  // sensors.
  *port &= ~(bitmask);
  *ddr |= bitmask;
  
  return cycles;
}[/code]


回复

使用道具 举报

 楼主| 发表于 2015-12-1 16:55:59 | 显示全部楼层
铜箔是那种屏蔽用的铜箔,另一面有双面胶。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-2 13:04:05 | 显示全部楼层
要沉贴了?@弘毅,帮忙诶~~~
回复 支持 反对

使用道具 举报

发表于 2015-12-2 15:56:41 | 显示全部楼层
涉及到不同材质的,看起来很难办

要不你试试玻璃?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-2 16:28:27 | 显示全部楼层
zoologist 发表于 2015-12-2 15:56
涉及到不同材质的,看起来很难办

要不你试试玻璃?

谢谢回复,用ad转换结果是电容充放电的曲线值,用帖子中的方法就是直接触摸到铜箔可以,隔着一层东西就不行了。我再找找其他的办法!
回复 支持 反对

使用道具 举报

发表于 2015-12-2 17:04:48 | 显示全部楼层
164335413 发表于 2015-12-2 16:28
谢谢回复,用ad转换结果是电容充放电的曲线值,用帖子中的方法就是直接触摸到铜箔可以,隔着一层东西就不 ...

哦 这样的话绝缘的东西都不行
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-12-8 09:19:01 | 显示全部楼层
昨天尝试用Pf级的电容和一些简单的电路做了一下,抗干扰能力有提升,我觉得铜箔的大小也会有关系,有效面积越大。看来还是模拟电路没搞好啊!
回复 支持 反对

使用道具 举报

发表于 2020-12-11 17:12:37 | 显示全部楼层
好东西,点个赞!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-25 09:35 , Processed in 0.043579 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表