Following Owen’s great overview of our progress so far, I was inspired to try my hand at keeping some kind of running documentation of our next steps. If you haven’t read his post, go [there] first.
OK. In order to make the clicking sounds, Owen created a very clever and efficient bit of code, using the Random() function in a For() loop to vary the number of times a simple Tone function would be quickly implimented:
for (int i = 0; i < random(0,10); i++) {
tone(speakerPin, NOTE_C4, 5);
}
Simple and smart. Linking an additional behavior to the clicks (and by "behavior", I mean a change in our organism's lighting, its movement, its temperature, etc) such as doing a PMW write to an LED strip, however, is tied to the repetition of the For() loop. Flashing the lights in exactly the same manner as the clicks just doesn't look very organic. The oscillation of different behaviors needs to be related, but not congruent.
Enter the millis() function. We'll need to create a couple of counters, probably one for each of the behaviors we want to implement. I'll start off by creating a timer class, so that we can call different timer objects. Rune, a 2011 ITP resident, helped me with this kind of code yesterday.
[insert processing code]
Well, *that* didn't work. Turns out the way that the Arduino language handles objects is a far cry from the way processing works 'em in. (Probably an overstatement). But, we'll try to just get millis functionality in Arduino without getting classy. Err, complicated. Meaning, no classes. For now.
void loop() {
duration = random(100,1000);
if(millis() - lastMillis > duration)
{
lastMillis = millis();
tone(asPin, NOTE_C3, 5);
}else{
noTone(asPin);
}
}
Weird. This code appears an even clicking, like a metronome. But after giving the random function a broader range–
duration = random(5,1000);
– a nice, syncopated clicking emerges. Great.

Recent Comments