通过轻触开关控制双色LED灯
下图是轻触开关模块
Arduion | 轻触开关 |
---|---|
GND | - |
5v | |
8 | S |
Arduion | 双色LED |
---|---|
GND | - |
9 | |
7 | S |
const int switchPin = 8;
const int redPin = 9;
const int greenPin = 7;
int state = 0;
void setup() {
pinMode(switchPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
void loop() {
state = digitalRead(switchPin);
if(state == HIGH){
digitalWrite(greenPin, LOW);
digitalWrite(redPin, HIGH);
}else {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
}
}
按住开关LED灯显示绿色,松开LED灯显示红色