久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4543|回復: 0
打印 上一主題 下一主題
收起左側

創客集結號:Arduino簡易觸摸傳感器

[復制鏈接]
跳轉到指定樓層
樓主
Arduino除了接受數字端口的數字信號,唯一能檢測的模擬物理量就是電壓。任何模擬傳感器的檢測值幾乎都是通過相關電路轉化成電壓值,再輸入它的模擬端口進行模數轉換的。電容值就需要相對更復雜和昂貴的電路轉化為電壓值,才能被Arduino檢測,而對很多物理過程的檢測,都可以很方便可靠地通過檢測電容值來實現,其中最常用的地方就是觸摸傳感器。現在,創客集結號為大家介紹一個除了一段導線和一個端口,不需要任何元器件的電容檢測方法。

這個方法的思路是,首先把一個數字端口設成低電位,并打開arduino的內部上拉電阻,開始計算這個端口到達高電位所需要的時間。而這個時間與此端口的對地電容值有關,電容越大,時間越長。在硬件上只需要在一個端口上連一根導線即可。用手指觸摸這段導線的裸露端,就會導致電容變化,arduino可以通過上述方法檢測這個變化。如果要增加靈敏度,可以在導線上連一片錫箔。為防止你手上有強靜電擊穿芯片,可以在錫箔上蓋一層薄紙。

使用的代碼如下:
int ledPin = 9;
int capval;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("Touch senser");
}

void loop ()
{
digitalWrite(ledPin,LOW);
capval = readCapacitivePin(8);
Serial.println(capval, DEC);
if (capval > 2) {
// turn LED on:
digitalWrite(ledPin, HIGH);
delay(10);
}
}

// readCapacitivePin
//  Input: Arduino pin number
//  Output: A number, from 0 to 17 expressing
//  how much capacitance is on the pin
//  When you touch the pin, or whatever you have
//  attached to it, the number will get higher
#include "pins_arduino.h" // Arduino pre-1.0 needs this
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);
  uint8_t SREG_old = SREG; //back up the AVR Status Register
  // Prevent the timer IRQ from disturbing our measurement
  noInterrupts();
  // 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;}

  // End of timing-critical section; turn interrupts back on if they were on before, or leave them off if

they were off before
  SREG = SREG_old;

  // 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;
}
若想學習更多arduino項目教程,可以搜索創客集結號或xk100。

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 国产线视频精品免费观看视频 | 亚洲精品日本 | 精品国产一二三区 | 羞羞视频在线免费 | av网址在线| 国产精品久久久久久久久大全 | 国产精品久久久久久久久久久免费看 | 影音先锋男 | 91在线视频观看免费 | 欧洲精品码一区二区三区免费看 | 日韩一区二区三区在线观看 | 亚洲毛片一区二区 | 久草中文在线 | 中文字幕第一页在线 | 久久一区二区三区电影 | 欧美激情精品久久久久久免费 | 亚洲一区二区中文字幕 | 欧美日韩国产三级 | 永久免费av | 国产精品自产拍 | 亚洲高清成人 | 国产电影一区二区三区爱妃记 | 免费在线一区二区 | 综合二区 | 成人在线观看免费视频 | 一级黄色大片 | 欧美极品视频在线观看 | a级毛片国产 | 午夜免费在线 | 久久这里只有精品首页 | 国产精品高潮呻吟久久av黑人 | 亚洲高清久久 | 手机在线一区二区三区 | 亚洲午夜av久久乱码 | 一区二区三区四区电影视频在线观看 | a成人| 一级片免费视频 | 在线观看免费黄色片 | 99久久久久国产精品免费 | 久久综合入口 | 色婷婷狠狠 |