米思齐触控+RGB灯实现红绿蓝灯循环切换

材料

Arduino uno R3 开发板:1个
RGB彩色流水灯四脚:1个
触摸式开关三脚:1个

硬件连接米思齐触控+RGB灯实现红绿蓝灯循环切换_第1张图片

RGB LED灯--------Arduino开发板
red-------11
green-------10
blue-------9
GND------GND

触摸开关-------------Arduino开发板
GND--------GND
VCC--------3.3V
SIG---------2

LED灯代码和效果

实验效果:每触摸一次,颜色进行下面的循环转换:红色-绿色-蓝色-暗

volatile int PIN_B;
volatile int PIN_G;
volatile int PIN_R;
//定义RGB灯颜色控制引脚
volatile int buttonState;
volatile int PIN_COUNT;//定义一个变量,用于控制颜色的转换
volatile int PIN_KEY;//定义按键位管脚

void setup(){
pinMode(2, INPUT);
PIN_R = 11;
PIN_G = 10;
PIN_B = 9;
PIN_KEY = 2;
PIN_COUNT = 0;
buttonState = 0;
pinMode(PIN_B, OUTPUT);
pinMode(PIN_G, OUTPUT);
pinMode(PIN_R, OUTPUT);
//设置RGB对应的管脚为输出
pinMode(PIN_KEY, INPUT);//设置PIN_KEY对应的管脚2为输入
}

//写一个颜色输出的方法
void color(char red, char green, char blue) {
pinMode(PIN_R, OUTPUT);
digitalWrite(PIN_R,(255 - red));
pinMode(PIN_G, OUTPUT);
digitalWrite(PIN_G,(255 - green));
pinMode(PIN_B, OUTPUT);
digitalWrite(PIN_B,(255 - blue));
}

void loop(){
buttonState = digitalRead(2);
if (buttonState ==HIGH) {
delay(1000);//延时
PIN_COUNT = PIN_COUNT + 1;
if ((long) (PIN_COUNT) % (long) (4) == 1) {
color(0, 255, 255);//红色
}
if ((long) (PIN_COUNT) % (long) (4) == 2) {
color(255, 0, 255);//绿色
}
if ((long) (PIN_COUNT) % (long) (4) == 3) {
color(255, 255, 0);//蓝色
}
if ((long) (PIN_COUNT) % (long) (4) == 0) {
color(255, 255, 255);//黑色
}
}

你可能感兴趣的:(米思齐触控+RGB灯实现红绿蓝灯循环切换)