now the servo will not move.
Code:
import tkinter as tkfrom flask import Flask, render_template, request, redirect, url_forimport sqlite3import timeimport datetimeimport boardimport busiofrom adafruit_pca9685 import PCA9685app_flask = Flask(__name__)DATABASE = 'pill_dispenser.db'# Initialize the PCA9685 objecti2c = busio.I2C(board.SCL, board.SDA)pca = PCA9685(i2c)# Set frequency for the PWM signal (typically 50Hz for servos)pca.frequency = 50# Define servo minimum and maximum pulse widths (adjust according to your servo's specifications)servo_min = 150servo_max = 600# Initialize the databasedef initialize_database(): conn = sqlite3.connect(DATABASE) c = conn.cursor() c.execute('''CREATE TABLE IF NOT EXISTS pills (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, hour INTEGER, am_pm TEXT, bin_number INTEGER)''') conn.commit() conn.close()# Add a new pill to the scheduledef add_pill_to_schedule(name, hour, am_pm, bin_number): if len(get_all_pills()) >= 30: raise ValueError("Maximum limit of 30 pills reached.") else: conn = sqlite3.connect(DATABASE) c = conn.cursor() c.execute("INSERT INTO pills (name, hour, am_pm, bin_number) VALUES (?, ?, ?, ?)", (name, hour, am_pm, bin_number)) conn.commit() conn.close()# Get all pills from the scheduledef get_all_pills(): conn = sqlite3.connect(DATABASE) c = conn.cursor() c.execute("SELECT * FROM pills") pills = c.fetchall() conn.close() return pills# Get pills scheduled at the current timedef get_pills_at_schedule(current_time): conn = sqlite3.connect(DATABASE) c = conn.cursor() c.execute("SELECT * FROM pills WHERE hour=? AND am_pm=?", current_time) pills = c.fetchall() conn.close() return pills# Delete a pill from the scheduledef delete_pill_from_schedule(pill_id): conn = sqlite3.connect(DATABASE) c = conn.cursor() c.execute("DELETE FROM pills WHERE id=?", (pill_id,)) conn.commit() conn.close()# Get a pill by its IDdef get_pill_by_id(pill_id): conn = sqlite3.connect(DATABASE) c = conn.cursor() c.execute("SELECT * FROM pills WHERE id=?", (pill_id,)) pill = c.fetchone() conn.close() return pill# Update a pill in the scheduledef update_pill_in_schedule(pill_id, name, hour, am_pm, bin_number): conn = sqlite3.connect(DATABASE) c = conn.cursor() c.execute("UPDATE pills SET name=?, hour=?, am_pm=?, bin_number=? WHERE id=?", (name, hour, am_pm, bin_number, pill_id)) conn.commit() conn.close()# Function to stop the servodef stop_servo(channel): try: pca.channels[channel].duty_cycle = 0 # Set duty cycle to 0 to stop the servo except Exception as e: print("Error stopping servo:", e)# Function to set servo pulsedef set_servo_pulse(channel, pulse_width): try: pca.channels[channel].duty_cycle = int(pulse_width * 65535 / 100) # Convert pulse width to a value between 0 and 65535 except Exception as e: print("Error setting servo pulse:", e)# Function to return servo to position 0def return_to_zero(channel): set_servo_pulse(channel, 0) # Set servo pulse width to 0 to return to position 0current_pill_index = 0# Flask routes@app_flask.route('/')def index(): return render_template('index.html')@app_flask.route('/dispense')def dispense_pills(): global current_pill_index try: pills = get_all_pills() if current_pill_index < len(pills): # Get the next pill from the list pill = pills[current_pill_index] current_pill_index += 1 # Code to activate the pill dispensing mechanism # Move servo 0 to a specific position (e.g., 180-degree position) set_servo_pulse(0, 50) # Move servo to position (adjust percentage as needed) time.sleep(1) # Wait for 1 second (adjust as needed) # Return servo to position 0 return_to_zero(0) time.sleep(2) # Wait for 2 seconds before allowing another dispensing return f"Pill {pill[0]} ({pill[1]}) dispensed and servo reset to 0" else: return "No more pills to dispense" except Exception as e: print("Error in dispense_pills route:", e) return "An error occurred while dispensing pills and resetting servo"@app_flask.route('/schedule')def view_schedule(): try: pills = get_all_pills() return render_template('schedule.html', pills=pills) except Exception as e: print("Error in view_schedule route:", e) return "An error occurred while viewing schedule"@app_flask.route('/add_pill', methods=['GET', 'POST'])def add_pill(): if request.method == 'POST': try: name = request.form['name'] hour = int(request.form['hour']) am_pm = request.form['am_pm'] bin_number = int(request.form['bin_number']) # Assuming bin_number is an integer add_pill_to_schedule(name, hour, am_pm, bin_number) return redirect(url_for('view_schedule')) except ValueError as e: return render_template('error.html', error=str(e)) except Exception as e: print("Error in add_pill route:", e) return "An error occurred while adding pill" else: return render_template('add_pill.html')if __name__ == '__main__': app_flask.run(host='0.0.0.0', port=5000)
Statistics: Posted by bob5731 — Wed Apr 10, 2024 12:51 am — Replies 0 — Views 31