Description
My ultimate goal with this program is to have a Flask web app be projected, someone connects to it, and they can control stepper motors with it using the Adafruit Stepper Motor HAT. At the moment, the visual code is a bit of a mess due to me wanting to have a login system, and realizing that with my limited time to get this done, I need to focus on making it work...
What's Wrong?
At the moment, the stepper motors work fine. The user pushes the (what's called for now) login button, and the motors spin, and pushes the stop motors button to stop. That's where the problem comes in... The motors don't stop, they just slow down to a painful pace, and the web page gets stuck trying to load an infinite loop. How do I stop this?
Try to understand that I have been at this for a week, and have pulled every trick I know. The code is a bit messy with the global variables and all now due to my desperation to just make it work.
Code is included here:
Python
from flask import Flask, flash, redirect, render_template, request, session, abort
import os
import time
from adafruit_motorkit import MotorKit
kit = MotorKit()
app = Flask(__name__)
def set_z_to_zero():
global z # Needed to modify global copy of globvar
z = 0
def set_z_to_one(): # Needed to modify global copy of globvar
z = 1
def rel():
kit.stepper1.release()
kit.stepper2.release()
set_z_to_one()
@app.route('/')
def home():
rel()
if not session.get('logged_in'):
return render_template('login.html')
else:
return "Hello Boss! <a href=/logout>Logout</a>"
@app.route('/stop', methods=['GET'])
def stop_motors():
rel()
return render_template('login.html')
def run():
for i in range(100):
kit.stepper1.onestep()
kit.stepper2.onestep()
if z == 1:
break
@app.route('/login', methods=['GET'])
def start_motors():
while z == 0:
run()
if z == 1:
break
set_z_to_zero()
rel()
return render_template('login.html')
if __name__ == "__main__":
app.secret_key = os.urandom(12)
set_z_to_zero()
app.run(debug=True,host='0.0.0.0', port=3860)
HTML
<form action="/login" method="GET">
<div class="login">
<div class="login-screen">
<div class="app-title">
<h1>Login</h1>
</div>
<div class="control-group">
<input type="text" class="login-field" value="" placeholder="username" name="username">
<label class="login-field-icon fui-user" for="login-name">
</label>
</div>
<input type="submit" value="Log in" class="btn btn-primary btn-large btn-block">
</form>
<form action="/stop" method="GET">
<input type="submit" value="Stop Motors" class="btn btn-primary btn-large btn-block">
</form>
</div>
</div>
CSS
box-sizing: border-box;
}
*:focus {
outline: none;
}
body {
font-family: Arial;
background-color: #3498DB;
padding: 50px;
}
.login {
margin: 20px auto;
width: 300px;
}
.login-screen {
background-color: #FFF;
padding: 20px;
border-radius: 5px
}
.app-title {
text-align: center;
color: #777;
}
.login-form {
text-align: center;
}
.control-group {
margin-bottom: 10px;
}
input {
text-align: center;
background-color: #ECF0F1;
border: 2px solid transparent;
border-radius: 3px;
font-size: 16px;
font-weight: 200;
padding: 10px 0;
width: 250px;
transition: border .5s;
}
input:focus {
border: 2px solid #3498DB;
box-shadow: none;
}
.btn {
border: 2px solid transparent;
background: #3498DB;
color: #F0FFFF;
font-size: 16px;
line-height: 25px;
padding: 10px 0;
text-decoration: none;
text-shadow: none;
border-radius: 3px;
box-shadow: none;
transition: 0.25s;
display: block;
width: 250px;
margin: 0 auto;
}
.btn:hover {
background-color: #2980B9;
}
.login-link {
font-size: 12px;
color: #444;
display: block;
margin-top: 12px;
}
And here's a screenshot of the web page
Thank you for any help, and if I need to clarify something please ask.