Paper flower singing Super Mario theme song

Description

I created a singing paper flower which can change colors following the rhythm. I can use a potentiometer to adjust the brightness of the colors.

Components

  • 1 Arduino
  • 1 Pot
  • 1 Piezo Buzzer
  • 3 LEDs
  • 4 Resistors
  • 1 Breadboard
  • several Papers

Code

#include "pitches.h"

const int piezoPin = 7; //piezo
int rPin = 9;  //red LED
int gPin = 11;  //green LED
int bPin = 10;  //blue LED
int potPin = A0;  //pot 

int potValue = 0;
// notes

int melody[] = {NOTE_E7, NOTE_E7, 0, NOTE_E7,
  0, NOTE_C7, NOTE_E7, 0,
  NOTE_G7, 0, 0,  0,
  NOTE_G6, 0, 0, 0,
 
  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,
 
  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0, NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0,
 
  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,
 
  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0, NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0};

// durations: 2 = half note, and 8/3,4,6,8,12. It appears that 8/2.9 is more accurate than 8/3.

float noteDurations[] = {
 12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
};

// calculates the number of elements in the melody array.
int musicLength=sizeof(melody)/sizeof('NOTE_F5');

void setup() {  
  pinMode(rPin, OUTPUT);
  pinMode(gPin, OUTPUT);
  pinMode(bPin, OUTPUT);
}

void loop() {
    Serial.println(potValue);
       
    for (int thisNote = 0; thisNote < musicLength; thisNote++) {
         potValue = analogRead(potPin);
      
      // calculate the note duration. change tempo by changing 2000 to other values
      int noteDuration = 2000/noteDurations[thisNote];
      tone(piezoPin, melody[thisNote],noteDuration);

      // to distinguish the notes, set a minimum time between them.
      // the note's duration + 30% seems to work well
      float pauseBetweenNotes = noteDuration * 1.1;
      delay(pauseBetweenNotes);

      // blink the three LEDs in sequence
      if (thisNote%3==0){    
          analogWrite(rPin, potValue/4);
          analogWrite(gPin, 0);
          analogWrite(bPin, 0);
          delay(100);
      }
      else if (thisNote%3==1){    
          analogWrite(rPin, 0);
          analogWrite(gPin, potValue/4);
          analogWrite(bPin, 0);
          delay(100);
      }
      else if (thisNote%3==2){    
          analogWrite(rPin, 0);
          analogWrite(gPin, 0);
          analogWrite(bPin, potValue/4);
          delay(100);
      }
   }
      
}

Leave a Reply