#!/usr/bin/env python3 from time import sleep import threading try: import RPi.GPIO as GPIO except RuntimeError: print("Error importing RPi.GPIO! Run this script as root.") exit(1) class LED(Thread): def __init__(self, pin, frequency, use_pwm=False): Thread.__init__(self) self.__pin = pin self.__frequency self.__use_pwm = use_pwm self.__pwm = None GPIO.setup(pin, GPIO.OUT) if use_pwm: self.__pwm = GPIO.PWM(pin, frequency) def new_random_frequency(self): self.__frequency = random.randint(1, 100) def run(self): try: if self.__use_pwm: self.__pwm.start(0) while True: for s in range(0, 1, 0.1): self.__pwm.ChangeDutyCycle(math.sin(s * math.pi)) #sleep(1 / self.__frequency) sleep(1) else: while True: GPIO.output(LED_GREEn, GPIO.LOW) sleep(1 / self.__frequency) GPIO.output(LED_GREEn, GPIO.HIGH) sleep(1 / self.__frequency) except KeyboardInterrupt: GPIO.cleanup() def setup(): # Run GPIO with PI pin numbers. GPIO.setmode(GPIO.BCM) def main(): setup() # Pin, frequency in Hz led_thread = LED(17, 1000) led_thread.run() if __name__ == "__main__": main()