You are on page 1of 3

# Import necessary libraries

import RPi.GPIO as GPIO


import time
import board
import digitalio
import adafruit_ssd1306
from threading import Timer

# Set up GPIO pins for stepper motor control


STEP_PIN = 18
DIR_PIN = 23
CW = 1
CCW = 0

# Set up GPIO pins for sensor/button inputs


BUTTON_PIN = 24

# Set up GPIO pin for LED output


LED_PIN = 25

# Set up potentiometer pins


POT_PIN = 17

# Set up variables for distance and time


distance = 0
time_interval = 0

# Set up variables for stepper motor control


step_count = 0
direction = CW

# Initialize OLED display


spi = board.SPI()
oled_cs = digitalio.DigitalInOut(board.CE0)
oled_dc = digitalio.DigitalInOut(board.D24)
oled_reset = digitalio.DigitalInOut(board.D25)
oled = adafruit_ssd1306.SSD1306_SPI(128, 32, spi, oled_dc, oled_reset, oled_cs)
oled.fill(0)
oled.show()

# Define function to move stepper motor one step


def step():
GPIO.output(STEP_PIN, GPIO.HIGH)
time.sleep(0.0005)
GPIO.output(STEP_PIN, GPIO.LOW)
time.sleep(0.0005)

# Define function to move stepper motor a certain distance


def move_distance(distance):
global step_count, direction
step_count = int(distance * 1600 / 5)
for i in range(step_count):
step()
direction = not direction

# Define function to handle button press


def button_callback(channel):
global distance
if GPIO.input(BUTTON_PIN) == GPIO.HIGH:
distance = read_potentiometer()
move_distance(distance)
GPIO.output(LED_PIN, GPIO.HIGH)
update_display()

# Define function to read potentiometer


def read_potentiometer():
pot_value = GPIO.input(POT_PIN)
distance = pot_value * 5 / 1023 + 0.05
return distance

# Define function to handle reversing direction at end of travel


def reverse_direction(channel):
global direction
if channel == 'A':
direction = CW
elif channel == 'B':
direction = CCW

# Define function to start timer


def start_timer():
global time_interval
time_interval = read_potentiometer() * 60
t = Timer(time_interval, stop_grinding)
t.start()
update_display()

# Define function to stop grinding


def stop_grinding():
GPIO.output(LED_PIN, GPIO.LOW)
start_timer()

# Define function to update OLED display


def update_display():
oled.fill(0)
oled.text("Time: {:.1f}".format(time_interval), 0, 0)
oled.text("Distance: {:.2f}".format(distance), 0, 10)
oled.show()

# Set up GPIO pins


GPIO.setmode(GPIO.BCM)
GPIO.setup(STEP_PIN, GPIO.OUT)
GPIO.setup(DIR_PIN, GPIO.OUT)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(LED_PIN,

thebluepotatoyt@gmail.com
continue.
Set up GPIO pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(STEP_PIN, GPIO.OUT)
GPIO.setup(DIR_PIN, GPIO.OUT)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(POT_PIN, GPIO.IN)

Set up interrupt for button press


GPIO.add_event_detect(BUTTON_PIN, GPIO.RISING, callback=button_callback)

Set up interrupts for reversing direction at end of travel


GPIO.add_event_detect('A', GPIO.RISING, callback=reverse_direction)
GPIO.add_event_detect('B', GPIO.RISING, callback=reverse_direction)

Start timer
start_timer()

Main loop
while True:
# Change direction if at end of travel
if (direction == CW and GPIO.input('A') == GPIO.HIGH) or (direction == CCW and
GPIO.input('B') == GPIO.HIGH):
reverse_direction(direction)
update_display()

lua
Copy code
# Move stepper motor
if step_count > 0:
GPIO.output(DIR_PIN, direction)
step_count -= 1
else:
time.sleep(0.1)
GPIO.cleanup()

This code should display the time and distance values on an OLED display using the
Adafruit SSD1306 library. You will need to install this library before running the
code by running the following command in the terminal:

Copy code
sudo pip3 install adafruit-circuitpython-ssd1306

You might also like