Jun-28-2024, 03:19 PM
Hi , hope all is good. I'm trying to put together a project to learn python and raspberry pi and have looked at counting how many times a button is pressed in an hour. Just now i'm just trying to out put to the screen , next to excel, then try to send via a pi to a network location.
The code i've got now i though worked , but now realise that there's a bug and cant seem to reset the minute properly.The program counts the number of button presses in an hour.
What i've noticed is that the last button for example if i press the button
say 6 times in minute 23, then dont register another button press for
say 3 mins, then the next button press is registered as minute 23 instead of minute
26. Could someone have a look please and see where the reset should be to close the count of that minute and be ready to count whenever another press occurs. Any help would be greatly appreciated for what is hopefully my new hobby. thanks
The code i've got now i though worked , but now realise that there's a bug and cant seem to reset the minute properly.The program counts the number of button presses in an hour.
What i've noticed is that the last button for example if i press the button
say 6 times in minute 23, then dont register another button press for
say 3 mins, then the next button press is registered as minute 23 instead of minute
26. Could someone have a look please and see where the reset should be to close the count of that minute and be ready to count whenever another press occurs. Any help would be greatly appreciated for what is hopefully my new hobby. thanks
import time # Initialize a dictionary to store button counts button_counts = {} # Initialize the current hour current_hour = None while True: # Get the current time now = time.localtime() hour = now.tm_hour mins = now.tm_min #added for mins print(mins) #added for mins # Check if a new min has started # if min != current_hour: added if mins != current_hour: # Print the counts for the previous hour if current_hour is not None: print(f"min {current_hour}:") #added to view minute for button, count in button_counts.items(): print(f"Button {button}: {count} presses") print("\n") # Reset counts for the new min button_counts = {} current_hour = mins # Simulate button press () button = input("Enter button number (1-5): ") if button.isdigit(): button = int(button) button_counts.setdefault(button, 0) button_counts[button] += 1 else: print("Invalid input. Please enter a number between 1 and 5.")