Hello,
I am trying to create a sketch that uses from a json file to r value to change colour. In the terminal, it shows the value changing, but the colour are not changing. Here is code:
import json
def setup():
size(720,480)
global jsondata, heart
jsondata = open('heart_rate-2020-09-11.json')
heart = json.load(jsondata)
def draw():
for beat in heart:
bpm = beat['value']['bpm']
print(bpm)
fill(bpm, 0, 0)
circle(width/2, height/2, 200)
I realise why it was not changing. It was making a circle for each value and putting them all on top of each other. How do I enable it to read through each value one at a time?
1 Like
Welcome, @fleshcircuit
Yes – every circle is drawn over the last one. You only see the final circle drawn on each frame.
You want to draw one circle per frame; you’re drawing several each frame. I don’t know what your JSON data looks like, so I’ve created some placeholder dictionary values. I’m using a modulo operation with the frameCount to pick row values:
def setup():
frameRate(3) # slow down frame rate to see what's happening
size(720,480)
global jsondata, heart
heart = [
{'value': {'bpm': 0}},
{'value': {'bpm': 100}},
{'value': {'bpm': 200}},
]
def draw():
background(255, 255, 255) # clear previous frame
heart_total = len(heart) # total entries in heart data
heart_row = frameCount % heart_total # pick a row
bpm = heart[heart_row]['value']['bpm']
print(bpm)
fill(bpm, 0, 0)
circle(width/2, height/2, 200)
The heart_row
is equal to 1
on the first frame, 2
on the second. Then it ‘wraps back around’ to 0
again for the third frame, and so on.
3 Likes
Thank you! I will try it out.