Advertisement
Guest User

Untitled

a guest
Nov 27th, 2023
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. import datetime
  2. import time
  3. import RPi.GPIO as GPIO
  4.  
  5. # use the Broadcom pin numbering scheme
  6. GPIO.setmode(GPIO.BCM)
  7.  
  8. # set up the GPIO pin to control the LED (replace X with the appropriate pin number)
  9. LED_PIN = 17
  10. GPIO.setup(LED_PIN, GPIO.OUT)
  11.  
  12. def set_led_state(state):
  13.     # Turn the LED on or off
  14.     GPIO.output(LED_PIN, state)
  15.  
  16. def read_last_number_from_file(filename):
  17.     # Read the last number from a file
  18.     # return the initial number (0001) if the file does not exist
  19.     try:
  20.         with open(filename, 'r') as f:
  21.             # read the last line of the file
  22.             last_line = f.readlines()[-1]
  23.             # split the line on the tab character
  24.             date_str, number_str = last_line.split('\t')
  25.             # return the number as an integer
  26.             return int(number_str)
  27.     except FileNotFoundError:
  28.         return 1
  29.  
  30. def write_number_to_file(filename, number_str, timestamp):
  31.     # Write a number and timestamp to a file
  32.     with open(filename, 'a') as f:
  33.         # write the number and timestamp to the file
  34.         f.write(f'{timestamp}\t{number_str}\n')
  35.  
  36. # read the last number from the file "number.txt"
  37. last_number = read_last_number_from_file("number.txt")
  38.  
  39. # iterate through the numbers from last_number + 1 to 9999
  40. for number in range(last_number + 1, 10000):
  41.     # flash the LED for 1 second before starting a new number and also blink shortly to start input
  42.     set_led_state(True)
  43.     time.sleep(0.6)
  44.     set_led_state(False)
  45.     time.sleep(0.5)
  46.     set_led_state(True)
  47.     time.sleep(0.6)
  48.     set_led_state(False)
  49.     time.sleep(0.6)
  50.     # convert the number to a string and pad it with leading zeros
  51.     number_str = format(number, '04d')
  52.  
  53.     # iterate through the digits of the number
  54.     for digit in number_str:
  55.         # convert the digit to an integer
  56.         digit_int = int(digit)
  57.  
  58.         # blink the LED for the appropriate number of times
  59.         for i in range(digit_int):
  60.             # turn the LED on
  61.             set_led_state(True)
  62.             # wait for 100ms
  63.             time.sleep(0.4)
  64.             # turn the LED off
  65.             set_led_state(False)
  66.             # wait for 100ms
  67.             time.sleep(0.4)
  68.  
  69.         # pause for 1 second between digits
  70.         time.sleep(3.0)
  71.  
  72.     # output the number to the console
  73.     print(number_str)
  74.     # write the number to the file "number.txt" with the current date and time
  75.     write_number_to_file("number.txt", number_str, datetime.datetime.now())
  76.  
  77. # clean up the GPIO resources
  78. GPIO.cleanup()
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement