单片机篮球计时计分器加时比赛代码实现
时间: 2023-08-31 11:05:07 浏览: 55
以下是一个基于Arduino的单片机篮球计时计分器加时比赛的代码实现:
```c++
#include <LiquidCrystal.h>
//定义液晶屏引脚
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//定义按键引脚
const int startPin = 6;
const int resetPin = 7;
const int addTimePin = 8;
const int homeScorePin = 9;
const int guestScorePin = 10;
//定义计时器初始值和步长
const int initialTime = 300; //5分钟
const int stepTime = 30; //加时长为30秒
//定义计时器和得分变量
int gameTime = initialTime;
int homeScore = 0;
int guestScore = 0;
//定义计时器状态,0表示停止,1表示计时
int timerState = 0;
void setup() {
//初始化液晶屏和按键
lcd.begin(16, 2);
pinMode(startPin, INPUT_PULLUP);
pinMode(resetPin, INPUT_PULLUP);
pinMode(addTimePin, INPUT_PULLUP);
pinMode(homeScorePin, INPUT_PULLUP);
pinMode(guestScorePin, INPUT_PULLUP);
//显示初始得分和计时器时间
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("HOME: ");
lcd.setCursor(6, 0);
lcd.print(homeScore);
lcd.setCursor(9, 0);
lcd.print("GUEST: ");
lcd.setCursor(15, 0);
lcd.print(guestScore);
lcd.setCursor(0, 1);
lcd.print("TIME: ");
lcd.setCursor(5, 1);
lcd.print(gameTime / 60);
lcd.print(":");
if (gameTime % 60 < 10) {
lcd.print("0");
}
lcd.print(gameTime % 60);
}
void loop() {
//判断按键状态,执行相应操作
if (digitalRead(startPin) == LOW && timerState == 0) {
//开始计时
timerState = 1;
}
if (digitalRead(resetPin) == LOW) {
//重置计时器和得分
gameTime = initialTime;
homeScore = 0;
guestScore = 0;
timerState = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("HOME: ");
lcd.setCursor(6, 0);
lcd.print(homeScore);
lcd.setCursor(9, 0);
lcd.print("GUEST: ");
lcd.setCursor(15, 0);
lcd.print(guestScore);
lcd.setCursor(0, 1);
lcd.print("TIME: ");
lcd.setCursor(5, 1);
lcd.print(gameTime / 60);
lcd.print(":");
if (gameTime % 60 < 10) {
lcd.print("0");
}
lcd.print(gameTime % 60);
}
if (digitalRead(addTimePin) == LOW && timerState == 0) {
//加时
gameTime += stepTime;
lcd.setCursor(5, 1);
lcd.print(gameTime / 60);
lcd.print(":");
if (gameTime % 60 < 10) {
lcd.print("0");
}
lcd.print(gameTime % 60);
}
if (digitalRead(homeScorePin) == LOW && timerState == 1) {
//主队得分
homeScore++;
lcd.setCursor(6, 0);
lcd.print(homeScore);
}
if (digitalRead(guestScorePin) == LOW && timerState == 1) {
//客队得分
guestScore++;
lcd.setCursor(15, 0);
lcd.print(guestScore);
}
//计时器递减
if (timerState == 1) {
gameTime--;
lcd.setCursor(5, 1);
lcd.print(gameTime / 60);
lcd.print(":");
if (gameTime % 60 < 10) {
lcd.print("0");
}
lcd.print(gameTime % 60);
delay(1000);
if (gameTime == 0) {
//计时结束
timerState = 0;
}
}
}
```
该代码使用了一个16x2液晶屏和5个按键,分别实现了开始计时、重置计时器和得分、加时、主队得分和客队得分等功能。在主循环中,不断检测按键状态和计时器状态,根据状态执行相应操作,同时通过递减计时器来实现计时功能。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)