Description:
This project is called "Pikachu's Electrocardiogram." When Pikachu is relaxed, his ECG plays a frolicking song I re-composed for piezo. The LEDs work as an actual metronome, which cycles between colors every beat. Some beats are rests, so the lights may change without playing sound. In a relaxed state, Pikachu's ECG only alternates between blue and green LEDs. When Pikachu is in an alarmed state, the ECG plays a battle song I have meticulously added note by note. This also introduces the red light, which is a warning to stay away from Pikachu.
The Potentiometer controls the light threshold and the tempo of the two songs. When the analog reading goes under 250, Pikachu enters an "alarmed state." The photoresistor also controls the tempo of the music: moving your hand over it will slightly increase the tempo. The Potentiometer and Photoresistor work together-- the Pot adjusts the photoresistor's light sensitivity because different rooms have different amounts of light.
The music was very time consuming to add, as I had to play my iPad piano note-by-note and add rests between. I hard-coded two songs for the project.
Materials Used:
1x Pikachu Action Figure
3x LEDS (Red, Green, Blue)
3x 220 Ohm Resistors
1x 10K Ohm Resistor
1x Photoresistor
1x Potentiometer
1x Piezo Speaker
1x Arduino Uno
Code:
//DEFINE NOTES FOR PIKACHU'S ELECTROCARDIOGRAM
//MUSIC NOTES FROM Google Code
#define c3 7634
#define d3 6803
#define e3 6061
#define f3 5714
#define g3 5102
#define a3 4545
#define b3 4049
#define c4 3816 // 261 Hz
#define d4 3401 // 294 Hz
#define e4 3030 // 329 Hz
#define f4 2865 // 349 Hz
#define g4 2551 // 392 Hz
#define a4 2272 // 440 Hz
#define a4s 2146
#define b4 2028 // 493 Hz
#define c5 1912 // 523 Hz
#define d5 1706
#define d5s 1608
#define e5 1517
#define f5 1433
#define g5 1276
#define a5 1136
#define a5s 1073
#define b5 1012
#define c6 955
// Define a special note, 'R', to represent a rest
#define R 0
// SETUP ============================================
// Set up speaker on a PWM pin (digital 9, 10 or 11)
int speakerOut = 7;
int pot = A0;
int x = 1;
int ledPinR = 9; // select the pin for the LED
int ledPinG = 10; // select the pin for the LED
int ledPinB = 11; // select the pin for the LED
// Do we want debugging on serial out? 1 for yes, 0 for no
int DEBUG = 1;
void setup() {
pinMode(speakerOut, OUTPUT);
Serial.begin(9600); // Set serial out if we want debugging
}
// Custom Pokemon Melodies
int melody[] = {e4, R, f4, R, g4, R, c5, R, R, b4, R, R, a4, b4, a4, R, R, f4, g4, a4, d5, c5, R, R, b4, R, a4, b4, c5, R, R, a4, g4, R, R, R, c5, b4, c5, a4, R, R, c5, b4, c5, g4, R, f4, c5, R, e5, d5, c5, R, b4, c5, d5, R, R, c5, b4, a4, g4, f4, R};
int beats[] = {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 8, 8, 8, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16};
int melody2[] = {a4, R, b4, R, c5, R, R, a4, b4, c5, R, g4, R, R, a4, R, b4, R, c5, R, R, a4, b4, c5, R, g4, R, R, a4, R, e4, R, R, a4, R, e4, R, a4, R, a4s, R, R, R, a4, R, e4, R, R, a4, R, e4, R, a4, R, g4, R, R, R, f4, R, R, c5, R, R, f4, c5, a4, g4, R, R, R, f4, R, c5, R, d5, R, e5, R, d5, R, R, R, f5, R, g5, f5, e5, d5, c5, R, d5, e5, R, R, R, R, R, R, R };
int beats2[] = {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 8, 8, 8, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16};
int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.
// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
// Initialize core variables
int toneM = 0;
int beat = 0;
long duration = 0;
// PLAY BATTLE SONG ==============================================
void playTone() {
long elapsed_time = 0;
if (toneM > 0) { // if this isn't a Rest beat, while the tone has
// played less long than 'duration', pulse speaker HIGH and LOW
while (elapsed_time < duration) {
digitalWrite(speakerOut,HIGH);
delayMicroseconds(toneM / 2);
// DOWN
digitalWrite(speakerOut, LOW);
delayMicroseconds(toneM / 2);
// Keep track of how long we pulsed
elapsed_time += (toneM);
}
}
}
// BEGIN POKEMON CAPTURE SEQUENCE =============================
void loop() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT; i++) {
// potentiometer value sets speed
int tempo = map(analogRead(pot), 0, 2048, 10000, 10500);
int delayValue = analogRead(pot);
if (delayValue > 250) {
toneM = melody[i];
beat = beats[i];
MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.
} else {
toneM = melody2[i];
beat = beats2[i];
MAX_COUNT = sizeof(melody2) / 2; // Melody length, for looping.
}
duration = beat * tempo; // Set up duration of song
Serial.println(duration);
Serial.println(analogRead(pot));
if (delayValue > 250) {
if (x == 1) {
analogWrite(ledPinG, 255); // analogWrite can be between 0-255
analogWrite(ledPinR, 0); // analogWrite can be between 0-255
analogWrite(ledPinB, 0); // analogWrite can be between 0-255
x = 2; }
else if (x == 2) {
analogWrite(ledPinB, 255); // analogWrite can be between 0-255
analogWrite(ledPinR, 0); // analogWrite can be between 0-255
analogWrite(ledPinG, 0); // analogWrite can be between 0-255
x = 1;
}
} else {
if (x == 1) {
analogWrite(ledPinR, 255); // analogWrite can be between 0-255
analogWrite(ledPinB, 0); // analogWrite can be between 0-255
analogWrite(ledPinG, 0); // analogWrite can be between 0-255
x = 2; }
else if (x == 2) {
analogWrite(ledPinG, 255); // analogWrite can be between 0-255
analogWrite(ledPinR, 0); // analogWrite can be between 0-255
analogWrite(ledPinB, 0); // analogWrite can be between 0-255
x = 3; }
else if (x == 3) {
analogWrite(ledPinB, 255); // analogWrite can be between 0-255
analogWrite(ledPinR, 0); // analogWrite can be between 0-255
analogWrite(ledPinG, 0); // analogWrite can be between 0-255
x = 1;
}
}
delay(delayValue);
playTone();
}
}
- Login to post comments