最近小编入手了一块基于I2C通信方式的触摸控制板,可以和手中的Arduino控制器配合应用,于是小编就想做个好玩的东西——基于Arduino的触摸式变色台灯。
Arduino
Arduino是一款便捷灵活、方便上手的开源电子原型平台,包含硬件(各种型号的Arduino控制器)和软件(Arduino IDE)。Arduino能通过各种各样的传感器来感知环境,通过控制灯光、马达和其他的装置来反馈、影响环境。对Arduino的编程是利用Arduino编程语言(基于Wiring)和Arduino开发环境(based on Processing)来实现的。基于Arduino的项目,可以只包含Arduino,也可以包含Arduino和其他一些在PC上运行的软件,它们之间进行通信来实现。
MPR121
MPR121是飞思卡尔公司设计研发的一种电容式触摸感应控制器,具有内部情报功能,可以感受手指对接收器的触摸或者接近,从而发出相应的感应信号提供给控制器。MPR121较之前的版本增加了一个内部情报和飞思卡尔的第二电容检测引擎,其次还增强了电极计数,硬件配置的I2C地址,一个扩大过滤系统和防反跳,完全独立电极带内置自动配置等功能。该设备还包括一个13模拟电极,代表同时充电的电极连接在一起,允许更大的附近探测距离和增加传感区域。
下面就看看我们需要的器材都有哪些:
Arduino控制器,各种类型的都可以,这里我们采用手中现有的Arduino UNO控制器,当然了,我们还得有和Arduino配套使用的传感器扩展板。
然后就是我们的重要器件了——I2C Touch Sensor和照明用的各色LED了,小编手中正好有红色、绿色、黄色和白色的,完全可以用于此次的制作了。
除了上述的器件,我们还需要一块面包板和连接线
让我们现在简单的验证一下这个想法是否可行,首先将各种需要的器件都按照需要使用面包板连接在一起,验证一下代码是否可行(库文件点这里)。
#include
#include
#define MPR121_R 0xB5
#define MPR121_W 0xB4
#define MPR121addr 0x5a
int StateOfTouch1 = 0;
int StateOfTouch2 = 0;
int StateOfTouch3 = 0;
int StateOfTouch4 = 0;
int irqpin = 2;
uint16_t touchstatus;
volatile int GetTouchSignal = 0;
unsigned char reg = 0;
unsigned char status1 =0;
unsigned char status2 =0;
long previousMillis = 0;
long interval = 100;
void setup()
{
Serial.begin(9600);
DDRC |= 0b00010011;
PORTC = 0b00110000;
i2cInit();
Serial.println("setting device up..");
delay(100);
mpr121QuickConfig();
}
void loop()
{
unsigned char MPR_Query=0;
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval)
{
previousMillis = currentMillis;
Read_MPR121();
}
if (Serial.available()>0)
{
Serial.println("Get a command form PC..");
reg = Serial.read();
Serial.print("Quiry the Reg of ");
Serial.println(reg,BIN);
MPR_Query = mpr121Read(reg);
Serial.print("the reg is = 0x");
Serial.println(MPR_Query,HEX);
}
delayMicroseconds(1000);
}
void Read_MPR121()
{
status1=mpr121Read(0x00);
status2=mpr121Read(0x01);
CheckStatus();
}
void CheckStatus()
{
if ((status1&0x01)==0x01)
{
StateOfTouch1 =1;
}
else
{
if (StateOfTouch1 ==1)
{
Serial.println("channel 0 has pressed..");
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
StateOfTouch1 =0;
}
}
if ((status1&0x02)==0x02)
{
StateOfTouch2 =1;
}
else
{
if (StateOfTouch2 ==1)
{
Serial.println("channel 1 has pressed..");
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
StateOfTouch2 =0;
}
}
if ((status1&0x04)==0x04)
{
StateOfTouch3 =1;
}
else
{
if (StateOfTouch3 ==1)
{
Serial.println("channel 2 has pressed..");
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
StateOfTouch3 =0;
}
}
if ((status1&0x08)==0x08)
{
StateOfTouch4 =1;
}
else
{
if (StateOfTouch4 ==1)
{
Serial.println("channel 3 has pressed..");
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
StateOfTouch4 =0;
}
}
}
byte mpr121Read(uint8_t address)
{
byte data;
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(MPR121_W);
i2cWaitForComplete();
i2cSendByte(address);
i2cWaitForComplete();
i2cSendStart();
i2cSendByte(MPR121_R);
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
data = i2cGetReceivedByte();
i2cWaitForComplete();
i2cSendStop();
cbi(TWCR, TWEN);
sbi(TWCR, TWEN);
return data;
}
void mpr121Write(unsigned char address, unsigned char data)
{
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(MPR121_W);
i2cWaitForComplete();
i2cSendByte(address);
i2cWaitForComplete();
i2cSendByte(data);
i2cWaitForComplete();
i2cSendStop();
}
void mpr121QuickConfig(void)
{
Serial.println("config MPR121..");
mpr121Write(MHD_R, 0x01);
mpr121Write(NHD_R, 0x01);
mpr121Write(NCL_R, 0x00);
mpr121Write(FDL_R, 0x00);
mpr121Write(MHD_F, 0x01);
mpr121Write(NHD_F, 0x01);
mpr121Write(NCL_F, 0xFF);
mpr121Write(FDL_F, 0x02);
mpr121Write(ELE0_T, TOU_THRESH);
mpr121Write(ELE0_R, REL_THRESH);
mpr121Write(ELE1_T, TOU_THRESH);
mpr121Write(ELE1_R, REL_THRESH);
mpr121Write(ELE2_T, TOU_THRESH);
mpr121Write(ELE2_R, REL_THRESH);
mpr121Write(ELE3_T, TOU_THRESH);
mpr121Write(ELE3_R, REL_THRESH);
mpr121Write(ATO_CFGU, 0xC9);
mpr121Write(ATO_CFGL, 0x82);
mpr121Write(ATO_CFGT,0xb5);
mpr121Write(ATO_CFG0, 0x1B);
mpr121Write(ELE_CFG, 0x8c);
Serial.println("config MPR121 Done!");
}
void checkInterrupt(void)
{
int IRQ =0;
IRQ = digitalRead(irqpin);
if(IRQ == 0)
{
GetTouchSignal=1;
}
else
{
GetTouchSignal=0;
}
}
相关联接
Arduino UNO
Arduino 传感器扩展板
红色LED
面包板
面包板跳线