Skip to content

Instantly share code, notes, and snippets.

@IdrisCytron
Last active July 12, 2019 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IdrisCytron/469f74c8504808707d2f96e14019cff1 to your computer and use it in GitHub Desktop.
Save IdrisCytron/469f74c8504808707d2f96e14019cff1 to your computer and use it in GitHub Desktop.
Control servo position and read distance sensor using GUI.
from gpiozero import Servo, DistanceSensor
from guizero import App, Box, Text, PushButton, Slider
from time import sleep
servo = Servo(17, 0, 0.0005, 0.0025)
sensor = DistanceSensor(echo=18, trigger=27)
def ServoPosition(slider_value):
servo.value = int(slider_value) / 90
def my_user_task():
print("Distance: {0:.2f}cm".format(sensor.distance * 100))
reading_text.value = "{0:.2f}".format(sensor.distance * 100)
app = App(title="Servo GUI", width=350, height=150, layout="auto")
instruction_text = Text(app, text="Drag slider below to control servo position.")
instruction_text.repeat(1000, my_user_task)
servo_position = Slider(app, command=ServoPosition, start=-90, end=90, width='fill')
distance_text = Text(app, text="Distance (cm):")
reading_text = Text(app, text="---")
designby_text = Text(app, text="Idris - Cytron Technologies", align='bottom')
app.display()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment