用Arduino语言写一个三路抢答器代码,要求有一位数码管,无源蜂鸣器,三个抢答按键控制三种led灯和一个复位按键中断抢答过程,
时间: 2023-04-04 15:03:27 浏览: 154
感谢您的问题,以下是代码:
#include <TM1637.h>
#define CLK 2
#define DIO 3
TM1637 tm1637(CLK, DIO);
#define LED1 4
#define LED2 5
#define LED3 6
#define BUTTON1 7
#define BUTTON2 8
#define BUTTON3 9
#define BUZZER 10
#define RESET 11
volatile int buttonPressed = 0;
volatile int buttonWinner = 0;
void setup() {
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(BUTTON1, INPUT_PULLUP);
pinMode(BUTTON2, INPUT_PULLUP);
pinMode(BUTTON3, INPUT_PULLUP);
pinMode(BUZZER, OUTPUT);
pinMode(RESET, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(RESET), resetGame, FALLING);
tm1637.init();
tm1637.setBrightness(0x0f);
}
void loop() {
if (buttonPressed == 0) {
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
tm1637.display(0, 0);
tm1637.display(1, 0);
tm1637.display(2, 0);
if (digitalRead(BUTTON1) == LOW) {
buttonPressed = 1;
buttonWinner = 1;
} else if (digitalRead(BUTTON2) == LOW) {
buttonPressed = 1;
buttonWinner = 2;
} else if (digitalRead(BUTTON3) == LOW) {
buttonPressed = 1;
buttonWinner = 3;
}
} else {
digitalWrite(LED1, buttonWinner == 1 ? HIGH : LOW);
digitalWrite(LED2, buttonWinner == 2 ? HIGH : LOW);
digitalWrite(LED3, buttonWinner == 3 ? HIGH : LOW);
tm1637.display(0, buttonWinner);
tm1637.display(1, buttonWinner);
tm1637.display(2, buttonWinner);
tone(BUZZER, 1000, 100);
delay(1000);
buttonPressed = 0;
buttonWinner = 0;
}
}
void resetGame() {
buttonPressed = 0;
buttonWinner = 0;
}
阅读全文