Code:
# Import Required Librariesimport RPi.GPIO as GPIOimport time# define the GPIO pin connected to the buttonBUTTON_PIN = 16button_counts = {}# Initialiation and Setup# Perform one-time setup tasks hereGPIO.setmode(GPIO.BCM)# Define the GPIO pin for the ledLED_PIN = 17 # Use GPIO pin 17# Set up the LED Pin as an outputGPIO.setup(LED_PIN, GPIO.OUT)# Initialize the pushbutton pin as an input with a pull-up resistor# The pull-up input pin will be HIGH when the switch is open and Low when the switch is closed.GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)# Variable to keep track of the previous button stateprev_button_state = GPIO.input(BUTTON_PIN)try: # Main Loop while True: # Main code logic goes here # Turn on the LED GPIO.output(LED_PIN, GPIO.HIGH) # Read the state of the switch/button button_state = GPIO.input(BUTTON_PIN) # Check if the button state has changed (press or release event) if button_state != prev_button_state: if button_state == GPIO.LOW: # Button is pressed print("The button is pressed!") else: # Button is released print("The button is released!") # Update the previous button state prev_button_state = button_state if button_counts == 15: break while False: # Turn off the LED GPIO.output(LED_PIN, GPIO.LOW) except KeyboardInterrupt: # Handle Crtl+c interruption print ("\nExiting the program.") GPIO.cleanup() #Clean up the GPIO
Statistics: Posted by Walking4 — Fri Aug 16, 2024 5:08 pm