Week 2 Writing Response

Original post: https://makingtoys.net/2011/02/07/space-squid-with-lee-williams/

Prompt:

1) Explain (in a  nutshell) your code structure, logic, important parts.

2) What did you do that you feel is new, non obvious, and useful.

The code works by putting the creature in one of several states, each of which cause a sound and sometimes an addition effect based on how long it has been in that state. There is one additional state that takes precedence over all others: Dead. The behavior of the creature is completely different while it is dead.

While alive, the creature will either be excited, neutral, or screaming. Each of these values are defined by the level of light going to the photoresistor. The code will only change the state (which is storred as a string for readability’s sake) if the value from the photoresistor corresponds to a state that it is not currently on. When this happens, the state changes and the counter is set to 0.

The counter is used to effectively create two additional states: bliss and dying. If the counter gets high enough while the creature is excited, the bliss sound will play. And if the counter gets too high while the creature is screaming, the death sound will play and the creature will be set to dead.

When dead, the creature makes a psuedo randomly generated death gurgle every so often and can only be revitalized by being kept in the dark uninterrupted for a set period of time. At this point it will play the revitalize sound and come back to life.

One of the less obvious solutions we had was for the excited sound. We originally set it so that the pitch was affected by the level of light coming in. And this worked reasonably well, but if a user kept the same level of light, the creature would make almost the same sound over and over and it didn’t really sound like it was getting more excited before it played the bliss sound. To add to the effect, we had the pace of the tone be dependent on how long it had been in the excited state. The result is a fairly dynamic sound dependent on both the light level and the counter value. And no matter how the user accomplishes it, the creature definitely sounds like it is getting more and more excited before the bliss sound finally plays.

I found that our code for setting the state was useful. We were constantly getting a value from the photoresistor, but didn’t want the state to be updated with every iteration of the loop because we wanted a counter to keep track of how long the creature has been in a given state. Although there are obviously many solutions to this, ours looked like this:

if (reading<excitedPoint && state!=”excited”){
state=”excited”;
counter=0;
}

This was repeated for all three of our potential states
I liked this, because it required very little additional code. If the reading is still within the range of excited, the fact that the state is still “excited” means this section will simply be skipped. If the state was anything else, the new reading will be acknowledged, the state will be set to excited, and the counter will be reset.

A final useful if not entirely innovative feature, was setting the piezo with a potentiometer to control the volume while testing. Very easy, and saves a lot of headache.