本文章将教会大家如何使用Arduino制作抢答器。
一、目标实现:
使用Arduino编程,使得按下按钮对应的LED灯会亮起,按下结束所有LED灯熄灭。
二、前期准备:
软件准备:Arduino 18.8
硬件准备:Arduino开发板,LED灯,220Ω电阻,面包板,杜邦线,按键。
三、接线:
如下图:
四、编写程序:
int LED_R = 6; int LED_Y = 7; int LED_G = 8; int PIN_R = 3; int PIN_Y = 4; int PIN_G = 5; int OVER = 2; void setup() { pinMode(LED_R, OUTPUT); pinMode(LED_Y, OUTPUT); pinMode(LED_G, OUTPUT); pinMode(PIN_R, INPUT); pinMode(PIN_Y, INPUT); pinMode(PIN_G, INPUT); } void loop() { if (digitalRead(PIN_R) == LOW) { RED(); } if (digitalRead(PIN_Y) == LOW) { YELLOW(); } if (digitalRead(PIN_G) == LOW) { GREEN(); } } void clear_led() { digitalWrite(LED_R, LOW); digitalWrite(LED_Y, LOW); digitalWrite(LED_G, LOW); } void RED() { while (digitalRead(OVER) == 1) { digitalWrite(LED_R, HIGH); digitalWrite(LED_Y, LOW); digitalWrite(LED_G, LOW); } clear_led(); } void YELLOW() { while (digitalRead(OVER) == 1) { digitalWrite(LED_R, LOW); digitalWrite(LED_Y, HIGH); digitalWrite(LED_G, LOW); } clear_led(); } void GREEN() { while (digitalRead(OVER) == 1) { digitalWrite(LED_R, LOW); digitalWrite(LED_Y, LOW); digitalWrite(LED_G, HIGH); } clear_led(); }
五、Arduino抢答器成果展示: