Last active
June 11, 2022 22:42
-
-
Save Br1an-Boyle/0c23d3cbcca62ce40a39a6e67252b0e6 to your computer and use it in GitHub Desktop.
Stepper Motor L298n
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
import sys | |
import time | |
GPIO.setmode(GPIO.BOARD) | |
flow_rate_map = dict([(1, 50), (2, 100), (3, 150), (4, 200), (5, 250)]) | |
control_pins = [15,13,11,12] | |
# Load the current flow rate from the local db.txt file | |
db = open("db.txt","r") | |
current_flow_rate = int(db.read().rstrip('\n')) | |
db.close() | |
print("Current Flow Rate: ", current_flow_rate) | |
selected_flow_rate = int((sys.argv)[1]) | |
print("Selected Flow Rate: ",selected_flow_rate) | |
# Exit if an invalid flow rate is entered | |
if selected_flow_rate < 1 or selected_flow_rate > 5: | |
print("Invalid value. Terminating!") | |
raise SystemExit | |
# Do nothing if the current flow are is the same as the selected flow rate | |
if selected_flow_rate == current_flow_rate: | |
print("!!Exiting!! Selected flow rate is already :", current_flow_rate) | |
raise SystemExit | |
# if selected_flow_rate > current_flow_rate: | |
step_value = flow_rate_map[selected_flow_rate] - flow_rate_map[current_flow_rate] | |
print("Step value: ",step_value) | |
# Reverse the control_pins array if we need to go anti-clockwise | |
if step_value < 0: | |
control_pins = control_pins[::-1] | |
for pin in control_pins: | |
GPIO.setup(pin, GPIO.OUT) | |
GPIO.output(pin, 0) | |
halfstep_seq = [ | |
[1,0,0,0], | |
[1,1,0,0], | |
[0,1,0,0], | |
[0,1,1,0], | |
[0,0,1,0], | |
[0,0,1,1], | |
[0,0,0,1], | |
[1,0,0,1] | |
] | |
for i in range(abs(step_value)): | |
for halfstep in range(8): | |
for pin in range(len(control_pins)): | |
GPIO.output(control_pins[pin], halfstep_seq[halfstep][pin]) | |
time.sleep(0.001) | |
GPIO.cleanup() | |
# Save the new flow rate | |
db = open("db.txt","w") | |
db.write(str(selected_flow_rate)) | |
db.close() | |
print("Current Flow Rate is now: ", selected_flow_rate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment