Python Forum

Full Version: I think I need to delete input data because returning to start fails
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
my messy and badly written code is to activate a 6 channel relay to open solenoids via the raspberry pi on my display cabinet.
it works, sort of. Iff the correct code is entered it opens and returns back to input question but when the correct code is entered again the program ends. I was wondering if maybe the input needed to be cleared before it asks again.
Hope your eyes don't bleed reading it but it was my first attempt at python code Big Grin
import RPi.GPIO as GPIO  # Import GPIO Module
import time # just in case
import os # just in case
from time import sleep  # Import sleep Module for timing
GPIO.setmode(GPIO.BCM)  # Configures how we are describing our pin numbering
GPIO.setwarnings(False)  # Disable Warnings
OutputPins = [2, 3, 4, 17, 22, 27]  # Set the GPIO pins that are required
def start(): # create input and correct entry code
    print("")
    lock = input("Enter The Unlock Code: ")
    unlock = ["007"] # Set unlock code

    while lock not in unlock: # Check for correct input
       lock = input("Enter The Unlock Code: ")

start()

# Activate pins and relay
for i in OutputPins:
         GPIO.setup(i, GPIO.OUT)
         GPIO.output(i, False)
# Switch relay off
time.sleep(10)
for i in OutputPins:

    GPIO.setup(i, GPIO.OUT)
    GPIO.output(i, True)


start() # return to input question
Your program ends because it has reached the end. Calling start() does not return your program to the start, it just executes the code in the start() function. You call start() twice, so it executes twice. If you called it three times it executes three times. If you want it to execute many times you should call it from inside a loop.

Also note that calling start() does not toggle the outputs on and off. Your program only does that once. The code for toggling the outputs must be called each time you enter the correct code.
import RPi.GPIO as GPIO
import time


unlock_codes = ["007"]
output_pins = [2, 3, 4, 17, 22, 27]


def start():
    while input("Enter The Unlock Code: ") not in unlock_codes:
         ...


GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
for i in output_pins:
    GPIO.setup(i, GPIO.OUT)

while True:
    start()
    for i in output_pins:
        GPIO.output(i, False)
    time.sleep(10)
    for i in output_pins:
        GPIO.output(i, True)
That is perfect Big Grin
Time to get back to studying and lean the commands you used to stream line it and get it working.
your help was much appreciated