简体   繁体   English

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

[英]Coin Counter in Raspberry Pi (RPi-GPIO)

I'm having a problem to count coins in raspberry pi B+ usin RPi-GPIO. 我有一个问题,计算覆盆子pi B +硬币在RPi-GPIO。

This is the code: 这是代码:

 import RPi.GPIO as GPIO

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

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

I called the manufacturer and he said the Coin Counter sends a pulse with value 1 constantly and the value 0 when the coin down. 我打电话给制造商,他说硬币计数器不断发送值为1的脉冲,当硬币输出时发送值为0。 But in the console is showing 0 and 1 at random. 但是在控制台中随机显示0和1。 And nothing change when coin dropped. 硬币掉落时没有任何改变。

4 wire outputs: 4线输出:

  • Black and Red: are 12v (font) 黑色和红色:是12v(字体)
  • White: is pulse (1 constantly, 0 when drop the coin). 白色:是脉冲(1不断,0投币时)。 It is plugged in raspberry on 23 pin (I choose this pin because is available). 它插在23针的树莓上(我选择这个引脚因为可用)。
  • Purple: is to count coins on separated led. 紫色:是指分开领导的硬币。

Voltage: Font 12v, and pulse (white wire) is 0.19~0.25 volts 电压:字体12v,脉冲(白线)为0.19~0.​​25伏

Note: Pulse is digital. 注意:脉冲是数字的。

I have this Coin Counter: 我有这个硬币计数器:

在此输入图像描述

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')
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 硬币

        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]

Weight 重量

                    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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM