#IMPORT LIBRARIES AND INTERFACE--------------------
from QTpy import *
import serial
#INTERFACE CLASS--------------------
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
#ESTABLISH CONNECTION----------------------
self.serial = serial.Serial('COM3',9600)
#VARIABLES--------------------------------
self.command=0
self.speed1=600
self.speed2=600
self.fanvalue=0
self.ledvalue=0
self.x=0
#INTERACTIONS MOTOR 1---------------------------------
self.B1.clicked.connect(self.motor1_right)
self.B2.clicked.connect(self.motor1_left)
self.B3.clicked.connect(self.motor1_move)
self.slider1.valueChanged.connect(self.motor1_speed)
#INTERACTIONS MOTOR 2--------------------------------
self.B4.clicked.connect(self.motor2_right)
self.B5.clicked.connect(self.motor2_left)
self.B6.clicked.connect(self.motor2_move)
self.slider2.valueChanged.connect(self.motor2_speed)
#INTERACTIONS SPEED BUTTON---------------------------------
self.Bspeed.clicked.connect(self.send)
#INTERACTIONS FAN---------------------------------
self.fan.clicked.connect(self.fanstate)
#INTERACTIONS LED---------------------------------
self.BLED.clicked.connect(self.ledstate)
#FUNCTIONS-----------------------------------------
#DIRECTION MOTOR 1---------------------------------
def motor1_right(self):
self.command = 7
self.send()
self.command = 8 #Reset to neutral value
def motor1_left(self):
self.command = 2
self.send()
self.command = 8 #Reset to neutral value
#TOGGLE MOTOR 1 START/STOP---------------------------------
def motor1_move(self):
self.command = 5
self.send()
if self.B3.styleSheet() == "background-color: rgb(136, 4, 6);""font: 8pt 'Roboto';""border-radius: 10px;":
self.B3.setStyleSheet("background-color: rgb(8, 96, 211);""font: 8pt 'Roboto';""border-radius: 10px;")
self.B3.setText("START") #change button text to START
else:
self.B3.setText("STOP") #change button text to STOP
self.B3.setStyleSheet("background-color: rgb(136, 4, 6);""font: 8pt 'Roboto';""border-radius: 10px;")
self.command = 8 #Reset to neutral value
#CHANGE MOTOR 1 Speed---------------------------------
def motor1_speed(self):
self.speed1=self.slider1.value()
self.Slider1_value.display(self.speed1) #display on LCD speed value
#DIRECTION MOTOR 2---------------------------------
def motor2_right(self):
self.command = 3
self.send()
self.command = 8 #Reset to neutral value
def motor2_left(self):
self.command = 4
self.send()
self.command = 8 #Reset to neutral value
#TOGGLE MOTOR 2 START/STOP---------------------------------
def motor2_move(self):
self.command = 6
self.send()
if self.B6.styleSheet() == "background-color: rgb(136, 4, 6);""font: 8pt 'Roboto';""border-radius: 10px;":
self.B6.setText("START") #change button text to START
self.B6.setStyleSheet("background-color: rgb(8, 96, 211);""font: 8pt 'Roboto';""border-radius: 10px;")
else:
self.B6.setStyleSheet("background-color: rgb(136, 4, 6);""font: 8pt 'Roboto';""border-radius: 10px;")
self.B6.setText("STOP") #change button text to STOP
self.command = 8 #Reset to neutral value
#CHANGE MOTOR 2 Speed---------------------------------
def motor2_speed(self):
self.speed2=self.slider2.value()
self.Slider1_value_2.display(self.speed2) #display on LCD speed value
#TOGGLE ON/OFF FAN---------------------------------
def fanstate(self):
self.fanvalue = 1
self.send()
self.fanvalue = 0 #Reset to neutral value
if self.fan.styleSheet() == "background-color: rgb(136, 4, 6);""font: 8pt 'Roboto';""border-radius: 10px;":
self.fan.setStyleSheet("background-color: rgb(8, 96, 211);""font: 8pt 'Roboto';""border-radius: 10px;")
self.fan.setText("ON")
else:
self.fan.setText("OFF")
self.fan.setStyleSheet("background-color: rgb(136, 4, 6);""font: 8pt 'Roboto';""border-radius: 10px;")
#TOGGLE ON/OFF LED---------------------------------
def ledstate(self):
self.ledvalue = 1
self.send()
self.ledvalue = 0 #Reset to neutral value
if self.BLED.styleSheet() == "background-color: rgb(136, 4, 6);""font: 8pt 'Roboto';""border-radius: 10px;":
self.BLED.setStyleSheet("background-color: rgb(8, 96, 211);""font: 8pt 'Roboto';""border-radius: 10px;")
self.BLED.setText("ON") #change button text to ON
else:
self.BLED.setText("OFF") #change button text to OFF
self.BLED.setStyleSheet("background-color: rgb(136, 4, 6);""font: 8pt 'Roboto';""border-radius: 10px;")
self.leer()
#SEND VARIABLES THROUGH SERIAL---------------------------------
def send(self):
data=f"{self.command},{self.speed1},{self.speed2},{self.fanvalue},{self.ledvalue}"
encoded_data = data.encode()
print(encoded_data.decode())
self.serial.write(encoded_data)
#READ SERIAL TO INDICATE IF EXTRUDER ON OR OFF-----------------
def leer(self):
self.x=self.serial.readline()
self.TEM.display(int(self.x)) #change LCD Screen
#APPLICATION START-------------------
if __name__ == '__main__':
app = QtWidgets.QApplication([])
window = MainWindow()
window.show()
app.exec()