Friday, April 1, 2011

D, I & E Intensive Day 2

Day 2, thinking about our little circuits as fireflies and then crickets, we set out to get them to interact with each other - the building blocks for emergent behavior and swarming.

We replaced the flex sensor with a light sensor (also a variable resistor) for the input - making both the input and the output light. Now the critters could in principle talk to each other, but first we had to calibrate the values. This time we did it in a more sophisticated way - after establishing the approximate range of input values with the serial monitor, we mapped the values to the entire range of possible output values (0 to 255) to ensure maximum sensitivity. Given that counting can wrap we also constrained any input values that were outside of our approximated range to be 0 or 255.

Calibrated values printed in the serial monitor  - these are written to the output LED pin
The light sensor gives a higher value in dark conditions and so without reversing our mapping (which is as easy as swapping the toLow and toHigh values in the map function) has the effect of turning the light on when it is dark.

The second critter's light is on when the first critter's light is off
Here every second critter's light is off
We then added a buzzer (essentially a small low quality speaker - it has a disc that can be vibrated to generate different sound at different frequency), and a knob (another variable resistor - this one has 3 terminals effectively making it 2 resistors in series meaning that it doesn't need additional resistors in the circuit - 3 terminal variable resistors are called potentiometers).

The finished critter with speaker (black) at back of breadboard and blue knob at front
We experimented with using the knob and the light sensor to control the frequency and duration of the tone. At first the sound was continuous because of the speed of the loop - another tone would begin immediately. We added delays, as we had previously with the blinking light. However delays pause the entire program, including the light sensor, making it all rather clunky!

I decided to use the time since the program had been running (called with the millis function) to set up intervals - I have used a similar strategy previously in Processing. That is: if the time now is greater than the time that the previous tone began plus the previous tone duration plus the interval then begin the next tone.

Timer using If statements and millis()

No comments:

Post a Comment