Python Forum

Full Version: Loop triggered by button help
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I need help. I'm trying to make a loop, triggered by a button, that:

First press: Relay goes on and off intermittently
Second press: Turns relay off
I have a functional button that goes on and off without the intermittent function but when I add the loop in the definition it goes on forever and freezes the program.

I tried to stop it with break but it stops everytime the cycle is done.
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication,QDialog
from PyQt5.uic import loadUi
import RPi.GPIO as gpio
import recu
from time import sleep
import time


load1=22
load2=5
load3=26

gpio.setmode(gpio.BCM)
gpio.setwarnings(False)Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.



gpio.setup(22,gpio.OUT)
gpio.output(22,gpio.LOW)
gpio.setup(5,gpio.OUT)
gpio.output(5,gpio.LOW)
gpio.setup(26,gpio.OUT)
gpio.output(26,gpio.LOW)



class industrial(QDialog):
    def __init__(self):
        super(industrial,self).__init__()
        loadUi('neo.ui',self)
        self.setWindowTitle('Spectrum')
        self.load_1.clicked.connect(self.load1)
        self.load_2.clicked.connect(self.load2)
        self.load_3.clicked.connect(self.load3)




@pyqtSlot()
def load1(self):
    if gpio.input(load1):
        gpio.output(22,gpio.LOW)
        self.load_1.setText('633 nm Off')
    else:
        gpio.output(22,gpio.HIGH)
        self.load_1.setText('633 nm ON')

def load2(self):
    if gpio.input(load2):
        gpio.output(5,gpio.LOW)
        self.load_2.setText('Neo Push Off')
    else:
        gpio.output(5,gpio.HIGH)
        self.load_2.setText('Neo Push ON')

def load3(self):
    if gpio.input(load3):
        gpio.output(26,gpio.LOW)
        self.load_3.setText('Push Off')
    else:
        gpio.output(5,gpio.HIGH)
        self.load_3.setText('Push ON')

def load3(loop):
    while True:
          gpio.output(26,True)
          time.sleep(0.3)
          gpio.output(26,False)
          time.sleep(0.3) 




app=QApplication(sys.argv)
widget=industrial()
widget.show()
sys.exit(app.exec_())
Look at QTimer. You could run your relay toggle loop in it's own thread, but I think QTimer may be a better fit.