/************************************************************************
Musical Bear
Tahani Almanie
This code runs two different melodies (Twinkle-Twinkle and Do-Re-Mi) using a LilyPad Buzzer and controlled by two switches.
Every melody is combined with a harmonious light pattern of Rainbow LEDs.
************************************************************************/
// LED pins are attched to
int redPin = 2;
int yellowPin = 3;
int purplePin = 4;
int greenPin = 5;
int pinkPin = 6;
int bluePin = 7;
// Buzzer pin is attached to
int buzzerPin = 10;
// Switch pins are attached to
int switch1Pin = A3;
int switch2Pin = A0;
// Variables to store switch state
int switch1State = 0;
int switch2State = 0;
// Melody Notes for Do-Re-Mi
const int C = 1046;
const int D = 1175;
const int E = 1319;
const int F = 1397;
const int G = 1568;
const int A = 1760;
const int B = 1976;
const int C1 = 2093;
const int D1 = 2349;
// Melody Notes for Twinkle-Twinkle
int length = 15; // the number of notes
char notes[] = "ccggaagffeeddc "; // a space represents a rest
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
void setup()
{ // Set LED pins as OUTPUT
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(purplePin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(pinkPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Set Buzzer pin as OUTPUT
pinMode(buzzerPin, OUTPUT);
//Set Switches as INPUT
pinMode(switch1Pin, INPUT_PULLUP);
pinMode(switch2Pin, INPUT_PULLUP);
}
void loop()
{
switch1State = digitalRead(switch1Pin);
switch2State = digitalRead(switch2Pin);
if (switch1State == LOW) // switch1 is ON
{
// turn on Do-Re-Mi melody
for (int delayTime = 500; delayTime >=100; delayTime-=100) {
switch1State = digitalRead(switch1Pin);
if (switch1State == LOW)
{
playSong(delayTime);
}
else
{
noTone(buzzerPin); // switch1 is OFF
}
}
}
else if (switch2State == LOW) // switch2 is ON
{
// turn on Twinkle-Twinkle melody
for (int i = 0; i < length; i++)
{
if (notes[i] == ' ')
{
delay(beats[i] * tempo); // rest
}
else
{
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
}
//delay(delayTime);
}
// for Do-Re-Mi melody
void playSong(int delayTime)
{
tone(buzzerPin, C);
digitalWrite(redPin, HIGH);
delay(delayTime);
tone(buzzerPin, D);
digitalWrite(yellowPin, HIGH);
delay(delayTime);
tone(buzzerPin, E);
digitalWrite(purplePin, HIGH);
delay(delayTime);
tone(buzzerPin, F);
digitalWrite(greenPin, HIGH);
delay(delayTime);
tone(buzzerPin, G);
digitalWrite(pinkPin, HIGH);
delay(delayTime);
tone(buzzerPin, A);
digitalWrite(bluePin, HIGH);
delay(delayTime);
tone(buzzerPin, B);
digitalWrite(redPin, HIGH);
delay(delayTime);
tone(buzzerPin, C1);
digitalWrite(yellowPin, HIGH);
delay(delayTime);
// Use noTone() to shut off the buzzer and delay to create a 'rest'
noTone(buzzerPin);
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(purplePin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(pinkPin, LOW);
digitalWrite(bluePin, LOW);
delay(delayTime);
}
//for Twinkle-Twinkle melody
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
//for Twinkle-Twinkle melody
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(buzzerPin, HIGH);
digitalWrite(redPin, HIGH);
digitalWrite(yellowPin, HIGH);
digitalWrite(purplePin, HIGH);
digitalWrite(greenPin, HIGH);
digitalWrite(pinkPin, HIGH);
digitalWrite(bluePin, HIGH);
delayMicroseconds(tone);
digitalWrite(buzzerPin, LOW);
digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(purplePin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(pinkPin, LOW);
digitalWrite(bluePin, LOW);
delayMicroseconds(tone);
}
}
Comments