Project 4.10 Piezo Fireworks
In this project we use the piezo and flashing LEDs to create the effect of a firework being launched into the sky. The user launches the firework by pressing SW1.
Concepts: % (modulo), random function
Circuits:
Concepts: % (modulo), random function
Circuits:
At the top of the loop() block, the sketch waits for SW1 to be pressed before continuing:
while(digitalRead(SW1)==HIGH){
}
First comes the sound of the firework launch. Because of the Doppler effect, the frequency of the firework decreases as it flies into the air. The variable freqIn is incremented from 200 to 500 in steps of 2. The actual frequency for the sound is 1000000/freqIn. This makes the frequency change at a non-constant rate (it changes at a slower rate as freqIn increases), making a more realistic sound:
//launch
for(freqIn = 200; freqIn < 500; freqIn = freqIn + 2){
piezoTone(1000000/freqIn,10);
}
We delay of 10 milliseconds before the sound of the explosion:
delay(10);
It’s difficult to mimic the sound of an explosion with the piezo. The sound we create seems like something out of a video game from the 1980s. We play 250 short bursts of random tones between 500 and 100 Hz in random durations between 1 and 5 milliseconds. The variable blow1 randomly picks the tones and the variable blow2 randomly picks the durations. We flash the red and blue channels of the RGB LED along with the sound. We flash redLED twice for every one time that blueLED is flashed. We use the modulo operator, %, to decide when to flash blueLED. The modulo operator returns the remainder of the division of two integers. The remainder of k % 3 equals zero when k = 0, 3, 6, 9, 12, etc:
//explosion
for(int k = 0; k < 250; k++){
thisLED = redLED;
if(k % 3 == 0) thisLED = blueLED;
digitalWrite(thisLED, HIGH);
blow1 = random(500,1000);
blow2 = random(1,5);
piezoTone(blow1,blow2);
digitalWrite(thisLED,LOW);
}
while(digitalRead(SW1)==HIGH){
}
First comes the sound of the firework launch. Because of the Doppler effect, the frequency of the firework decreases as it flies into the air. The variable freqIn is incremented from 200 to 500 in steps of 2. The actual frequency for the sound is 1000000/freqIn. This makes the frequency change at a non-constant rate (it changes at a slower rate as freqIn increases), making a more realistic sound:
//launch
for(freqIn = 200; freqIn < 500; freqIn = freqIn + 2){
piezoTone(1000000/freqIn,10);
}
We delay of 10 milliseconds before the sound of the explosion:
delay(10);
It’s difficult to mimic the sound of an explosion with the piezo. The sound we create seems like something out of a video game from the 1980s. We play 250 short bursts of random tones between 500 and 100 Hz in random durations between 1 and 5 milliseconds. The variable blow1 randomly picks the tones and the variable blow2 randomly picks the durations. We flash the red and blue channels of the RGB LED along with the sound. We flash redLED twice for every one time that blueLED is flashed. We use the modulo operator, %, to decide when to flash blueLED. The modulo operator returns the remainder of the division of two integers. The remainder of k % 3 equals zero when k = 0, 3, 6, 9, 12, etc:
//explosion
for(int k = 0; k < 250; k++){
thisLED = redLED;
if(k % 3 == 0) thisLED = blueLED;
digitalWrite(thisLED, HIGH);
blow1 = random(500,1000);
blow2 = random(1,5);
piezoTone(blow1,blow2);
digitalWrite(thisLED,LOW);
}