jakerthegamer3
Posts: 12
Joined: Sun Mar 04, 2018 6:50 pm

can some one make this have 2 sliders

Wed Mar 07, 2018 10:25 pm

can some one make this have 2 sliders

what this does i show a slider on screen to move a servo but it only shows one i want 2 for 2 servos

Code: Select all

from Tkinter import *
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
pwm = GPIO.PWM(18, 100)
pwm.start(5)

class App:

    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        scale = Scale(frame, from_=0, to=180,
              orient=HORIZONTAL, command=self.update)
        scale.grid(row=0)


    def update(self, angle):
        duty = float(angle) / 10.0 + 2.5
        pwm.ChangeDutyCycle(duty)

root = Tk()
root.wm_title('Servo Control')
app = App(root)
root.geometry("200x50+0+0")
root.mainloop()

scotty101
Posts: 3958
Joined: Fri Jun 08, 2012 6:03 pm

Re: help me with this problem i have

Thu Mar 08, 2018 9:57 am

Not very difficult. A simple hack to your existing code made below. There are much neater ways to do it but this should serve your purposes.
You'll just need to change the SERVO2 to be the number of the GPIO pin your second servo is connected to.

Code: Select all

#https://www.raspberrypi.org/forums/viewtopic.php?f=32&t=207318
from tkinter import *
#import RPi.GPIO as GPIO
import time

SERVO1 = 18
SERVO2 = 18 #CHANGE ME!

GPIO.setmode(GPIO.BCM)
GPIO.setup(SERVO1, GPIO.OUT)
pwm1 = GPIO.PWM(SERVO1, 100)
pwm1.start(5)
GPIO.setup(SERVO2, GPIO.OUT)
pwm2 = GPIO.PWM(SERVO2, 100)
pwm2.start(5)

class App:

    def __init__(self, master):
        frame = Frame(master)
        frame.grid()
        scale1 = Scale(frame, from_=0, to=180,
              orient=HORIZONTAL, command=self.updatePWM1)
        scale1.grid(column=0,row=1)
        scale2 = Scale(frame, from_=0, to=180,
              orient=HORIZONTAL, command=self.updatePWM2)
        scale2.grid(column=1,row=1)


    def updatePWM1(self, angle):
        duty = float(angle) / 10.0 + 2.5
        print("PWM1: {}".format(duty))
        pwm1.ChangeDutyCycle(duty)

    def updatePWM2(self, angle):
        duty = float(angle) / 10.0 + 2.5
        print("PWM2: {}".format(duty))
        pwm2.ChangeDutyCycle(duty)

root = Tk()
root.wm_title('Servo Control')
app = App(root)
root.geometry("200x50+0+0")
root.mainloop()
(Note: I haven't tested this with the GPIO code on a raspberry pi nor have i tested it with a servo)
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

jakerthegamer3
Posts: 12
Joined: Sun Mar 04, 2018 6:50 pm

Re: help me with this problem i have

Thu Mar 08, 2018 4:39 pm

thanks a lot

Return to “Python”