//------------------------------------------------------------------------
// -- code section //
// drip the drop hidden tormentor code. Copyright (c) 2018
// Public domain. Atomkey Laboratories.
// for use with an active piezo buzzer - the kind that already has
// the driver circuit built in and makes a tone on its own when given
// a DC power source.
// Intended setup is for the ATTINY 85 for a small form factor.
#define PIEZO 0
#define DRIP_TIME 7
#define DRIP_REPEAT 3
#define DROP_SIZE 255
#define DROP_STOP 8
#define JUST_WAIT 100
#define OFF 0
#define ON 255
#define MAX_WAIT 5
#define MAX_REPEAT 3
#define DRIP_CHANGE 3
typedef enum {as_digital, as_analog} faucet;
int drip, drop, dropSize, i; // some vars and a counter
faucet dripping;
// function prototype
void dripTheDrop(faucet flowrate, int on_off); // sound function
// the setup function ------------------------------------------------
void setup()
{
// set the ATTINY 85 pinout to the active piezo buzzer
pinMode(PIEZO, OUTPUT);
drip = 0, drop = 0; // not necessary to init, but good code to see
i = 0, dripping = as_digital; // here too...
}
// the loop function -------------------------------------------------
void loop() // start dripping some drops...
{
if(!random(MAX_WAIT)) // outer loop triggers "now and then" drip
{ // if this is the case, then we are going to drip a drop from
// the faucet, so let's decide what kind. Digital or analog?
if(random(DRIP_CHANGE)) // chance to make something other
// than a tone
{
dripping = as_analog; // going to make a cricket sound
dropSize = random((DROP_SIZE - (DROP_SIZE / 2) / 2)) + 10;
// this sound will be a lower pitch the smaller the value
}
else
{
dripping = as_digital; // going to make a tonal sound
dropSize = DROP_SIZE; // highest pitch
}
drip = random(DRIP_TIME) + 1; // how long to make the drip last
dripTheDrop(dripping, dropSize); // make the sound
delay(drip);
if(!random(MAX_REPEAT)) // inner "repeat the drip" loop
{
// repeat drip exactly to show there can be design and not
// just randomness - designed to perodically make the observer
// ponder the source differently
drop = DROP_STOP; (random(DROP_STOP) + 1); // delay after drip
i = DRIP_REPEAT; // a random number of repetitions coutner
while(--i) // repeat the former drip this [i] many times
{
dripTheDrop(dripping, OFF);
delay(drop);
dripTheDrop(dripping, dropSize);
delay(drip);
}
}
}
digitalWrite(PIEZO, OFF); // clean up and set to go again.
delay(DROP_STOP);
delay(JUST_WAIT); // add to force a longer delay between chances
}
// function to make our sounds
void dripTheDrop(faucet flowrate, int on_off)
{
if(on_off) // are we turning the drip on?
switch(flowrate)
{
case as_digital: digitalWrite(PIEZO, on_off); break;
case as_analog: analogWrite(PIEZO, on_off); break;
}
else digitalWrite(PIEZO, LOW); // turn drip off
}
// ------------- end of code --
|
Click to see full size with pinouts labeled. +Opens in a new tab. |