Python Forum
I think I need to delete input data because returning to start fails
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I think I need to delete input data because returning to start fails
#1
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
deanhystad write Sep-24-2024, 03:15 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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)
thelad likes this post
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Returning data on button click by buttons created by a loop bradells 3 385 Apr-23-2025, 03:01 PM
Last Post: Pedroski55
  Help with to check an Input list data with a data read from an external source sacharyya 3 1,688 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  manually input data jpatierno 0 787 Nov-10-2023, 02:32 AM
Last Post: jpatierno
  Returning Column and Row Data From Spreadsheet knight2000 0 1,108 Oct-22-2023, 07:07 AM
Last Post: knight2000
  Input network device connection info from data file edroche3rd 6 2,695 Oct-12-2023, 02:18 AM
Last Post: edroche3rd
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 2,540 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
Question in this code, I input Key_word, it can not find although all data was exact Help me! duchien04x4 3 1,982 Aug-31-2023, 05:36 PM
Last Post: deanhystad
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 3,152 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  Showing an empty chart, then input data via function kgall89 0 1,482 Jun-02-2022, 01:53 AM
Last Post: kgall89
Question Change elements of array based on position of input data Cola_Reb 6 3,484 May-13-2022, 12:57 PM
Last Post: Cola_Reb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020