倒 计 时

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<style>
  div {
    margin: 200px;
  }

  span {
    /* 转换为行内块元素 */
    display: inline-block;
    width: 40px;
    height: 40px;
    background-color: #333;
    font-size: 20px;
    color: #fff;
    text-align: center;
    line-height: 40px;
  }
</style>
</head>
<body>
<div>
  <!-- 行内元素 -->
  <span class="hour">时</span>
  <span class="minute">分</span>
  <span class="second">秒</span>
</div>
<script>
  var hour=document.querySelector('.hour');
  var minute=document.querySelector('.minute');
  var second=document.querySelector('.second');
  var times1=+new Date('2022-4-20 20:10:00');
  function jishi(){
    var nowtime=+new Date();
    var time=(times1-nowtime)/1000;
    var h=parseInt(time/60/60%24);
    h=h<10 ? '0'+h : h;
    hour.innerHTML=h;
    var m=parseInt(time/60%60);
    m=m<10 ? '0'+m : m;
    minute.innerHTML=m;
    var s=parseInt(time%60);
    s=s<10 ? '0'+s : s;
    second.innerHTML=s;
  }
  jishi();
  setInterval(jishi,1000);
</script>
</body>
</html>
这是一个基于Arduino的示例代码,使用了一个4位数码管、一个按钮和一个蜂鸣器。请确保你已经连接好了这些硬件设备。 ```c++ #include <SevSeg.h> SevSeg sevseg; // 数码管对象 const int buttonPin = 2; // 按钮引脚 const int buzzerPin = 3; // 蜂鸣器引脚 int countdown = 0; // 间(秒) bool countdownStarted = false; // 是否已经开始 bool countdownStopped = false; // 是否已经停止 bool countdownFinished = false; // 是否已经完成 void setup() { byte numDigits = 4; // 数码管显示4位数字 byte digitPins[] = {4, 5, 6, 7}; // 数码管的4个引脚 byte segmentPins[] = {8, 9, 10, 11, 12, 13, A0, A1}; // 数码管的7个段的引脚 bool resistorsOnSegments = false; // 数码管的段是否接上了电阻 bool updateWithDelays = false; // 是否使用延更新数码管的显示 bool leadingZeros = false; // 是否在前面补0 bool disableDecPoint = true; // 是否禁用小数点 sevseg.begin(COMMON_ANODE, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros, disableDecPoint); pinMode(buttonPin, INPUT_PULLUP); // 按钮使用内置上拉电阻 pinMode(buzzerPin, OUTPUT); // 蜂鸣器输出 Serial.begin(9600); // 打开串口以便调试 } void loop() { // 读取按钮状态 bool buttonState = digitalRead(buttonPin) == LOW; // 如果按钮被按下并且没有开始,则设置间 if (buttonState && !countdownStarted) { countdown = 10; // 设置间为10秒 countdownStarted = true; // 标记已经开始 countdownStopped = false; // 标记没有停止 countdownFinished = false; // 标记没有完成 } // 如果按钮被按下并且已经开始,则停止并重新设置间 if (buttonState && countdownStarted && !countdownStopped) { countdownStopped = true; // 标记已经停止 countdown = 5; // 重新设置间为5秒 } // 更新数码管的显示 if (!countdownFinished) { sevseg.setNumber(countdown); // 显示间 sevseg.refreshDisplay(); // 刷新数码管的显示 } // 如果已经开始并且没有被停止,则减1 if (countdownStarted && !countdownStopped && !countdownFinished) { countdown--; // 减1 if (countdown == 0) { countdownFinished = true; // 标记已经完成 digitalWrite(buzzerPin, HIGH); // 开启蜂鸣器 delay(3000); // 等待3秒 digitalWrite(buzzerPin, LOW); // 关闭蜂鸣器 } } // 调试信息 Serial.print("Countdown: "); Serial.println(countdown); Serial.print("Countdown started: "); Serial.println(countdownStarted); Serial.print("Countdown stopped: "); Serial.println(countdownStopped); Serial.print("Countdown finished: "); Serial.println(countdownFinished); Serial.println(); } ``` 这个代码主要是通过读取按钮的状态来设置间,并且可以在过程中按下按钮停止并重新设置间。同,它还会在结束开启蜂鸣器响3秒。你可以根据需要修改代码来适应自己的硬件环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值