//------------------- piezo_random_freq --------------------- //--- Plays a pitch thru a piezo speaker //- Set up: > Attach a piezo speaker between ground and Digital 6 (pin 12) // int piezoPin = 6; // variable for which pin is attached to piezo int rand_freq; // variable for storing random frequency int short_note = 200; int long_note = 500; void setup(){ pinMode(piezoPin, OUTPUT); // Set the digital LED pin as an output srand((int)15); //Set "seed" for random number generator } void loop(){ for(int i = 0; i < 3; i++){ // 3 short tones rand_freq = random(60, 900); // Random pitch between 60 and 900 tone(piezoPin, rand_freq, short_note); // syntax: tone(pin, freq, duration) delay(100); } for(int i = 0; i < 3; i++){ // 3 long tones rand_freq = random(300, 1200); // Random pitch between 300 and 1200 tone(piezoPin, rand_freq, long_note); delay(300); } } |