Here is a sample Python(2.7) script that may work:
import RPi.GPIO as g
from time import sleep
g.setmode(g.BCM)
g.setup(2, g.IN)
global revcount
revcount = 0
def increaserev(channel):
global revcount
revcount += 1
g.add_event_detect(2, g.RISING, callback=increaserev)
while True:
sleep(60)
print "RPM is {0}".format(revcount)
revcount = 0
What this does is setup event detection on channel 2, which has a physical pull-up resistor. Anytime that pin detects a change from LOW to HIGH, it will increase the revcount, then print the current RPM each minute, resetting the counter each time.
Answer from user32264 on Stack Exchangepython - Coin Counter in Raspberry Pi (RPi-GPIO) - Stack Overflow
python - PIR counter for Raspberry Pi - Stack Overflow
Gpio pulse counter pi3 - Raspberry Pi Forums
python - Update counter for GUI, for every high signal from GPIO - Stack Overflow
Videos
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()
The RPi.GPIO Python library now supports Events, which are explained in the Interrupts and Edge detection paragraph.
So after updating your Raspberry Pi with sudo rpi-update to get the latest version of the library, you can change your code to:
from time import sleep
import RPi.GPIO as GPIO
var=1
counter = 0
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def my_callback(channel):
if var == 1:
sleep(1.5) # confirm the movement by waiting 1.5 sec
if GPIO.input(7): # and check again the input
print("Movement!")
captureImage()
# stop detection for 20 sec
GPIO.remove_event_detect(7)
sleep(20)
GPIO.add_event_detect(7, GPIO.RISING, callback=my_callback, bouncetime=300)
GPIO.add_event_detect(7, GPIO.RISING, callback=my_callback, bouncetime=300)
# you can continue doing other stuff here
while True:
pass
I chose the Threaded callbacks method because I suppose that your program does some other things in parallel to change the value of var.
Now the RPi GPIO library has inbuilt interrupt driven GPIO control which can happen in separate thread freeing up resources. You may wish to read the following http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-3