1、触摸变色台灯七彩台灯制作的元器件如下(元器件图在文件夹中):1、Light Disc with 7 SMD RGB LED(七彩 LED 灯)七彩 LED 灯 (1).jpg (58.6 KB)2、DFRduino Nano(主控芯片为 ATMEGA328P)板子DFRduino Nano.jpg (57.5 KB)3、Capacitive Touch Sensor(触摸开关)触摸开关.jpg (73.16 KB)4、USB 下载线一条USB 下载线.jpg (72.29 KB)5、面包板一块及插针、导线若干面包板.jpg (82.31 KB)6、小纸杯一个(本来是用来罩住七彩 LED 灯产生
2、绚丽效果,但是未找到合适的 )七彩台灯的开发环境为 Arduino,版本为 arduino-0022(同上回 LED 魔方版本一样)。代码大致过程是:首先确定能产生 PWM 信号的 AVR 单片机数字 I/O 引脚(D3、D5 、D6、D9、D10、D11)用于控制 RGB_LED 七彩灯的电平给入,选择D9 控制七彩灯的 G 引脚,D10 控制七彩灯的 R 引脚,D5 控制七彩灯的 B 引脚。而单片机 D2 引脚控制触摸开关电平的读取。选择定时器 T/C0 为外部中断,每当触摸开关电平改变,触发一次中断。T/C2 为内部溢出中断(此内部中断为普通工作模式),设定为每 2ms 溢出一次。当程序
3、下载完成,七彩灯不工作,长按触摸开关 5s,彩灯处于普通模式下,再触摸开关 3s,进入调光模式,调光模式下彩灯一直处于多种色彩循环亮状态。最后长按 5s 触摸开关,彩灯关闭。 (如 DV 拍摄视频所示)注:由于标志位太多,逻辑比较复杂,不大好控制,因此,暂时写这么一点小小的功能,更多功能后续补加。 复制内容到剪贴板 代码:#define touch 2 /设置触摸的数字 IO 脚 2#define LED_R 10 /全彩灯 红色#define LED_G 9 /全彩灯 绿色#define LED_B 5 /全彩灯 蓝色void setup()pinMode(touch,INPUT);/设置数
4、字 IO 引脚 2 为输入pinMode(9,OUTPUT);/设置数字 IO 引脚 为输出pinMode(10,OUTPUT);/设置数字 IO 引脚 为输出pinMode(11,OUTPUT);/设置数字 IO 引脚 为输出analogWrite(LED_R,0);/设置第 10 引脚为高电平,点亮 led 灯analogWrite(LED_G,0);/设置第 9 引脚为高电平,点亮 led 灯analogWrite(LED_B,0);/设置第 5 引脚为高电平,点亮 led 灯timer2_init(); /初始化定时器 2attachInterrupt(0, blink, CHANGE
5、); /设置为 0 号中断,中断函数 blink,触发方式为 changevoid timer2_init(void)/定时器 2 初始化 /TCCR2A = 0X00;TCCR2B = 0X07; /时钟源 1024 分频TCNT2 = 224; /2ms 溢出一次TIMSK2 = 0X01; /允许中断volatile int flag = 0; / 中断状态volatile int flag_1 = 0; / 工作状态标识volatile int flag_2 = 0; / 工作状态标识volatile int flag_s = 0; / 中断标识volatile int T = 0;
6、/声明 volatile int 量void loop()if(flag_s = 1) /开机状态下- 可以预设为读取 EPROM 中的值analogWrite(LED_R,200);analogWrite(LED_G,100);analogWrite(LED_B,100);if(flag_s = 2) /关机状态下analogWrite(LED_R,0);analogWrite(LED_G,0);analogWrite(LED_B,0);if(flag_s = 3) /调光模式下analogWrite(LED_R,155);analogWrite(LED_G,0);analogWrite(L
7、ED_B,0);delay(200);analogWrite(LED_R,0);analogWrite(LED_G,155);analogWrite(LED_B,0);delay(200);analogWrite(LED_R,0);analogWrite(LED_G,0);analogWrite(LED_B,155);delay(200);analogWrite(LED_R,255);analogWrite(LED_G,100);analogWrite(LED_B,0);delay(200);analogWrite(LED_R,255);analogWrite(LED_G,0);analogW
8、rite(LED_B,100);delay(200);analogWrite(LED_R,100);analogWrite(LED_G,255);analogWrite(LED_B,0);delay(200);analogWrite(LED_R,0);analogWrite(LED_G,255);analogWrite(LED_B,100);delay(200);analogWrite(LED_R,255);analogWrite(LED_G,100);analogWrite(LED_B,0);delay(200);analogWrite(LED_R,255);analogWrite(LED_
9、G,0);analogWrite(LED_B,100);delay(200);analogWrite(LED_R,255);analogWrite(LED_G,100);analogWrite(LED_B,100);delay(200);analogWrite(LED_R,100);analogWrite(LED_G,255);analogWrite(LED_B,100);delay(200);analogWrite(LED_R,100);analogWrite(LED_G,100);analogWrite(LED_B,255);delay(200);analogWrite(LED_R,255
10、);analogWrite(LED_G,255);analogWrite(LED_B,255);delay(200);void blink()/外部中断执行函数if(digitalRead(touch) = 1)switch(flag_1)case (0): /持续触碰按键 5s,自动开机flag = 1;break;case (1):if(flag_2 = 0) /持续触碰按键 5s,自动关机flag = 3;if(flag_2 = 1) /调光模式flag = 2;break;if(digitalRead(touch) = 0)flag = 0;ISR(TIMER2_OVF_vect)/定时器 2,2ms 中断溢出一次.内部溢出中断执行函数TCNT2 = 224;/给定时器计时初值if(flag = 1) /开启台灯T +;if(T=2500)flag_s = 1; / 到 5 秒标志开启台灯T = 0;flag_1 = 1;if(flag = 2) /关闭台灯T +;if(T=2500)flag_s = 2; / 到 5 秒标志关闭台灯T = 0;flag_1 = 0;flag_2 = 0;if(flag = 3) /调光模式T +;if(1500= T / 到 3 秒标志调光模式T = 0;flag_2 = 1;