Hi
I try to convert the example in ARDUINO to Python for Raspberry and GrovePI
The code that I used is :
https://wiki.seeedstudio.com/Grove-Ear-clip_Heart_Rate_Sensor/
The result is :
import time
import grovepi
# Connect the Grove Ear Clip Sensor to digital port D2
sensorPin = 3
start_time = time.time()
counter = 0
temp = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
sub = 0
data_effect = True
heart_rate = 0
max_heartpulse_duty = 2000
print("Please place the sensor correctly")
time.sleep(4)
print("Starting measurement...")
def millis(): #MILLIS FUNCTION
print("Millis")
return (time.time() - start_time)
def arrayInit(): #ARRAY INIT
print("arrayInit")
i = 0
temp = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
temp[20] = millis()
def sum(): #SUM
print("sum")
global heart_rate
if data_effect:
heart_rate=1200000/(temp[20]-temp[0]);
print heart_rate
def interrupt():
global counter
global temp
global data_effect
global sub
print("Interrupt")
print(counter)
temp[counter] = millis()
if counter == 0:
sub = temp[counter]-temp[20]
else:
sub = temp[counter]-temp[counter-1]
if sub > max_heartpulse_duty:
data_effect = False
counter = 0
arrayInit()
if counter == 20 and data_effect:
counter = 0
sum()
elif counter != 20 and data_effect:
counter += 1
else:
counter = 0
data_effect = True
def main():
arrayInit()
grovepi.set_pin_interrupt(sensorPin, grovepi.COUNT_CHANGES, grovepi.RISING, 1000)
value = False
while True:
value = grovepi.read_interrupt_state(sensorPin)
if value == True:
interrupt()
time.sleep(.5)
print("Loop")
if __name__ == '__main__':
main()
the problem is that I have unlogical value as heart_rate. Like 83460.64433
according to me there is somenthing wrong…