計數器

最後編輯:2016-01-04 建立:2015-12-31 歷史紀錄

 

118 :"108"

 

按鈕按一下七段顯示器的數字就會加一

從零開始慢慢加最多可以加到9999

 

  • import RPi.GPIO as GPIO
  • import time
  • GPIO.setmode(GPIO.BCM)
  • GPIO.setwarnings(False)
  • GPIO.setup(2, GPIO.IN)
  • segments = (5, 9, 17, 13, 19, 11, 4, 6)
  • for segment in segments:
  • GPIO.setup(segment, GPIO.OUT)
  • GPIO.output(segment, 0)
  • digits = (26, 10, 27, 22)
  • for digit in digits:
  • GPIO.setup(digit, GPIO.OUT)
  • GPIO.output(digit, 1)
  • #word =['0','0','0','0']
  • #number = [0,0,0,0]
  • dictionary = {' ':(0,0,0,0,0,0,0),
  • '0':(1,1,1,1,1,1,0),
  • '1':(0,1,1,0,0,0,0),
  • '2':(1,1,0,1,1,0,1),
  • '3':(1,1,1,1,0,0,1),
  • '4':(0,1,1,0,0,1,1),
  • '5':(1,0,1,1,0,1,1),
  • '6':(1,0,1,1,1,1,1),
  • '7':(1,1,1,0,0,0,0),
  • '8':(1,1,1,1,1,1,1),
  • '9':(1,1,1,1,0,1,1),}
  • count=0
  • push=0
  • while True:
  • if GPIO.input(2):
  • push+=1
  • time.sleep(0.001)
  • if GPIO.input(2)==False and push>0:
  • count+=1
  • push=0
  • word=str(count)
  • while len(word)<4:
  • word='0'+word
  • for n in range(4):
  • for digit in digits: # all digits off
  • GPIO.output(digit, 1)
  • GPIO.output(digits[n], 0)
  • i = 0
  • for on_or_off in dictionary[word[n]]:
  • GPIO.output(segments[i], on_or_off)
  • i += 1
  • time.sleep(0.001)
  • GPIO.cleanup()