from machine import Pin
import time
# Define pins connected to the 74HC595
data_pin = Pin(25, Pin.OUT) # DS --> Serial Input
clock_pin = Pin(13, Pin.OUT) # SHCP --> Serial Clock
latch_pin = Pin(26, Pin.OUT) # STCP --> latch
# Define pin connected to the button
button_pin = Pin(23, Pin.IN) # Button connected to pin 14
# Initial state of Q1
q1_state = 0
def shift_out(value):
for i in range(8):
data_pin.value((value >> i) & 1)
clock_pin.value(1)
time.sleep_us(1)
clock_pin.value(0)
def update_shift_register(value):
latch_pin.value(0)
shift_out(value)
latch_pin.value(1)
# Function to toggle Q1
'''
def toggle_q1():
global q1_state
q1_state = 1 - q1_state # Toggle between 0 and 1
update_shift_register(q1_state << 1) # Shift Q1 state to the correct bit position
'''
# Function to toggle Q1
def toggle_q1():
global q1_state
q1_state = 1 - q1_state # Toggle between 0 and 1
# Create a byte with only Q1 affected
value = q1_state << 1
update_shift_register(value)
# Example usage
last_button_state = 1 # Assume button is not pressed initially
while True:
current_button_state = button_pin.value()
print(f'Status before : {current_button_state}')
if last_button_state == 1 and current_button_state == 0:
# Button was pressed
toggle_q1()
time.sleep(0.2) # Debounce delay
last_button_state = current_button_state
print(f'Status after : {last_button_state}')
time.sleep(0.01) # Small delay to avoid bouncing issues