Press 0 (GPIO23), 1 (GPIO24), 2 (GPIO25) or 3 (GPIO26) to pulse that output for 200 milliseconds, r for random(ish) pulses on all, CTRL-C to exit. Setting GPIO23 high for 200 milliseconds. Cleaning up... rob@MotherHen:~ $ cat eggs.py import pigpio import sys import tty import termios import select from random import randrange from time import sleep # The pulse duration PULSE_DURATION_MILLISECONDS = 200 # Get keyboard input: for non-blocking input, specify a timeout value def getch(timeout_seconds=None): fd = sys.stdin.fileno() saved_settings = termios.tcgetattr(fd) try: # Switch echo off new_settings = saved_settings[:] new_settings[3] &= ~termios.ECHO termios.tcsetattr(fd, termios.TCSADRAIN, new_settings) # Set unbuffered tty.setraw(fd) x = None if timeout_seconds is None or select.select([sys.stdin], [], [], timeout_seconds)[0]: x = sys.stdin.read(1) # Check for arrow keys (start with 0x1b) if x == '\x1b': # Read two more characters sys.stdin.read(1) # Discard the '[' x = sys.stdin.read(1) # Raise SIGINT if CTRL-C (ASCII 0x03) elif x == '\x03': termios.tcsetattr(fd, termios.TCSADRAIN, saved_settings) raise KeyboardInterrupt # Won't get to this line finally: termios.tcsetattr(fd, termios.TCSADRAIN, saved_settings) # Flush anything else termios.tcflush(fd, termios.TCIFLUSH) return x # Crack an egg def crack_egg(pi, gpio, pulse_duration_milliseconds): print(f"Setting GPIO{gpio:02d} high for {pulse_duration_milliseconds} milliseconds.") pi.write(gpio, 1) sleep(pulse_duration_milliseconds / 1000) pi.write(gpio, 0) # Randomly crack eggs def crack_egg_random(pi, gpio_list, pulse_duration_milliseconds): count_seconds = 0 wait_seconds = 0 start_seconds = 0 gpio_index = None print("Cracking eggs randomly, press another valid key to return to normal.") while True: key = getch(0.1) if key: break else: count_seconds += 0.1 elapsed_seconds = count_seconds - start_seconds if elapsed_seconds > wait_seconds: if gpio_index is not None: crack_egg(pi, gpio_list[gpio_index], pulse_duration_milliseconds) wait_seconds = randrange(5, 30, 1) start_seconds = count_seconds gpio_index = randrange(0, len(gpio_list) - 1, 1) print(f"Waiting {wait_seconds} second(s) to pulse egg {gpio_index} (GPIO{gpio_list[gpio_index]:02d})...") print("Back to normal key-presses now") return key # Clean-up: after this pi is stopped def cleanup(pi): print ("Cleaning up...") if pi and pi.connected: pi.stop() # List of eggs, where the numbers are the GPIOs (i.e. GPIOXX) they are connected to egg_list = [23, 24, 25, 26] # Connect to pigpiod pi = pigpio.pi() if pi.connected: try: # Set all of the GPIOs for egg in egg_list: pi.write(egg, 0) pi.set_mode(egg, pigpio.OUTPUT) # Wait for keyboard input print(f"Press 0 (GPIO{egg_list[0]:02d}), 1 (GPIO{egg_list[1]:02d}), 2 (GPIO{egg_list[2]:02d}) or 3 (GPIO{egg_list[3]:02d}) to pulse that output for {PULSE_DURATION_MILLISECONDS} milliseconds, r for random(ish) pulses on all, CTRL-C to exit.") key = None while True: if key is None: key = getch() if key: if key.lower() == 'r': key = crack_egg_random(pi, egg_list, PULSE_DURATION_MILLISECONDS) else: gpio = None try: gpio = int(key) except ValueError: pass if gpio is not None and gpio >= 0 and gpio < len(egg_list): crack_egg(pi, egg_list[gpio], PULSE_DURATION_MILLISECONDS) else: print(f"Only CTRL-C, 0 to {len(egg_list) - 1} or r are valid.") key = None cleanup(pi) except KeyboardInterrupt as ex: cleanup(pi) else: print("pigpiod is not running: sudo pigpiod please!")