【Arduino】利用Touch模块实现触摸开关的功能

天我们利用Touc模块制作一个触摸开关。OLED来倒计时和显示LED的状态。LED灯来实现亮灭的功能。

本次完成的内容需要的材料:

Arduino UNO*1

一块触摸模块Touch

一个OLED灯

一个LED灯

导线(若干)

线路图:

所需代码:

/***********************************************************

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: support@adeept.com

Author: Tom

Date: 2017/03/15 

***********************************************************/

#include <Adafruit_SSD1306.h>

#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;

  }

    

}
跳至工具栏