I am trying to make an animation that uses heart rate date as the trigger using raspberry pi.
I have used github code https://github.com/tutRPi/Raspberry-Pi-Heartbeat-Pulse-Sensor and am using the example.py
as a basis for my work.
I have also used a great tutorial that makes balls move around the screen using tkinter which works.
I am totally new to python and am having trouble getting the animation to play on the if statement. Ideally I want the balls to move at speed generated by the bmp data, so that faster heart rates will make them move around faster. I seem to be very far from this at the moment. If anyone can help to get this working I will be forever grateful. as it stands, the balls appear but do not move. I think there is a conflict with the movement and the bpm updating...
Here is the code:
from pulsesensor import Pulsesensor
import time
from tkinter import *
import random
tk = Tk()
WIDTH=1500
HEIGHT=800
canvas = Canvas(tk, bg="brown4", height=HEIGHT, width= WIDTH)
tk.title("drawing")
canvas.pack()
##below is the class to create multiple balls that are coloured
##and move and detect the edge and bounce
class Ball:
def __init__(self, color, size):
self.shape = canvas.create_oval (10, 10, size, size, fill=color,
outline=color, stipple="gray25")
self.xspeed = random.randrange(-1,5)
self.yspeed = random.randrange(-1,5)
def move(self):
canvas.move(self.shape, self.xspeed, self.yspeed)
pos = canvas.coords(self.shape)
if pos[3]>=HEIGHT or pos[1]<=0:
self.yspeed=-self.yspeed
if pos[2] >=WIDTH or pos[0] <=0:
self.xspeed=-self.xspeed
colors=["red4", "red3", "OrangeRed2","OrangeRed4","firebrick3"]
##this is make 100 balls
balls=[]
##this is to set the colour and size of the balls which is randomised:
for i in range (100):
balls.append(Ball(random.choice(colors), random.randrange(150, 200)))
##this is to call the balls
##while True:
p = Pulsesensor()
p.startAsyncBPM()
try:
while True:
bpm = p.BPM
if bpm > 0:
print("BPM: %d" % bpm)
for ball in balls:
ball.move()
tk.update()
time.sleep(0.02)
tk.mainloop()
else:
print("No Heartbeat found")
time.sleep(1)
except:
p.stopAsyncBPM()