Created
May 13, 2013 15:33
-
-
Save walshbr/5569210 to your computer and use it in GitHub Desktop.
Arduino Rainbow Hack. Mash up of http://ardx.org/CODE12S and http://www.arduino.cc/en/Tutorial/Melody
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Piezo speaker connected to pins | |
int speakerPin = 6; | |
// RGB LED leads connected to PWM pins | |
const int RED_LED_PIN = 9; | |
const int GREEN_LED_PIN = 10; | |
const int BLUE_LED_PIN = 11; | |
// Used to store the current intensity level of the individual LEDs | |
int redIntensity = 0; | |
int greenIntensity = 0; | |
int blueIntensity = 0; | |
char notes[] = {"cCbgabccaag "}; // a space represents a rest | |
int length = sizeof(notes); // the number of notes | |
int beats[] = {4,4,2,1,1,2,4,1,1,1,4,1}; | |
int tempo = 300; | |
void playTone(int tone, int duration) { | |
for (long i = 0; i < duration * 1000L; i += tone * 2) { | |
digitalWrite(speakerPin, HIGH); | |
delayMicroseconds(tone); | |
digitalWrite(speakerPin, LOW); | |
delayMicroseconds(tone); | |
} | |
} | |
void playNote(char note, int duration) { | |
char names[] = { 'c', 'l', 'd', 'm','e', 'f', 'n', 'g', 'o','a', 'p', 'b', 'C', 'L', 'D', 'M', 'E', 'F', 'N', 'G', 'O', 'A', 'P', 'B' }; | |
int tones[] = { 1915, 1805, 1700, 1608, 1519, 1432, 1351, 1275, 1205, 1136, 1073, 1014, 956, 903, 850, 804, 760, 716, 676, 638, 603, 568, 478 }; | |
// play the tone corresponding to the note name | |
for (int i = 0; i < sizeof(names); i++) { | |
if (names[i] == note) { | |
playTone(tones[i], duration); | |
} | |
} | |
} | |
void setup() { | |
pinMode(speakerPin, OUTPUT); | |
} | |
void loop() { | |
for (int i = 0; i < length; i++) { | |
if (notes[i] == ' ') { | |
delay(beats[i] * tempo); // rest | |
} else { | |
playNote(notes[i], beats[i] * tempo); | |
greenIntensity = 255 * rand(); | |
redIntensity = 255 * rand(); | |
blueIntensity = 255 * rand(); | |
analogWrite(GREEN_LED_PIN, greenIntensity); | |
analogWrite(RED_LED_PIN, redIntensity); | |
analogWrite(BLUE_LED_PIN, blueIntensity); | |
delay(beats[i]); | |
} | |
// pause between notes | |
delay(tempo / 2); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment