Raspberry Pi中的硬币计数器(RPi-GPIO)

时间:2015-10-08 20:15:12

标签: python python-3.x raspberry-pi gpio

我在使用覆盆子pi B +中使用RPi-GPIO计算硬币时遇到问题。

这是代码:

 import RPi.GPIO as GPIO

 GPIO.setmode(GPIO.BCM)
 GPIO.setup(23, GPIO.IN)

 while True:
    print(GPIO.input(23))

我打电话给制造商,他说硬币计数器不断发送一个值为1的脉冲,当硬币输出时发送值为0。但是在控制台中随机显示0和1。硬币掉落时没有任何改变。

4线输出:

  • 黑色和红色:是12v(字体)
  • 怀特:是脉搏(1 经常,0投币时)。它插在23针的树莓上(我选择这个引脚因为可用)。
  • 紫色:是数钱币 分开领导。

电压: 字体12v和脉冲(白线)为0.19~0.​​25伏

注意:脉冲是数字的。

我有这个硬币计数器:

enter image description here

2 个答案:

答案 0 :(得分:1)

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
counterPin=23
GPIO.setup(counterPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    input_state = GPIO.input(counterPin)
    if input_state == False:
        print('coin dropped')

答案 1 :(得分:0)

again=True

Total=0

Wrongs=0

Corrects=0


Total_bags=0

Wrong_bags=0

Correct_bags=0

coins=['1p','2p','5p','10p','20p','50p','£1','£2']

bag_value=[1.00,1.00,5.00,5.00,10.00,10.00,20.00,20.00]

bag_amount=[100,50,100,50,50,20,20,10]

bag_weight=[3.56,7.12,3.25,6.5,5.0,8.0,8.75,12.0]

coins_len=len(coins)

Master_list=list()

CoinCount_list=list()

Tilte_list=['Name','Coin','Weight','Accurate']

Master_list.append(Tilte_list)

with open("Master.txt","w") as f: 
    f.write("{}".format(Master_list))

while True:

    again=True


    what=input("\nMenu\n1)\tAdd a bag\n2)\tStats\n")

    CoinCount_list=list()
    if what=='1':

        name=input("What is your name\n")

        CoinCount_list.append(name)

        with open("CoinCount.txt","w") as f: 
            f.write("{}".format(CoinCount_list))


        with open("CoinCount.txt","w") as f: 
            f.write("{}".format(CoinCount_list))

        Percent=0

        Wrongs=0

        Corrects=0 


while again:

硬币

        coin_input=input("What type of coin do you have\n")
        for i in range (0,coins_len):
            if coin_input==coins[i]:

                Coin=coins[i]
                valid=input("That is valid, is that what you wanted\n")

                if valid=="Yes":


                    CoinCount_list.append(coins[i])

                    with open("CoinCount.txt","w") as f: 
                        f.write("{}".format(CoinCount_list))

                    while True:
                        try:    
                            weight=int(input("What is the weight of the bag\n"))
                            break
                        except ValueError:
                            print("Oops!  That was no valid number.  Try again...\n")
                    amountCoin=weight/bag_weight[i]

重量

                    CoinCount_list.append(weight)

                    with open("CoinCount.txt","w") as f: 
                        f.write("{}".format(CoinCount_list))

                        if bag_amount[i] == amountCoin:

                            Total_bags=Total_bags+1

                            again=input("Thank you would you like to add another bag\n")

                            CoinCount_list.append('Correct')

                            with open("CoinCount.txt","w") as f: 
                                f.write("{}".format(CoinCount_list))

                            Total=bag_value[i]+Total
                            Correct_bags=Correct_bags+1

                            Corrects=Corrects+1

                            if again== 'Yes':
                                a='b'



                            else:
                                again=False


                                Master_list.append(CoinCount_list)

                                with open("Master.txt","w") as f: 
                                    f.write("{}".format(Master_list))







                        elif bag_amount[i] > amountCoin:
                            print("You have ",amountCoin,"coins, add ",amountCoin-bag_amount[i], "coin\n")

                            Wrong_bags=Wrong_bags+1
                            Total_bags=Total_bags+1

                            Wrongs=Wrongs+1



                        elif bag_amount[i] <amountCoin:
                            print("You have ",amountCoin,"coins, take away ",amountCoin-bag_amount[i], " coin\n")

                            Wrong_bags=Wrong_bags+1
                            Total_bags=Total_bags+1

                            Wrongs=Wrongs+1



if what=='2':
        print("")
        print("  We have check",Total_bags," ")
        print(" ",Correct_bags,"Correctly")
        print(" ",Wrong_bags,"Incorrectly")
        print("  We have raised £",Total," ")
        print("")
        with open("Master.txt") as f:
           rd=f.readlines()
        #print (rd)

        print(*Master_list, sep='\n')

        f = open("CoinCount.txt", "w")
        f.write( str(Master_list) ) 
        f.close()