Toymaker Television

Daily content for the geek and DIYer

65 notes &

Coding a Heartbeat Part II

Goal: To code a heartbeat (audio/visual) that increases in rate and volume as you “get closer” and have it decrease in rate and volume as you “back away”

Step 3

We need to be able to connect what RCTime/PST is showing the photoresistor as seeing and connect that to how the LEDs on the demo board react.

So the main new thing is setting boundaries/limits on the variable RCValue.  From the last article, we saw that the range of numbers the chip was given from RC time was from ~0 to ~45000. 

  • So we had to watch out for any 0’s that may happen from the different equations we’ve got going on, so we just made it so that anything less than 1 would equal 1 and anything greater than 44000 would equal 44000.  In other words, artificially setting limits.

In previous coding experiments, I would have just used RCValue as a multiplier for determining how long the LED stayed on, but with numbers like 40,000 it’s just easier to break that down further. 

So we made another variable called Multiplier.  And this variable was equal to:

  • ((The max value: 44000) - (RCValue)) / 10
  • OR
  • (44000 - RCValue) / 10

So for:

  • No Light = High RCTime (I’ll choose 43k) ==> 44000-43000 / 10 = Small Multiplier
  • A small multiplier means the heartbeats would be faster/shorter in duration.  Or the closer you get to this photoresistor/heart, the faster the heartbeat.
  • Hi Light = Low RCTime (I’ll choose 5) ==> 44000-5 / 10 = High Multiplier
  • A high multiplier means that the heartbeats are slower/longer in duration.  Or the farther you are from the photoresistor/heart, the slower the heartbeat.

And you can see this when you ask PST to show you Multiplier instead of RCValue. 

And I just used the Multiplier variable to dictate the scale of the LED blinks and how long they’d stay on.  So now we have the leds set and reacting!

Many thanks to @whixr for his help and @royeltham as well.


Filed under heartbeat coding programming spin propeller halloween rctime parallax serial terminal setting variables rc value tymkrs electronics programming for dummies

  1. tymkrs posted this