Arduino做一个三路抢答器,有蜂鸣器,数码管,按键控制led灯
时间: 2023-03-25 19:03:09 浏览: 337
可以回答这个问题。对于这个问题,可以使用Arduino开发板和相关电子元件来实现一个三路抢答器。可以使用数码管来显示当前的抢答顺序,使用蜂鸣器来提示抢答开始和结束,使用按键来控制LED灯的亮灭。具体实现方法可以参考相关的Arduino教程和资料。
相关问题
用Arduino语言写一个三路抢答器代码,要求有一位数码管,无源蜂鸣器,三个抢答按键控制三种led灯和一个复位按键中断抢答过程,
感谢您的问题,以下是代码:
#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;
}
用Arduino语言写一个三路抢答器代码,要求有数码管上数字显示蜂鸣器响声以及控制led灯的亮灭
感谢您的提问。以下是Arduino语言写一个三路抢答器的代码:
int ledPin1 = 2; // 第一组LED灯的引脚
int ledPin2 = 3; // 第二组LED灯的引脚
int ledPin3 = 4; // 第三组LED灯的引脚
int buzzerPin = 5; // 蜂鸣器的引脚
int digitPins[] = {6, 7, 8, 9}; // 数码管的引脚
int digits[] = {0, 0, 0, 0}; // 数码管显示的数字
int button1 = 10; // 第一组按钮的引脚
int button2 = 11; // 第二组按钮的引脚
int button3 = 12; // 第三组按钮的引脚
int lastButton = 0; // 上一次按下的按钮
int lastTime = 0; // 上一次按下按钮的时间
int interval = 1000; // 抢答器的时间间隔
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
for (int i = 0; i < 4; i++) {
pinMode(digitPins[i], OUTPUT);
}
}
void loop() {
int button = 0;
if (digitalRead(button1) == LOW) {
button = 1;
} else if (digitalRead(button2) == LOW) {
button = 2;
} else if (digitalRead(button3) == LOW) {
button = 3;
}
if (button != 0 && button != lastButton) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(buzzerPin, LOW);
digits[3 - lastButton] = 0;
lastButton = button;
lastTime = millis();
}
if (button == lastButton && millis() - lastTime >= interval) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(buzzerPin, HIGH);
digits[3 - lastButton] = 1;
}
for (int i = 0; i < 4; i++) {
digitalWrite(digitPins[i], LOW);
switch (digits[i]) {
case 0:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
break;
case 1:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
break;
}
digitalWrite(digitPins[i], HIGH);
}
}
希望对您有所帮助。
阅读全文