Learn Arduino with Olympia Circuits
Learn Arduino
  • Home
    • Get Started
    • How to Use This Site
  • Electronics
    • The Basics
    • Electricity Flows like Water
    • Electronic Components
    • The Arno Board
  • Programming
    • The Basics
    • setup and loop Blocks
    • Variables and Arrays
    • Connecting with the Pins
    • Flow Control >
      • if Statement
      • Loops
      • Delays
    • Functions
    • Serial Communication
    • USB
    • Some Thoughts
  • Projects
    • Projects 1 >
      • 1.01: Blink
      • 1.02 Blink x2
      • 1.03 Blink Faster
      • 1.04 LED Chase!
      • 1.05 Wait To Blink
      • 1.06 Blink a Little Faster Now
      • 1.07 LED Fade
      • 1.08 RGB Blink
      • 1.09 Change RGB Color with SW1
      • 1.10 Fade RGB Colors
      • 1.11 Reaction Time Game
    • Projects 2 >
      • 2.01 Hello World
      • 2.02 Talk Back
      • 2.03 ASCII Values
      • 2.04 Ski Game
      • 2.05 Demonstration of the String Object
    • Projects 3 >
      • 3.01 Read the Potentiometer
      • 3.02 ASCIIbet Soup
      • 3.03 Potentiometer sets LED Brightness
      • 3.04 Potentiometer Sets Blink Rate
      • 3.05 LED Chase, Part II
    • Projects 4 >
      • 4.01 Bringing the Piezo to Life
      • 4.02 Controlling the Piezo with a Function
      • 4.03 Piezo C Major
      • 4.04 Piezo Greensleaves
      • 4.05 Piezo Metronome
      • 4.06 Piezo as an Input
      • 4.07 Piezo as an Input 2
      • 4.08 Metronome II
      • 4.09 Piezo Playback
      • 4.10 Piezo Fireworks
      • 4.11 Piezo Mosquito
    • Projects 5 >
      • 5.01 The Phototransistor
      • 5.02 Light and Sound
      • 5.03 Light and Sound II
    • Projects 6 >
      • 6.01 EEPROM
      • 6.02 I2C Address Scan
      • 6.03 Read the I2C Temperature Sensor
      • 6.04 High Temperature Alarm
    • Projects 7 >
      • 7.01 Arno Phone Home
      • 7.02 Keyboard Alphabet
      • 7.03 Move Mouse
      • 7.04 Draw Squares
    • Special Projects >
      • Bike Light Demo
  • References
    • Arno Pin Key
    • Arno Schematic
    • Project Index

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:
  • Circuit 2
  • Circuit 3
  • Circuit 4


  • Select Sketch

    ///////////////////////////////////////////////////
    //Project 4.07 Piezo Fireworks
    int piezo = 12;
    int redLED = 9;
    int blueLED = 11;
    int thisLED;
    int SW1 = 1;
    long freqIn;
    long blow1;
    long blow2;
    void setup(){
    pinMode(SW1,INPUT);
    pinMode(piezo,OUTPUT);
    pinMode(redLED,OUTPUT);
    pinMode(blueLED,OUTPUT);
    }
    void loop(){
    while(digitalRead(SW1)==HIGH){
    }
    //launch
    for(freqIn = 200; freqIn < 500; freqIn = freqIn + 2){
    piezoTone(1000000/freqIn,10);
    }
    delay(10);
    //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);
    }
    }
    void piezoTone(long freq, long duration){
    long aSecond = 1000000;
    long period = aSecond/freq;
    duration = duration*1000;
    duration = duration/period;
    for(long k = 0; k < duration; k++){
    digitalWrite(piezo,HIGH);
    delayMicroseconds(period/2);
    digitalWrite(piezo,LOW);
    delayMicroseconds(period/2);
    }
    }
    ///////////////////////////////////////////////////

    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);

      } 

     

    Back to Projects 4

    Copyright Olympia Circuits LLC 2014. All Rights Reserved.