【发布时间】:2022-01-31 02:06:20
【问题描述】:
一般来说,我是编码新手,我的老师告诉我们用物理 LED 制作倒数计时器,并使用网络服务器(烧瓶)作为虚拟按钮。我想显示计时器的当前时间,但我似乎无法使用 LED 进行当前时间倒计时,因为它试图在更新之前完成 LED 代码。每次点击都会更新,计数工作... 这是我的烧瓶代码:
import RPi.GPIO as GPIO
import time
from flask import Flask, render_template, request
app = Flask(__name__)
GPIO.setmode (GPIO.BCM)
GPIO.setup(5, GPIO.OUT)
GPIO.setup(6, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(19, GPIO.OUT)
GPIO.setup(26, GPIO.OUT)
intRepeat = 0
intCount = 0
@app.route('/')
def index():
templateData = {
'intCount' : intCount
}
return render_template('index3.html', **templateData)
@app.route('/up/')
def up():
global intCount
intCount = intCount + 1
if intCount == 1:
GPIO.output(5, GPIO.HIGH)
elif intCount == 2:
GPIO.output(6, GPIO.HIGH)
GPIO.output(5, GPIO.LOW)
elif intCount == 3:
GPIO.output(5, GPIO.HIGH)
elif intCount == 4:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
elif intCount == 5:
GPIO.output(5, GPIO.HIGH)
elif intCount == 6:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
elif intCount == 7:
GPIO.output(5, GPIO.HIGH)
elif intCount == 8:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.HIGH)
elif intCount == 9:
GPIO.output(5, GPIO.HIGH)
elif intCount == 10:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
elif intCount == 11:
GPIO.output(5, GPIO.HIGH)
elif intCount == 12:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
elif intCount == 13:
GPIO.output(5, GPIO.HIGH)
elif intCount == 14:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
elif intCount == 15:
GPIO.output(5, GPIO.HIGH)
elif intCount == 16:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.HIGH)
elif intCount == 17:
GPIO.output(5, GPIO.HIGH)
elif intCount == 18:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
elif intCount == 19:
GPIO.output(5, GPIO.HIGH)
elif intCount == 20:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
elif intCount == 21:
GPIO.output(5, GPIO.HIGH)
elif intCount == 22:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
elif intCount == 23:
GPIO.output(5, GPIO.HIGH)
elif intCount == 24:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.HIGH)
elif intCount == 25:
GPIO.output(5, GPIO.HIGH)
elif intCount == 26:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
elif intCount == 27:
GPIO.output(5, GPIO.HIGH)
elif intCount == 28:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
elif intCount == 29:
GPIO.output(5, GPIO.HIGH)
elif intCount == 30:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
elif intCount == 31:
GPIO.output(5, GPIO.HIGH)
elif intCount == 32:
intCount = intCount - 1
templateData = {
'intCount' : intCount
}
return render_template('index3.html', **templateData)
@app.route('/down/')
def down():
global intCount
while(intCount > 0):
intCount = intCount - 1
time.sleep(1)
if intCount == 31:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.HIGH)
elif intCount == 30:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.HIGH)
elif intCount == 29:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.HIGH)
elif intCount == 28:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.HIGH)
elif intCount == 27:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.HIGH)
elif intCount == 26:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.HIGH)
elif intCount == 25:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.HIGH)
elif intCount == 24:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.HIGH)
elif intCount == 23:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.HIGH)
elif intCount == 22:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.HIGH)
elif intCount == 21:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.HIGH)
elif intCount == 20:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.HIGH)
elif intCount == 19:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.HIGH)
elif intCount == 18:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.HIGH)
elif intCount == 17:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.HIGH)
elif intCount == 16:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.HIGH)
elif intCount == 15:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.LOW)
elif intCount == 14:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.LOW)
elif intCount == 13:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.LOW)
elif intCount == 12:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.LOW)
elif intCount == 11:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.LOW)
elif intCount == 10:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.LOW)
elif intCount == 9:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.LOW)
elif intCount == 8:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.LOW)
elif intCount == 7:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
elif intCount == 6:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
elif intCount == 5:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
elif intCount == 4:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
elif intCount == 3:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
elif intCount == 2:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
elif intCount == 1:
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
elif intCount == 0:
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
#loop for repeated flashing
global intRepeat
while(intRepeat <= 5) and (intCount == 0):
intRepeat = intRepeat + 1
time.sleep(0.4)
GPIO.output(5, GPIO.HIGH)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.HIGH)
time.sleep(0.4)
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.HIGH)
GPIO.output(26, GPIO.LOW)
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
intRepeat = 0
templateData = {
'intCount' : intCount
}
return render_template('index3.html', **templateData)
@app.route('/reset/')
def reset():
global intCount
intCount = 0
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(26, GPIO.LOW)
templateData = {
'intCount' : intCount
}
return render_template('index3.html', **templateData)
if __name__ == '__main__':
app.run(debug=True, port=777, host='0.0.0.0')
这是我的 HTML 索引模板
<!DOCTYPE html>
<body>
<h1>CountDown Timer 1.0<h1>
<p>Current time: {{ intCount }}</p>
<p>
<form action="/up/">
<input type="submit" style="height:200px;width:200px" value="Count Up" />
</form>
</p>
<p>
<form action="/down/">
<input type="submit" style="height:200px;width:200px" value="Start!" />
</form>
</p>
<p>
<form action="/reset/">
<input type="submit" style="height:200px;width:200px" value="Reset/Stop" />
</form>
</p>
</body>
</html>
我看过其他使用 AJAX 的帖子,但我不知道该怎么做
PS 我知道二进制 LED 倒计时效率低下,但这会...
【问题讨论】:
标签: python html flask timer countdown