![「Arduino」利用Touch模塊實現觸摸開關的功能](http://p2.ttnews.xyz/loading.gif)
今天我們利用Touc模塊製作一個觸摸開關。OLED來倒計時和顯示LED的狀態。LED燈來實現亮滅的功能。
![「Arduino」利用Touch模塊實現觸摸開關的功能](http://p2.ttnews.xyz/loading.gif)
本次完成的內容需要的材料:
Arduino UNO*1
一塊觸摸模塊Touch
一個OLED燈
一個LED燈
導線(若干)
線路圖:
所需代碼:
<code>/*********************************************************** File name: _15_TouchButtonModule.ino Description: The information of touch button module has been detected by UNO R3,and displayed in the serial monitor Website: www.adeept.com E-mail: [email protected] Author: Tom Date: 2017/03/15 ***********************************************************/ #include #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); int touchbuttonPin=2; //Set the digital 8 to touch button int LEDPin=13; //Set the digital 8 to touch button void setup() { pinMode( touchbuttonPin,INPUT);//initialize the touch button pin as input pinMode( LEDPin,OUTPUT);//initialize the touch button pin as input display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64) display.clearDisplay(); // Make sure the display is cleared display.setTextColor(WHITE); } unsigned int m_delay = 0; void loop() { display.clearDisplay(); display.setTextSize(2); display.setCursor(25,0); display.print("LED:"); if(m_delay > 0) { display.setCursor(70,0); display.print("ON"); display.setCursor(60,15); display.print(m_delay); display.display(); delay(1000); //delay 1s m_delay--; } else { digitalWrite(LEDPin, LOW); display.setCursor(70,0); display.print("OFF"); display.setCursor(60,15); display.print(m_delay); display.display(); } if(digitalRead(touchbuttonPin)==HIGH) { digitalWrite(LEDPin, HIGH); m_delay+=3; } }/<code>