import machine import Pin, SoftI2C
import ssd1306
import time
# Inisialisasi pin untuk motor stepper
step_pin = machine.Pin(2, machine.Pin.OUT)
dir_pin = machine.Pin(3, machine.Pin.OUT)
enable_pin = machine.Pin(4, machine.Pin.OUT)
# Inisialisasi pin untuk OLED display (contoh: SDA=Pin 4, SCL=Pin 5)
i2c = SoftI2C(scl=Pin(5), sda=Pin(4))
oled = ssd1306.SSD1306_I2C(128, 64, i2c)
# Inisialisasi variabel untuk mengatur kecepatan dan arah motor
step_delay = 0.001 # Delay antara setiap langkah motor (dalam detik)
direction = 1 # Arah putaran motor (1 untuk searah jarum jam, -1 untuk berlawanan arah jarum jam)
# Fungsi untuk menggerakkan motor stepper
def move_stepper(steps, direction):
enable_motor(True) # Enable the motor driver
dir_pin.value(direction) # Set motor direction
for _ in range(steps):
step_pin.on()
time.sleep_us(500)
step_pin.off()
time.sleep_us(500)
enable_motor(False) # Disable the motor driver after movement
# Fungsi untuk menampilkan teks di OLED display
def display_text(text):
oled.fill(0) # Menghapus layar OLED
oled.text(text, 0, 0)
oled.show()
# Loop utama program
while True:
try:
# Membaca perintah dari serial input
oled.text("Motor Status:", 0, 0)
oled.text("Kelompok 6.", 25, 50)
oled.show()
command = input("Enter command (forward/backward): ").strip()
if command == "forward":
direction = 1
steps = 200 # Number of steps to move
move_stepper(steps, direction)
display_text("Moving Forward")
elif command == "backward":
direction = 0
steps = 200 # Number of steps to move
move_stepper(steps, direction)
display_text("Moving Backward")
else:
display_text("Unknown Command")
# Tangani kesalahan jika ada
except Exception as e:
display_text("Error: {}".format(str(e)))