Skip to content

Instantly share code, notes, and snippets.

@booyaa
Created December 18, 2013 21:06
Show Gist options
  • Save booyaa/8029828 to your computer and use it in GitHub Desktop.
Save booyaa/8029828 to your computer and use it in GitHub Desktop.
Colchester HackSpace (@ColchHackSpace) HAXMAS challenge
/*
Colchester HackSpace (@ColchHackSpace) HAXMAS!!!
heavily based on blink
tunes provided by https://gist.github.com/elubow/7844436
/This/ example code is in the public domain.
Power to v3.3
LEDs on pins 10-13
Speaker on pin 5
*/
/**************************************************************
** Setup chasing lights.
***************************************************************/
int leds[] = { 13, 12, 11, 10 };
int maxLeds = 4;
/**************************************************************
** Setup speaker
***************************************************************/
int speakerPin = 5;
int length = 26;
char notes[] = "eeeeeeegcde fffffeeeeddedg";
int beats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2};
int tempo = 200;
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', '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);
}
}
}
/**************************************************************
** Setup arduino pins
***************************************************************/
void setup() {
Serial.begin(9600);
// initialize the digital pin as an output.
for(int i = 0; i < maxLeds; i++) {
pinMode(leds[i], OUTPUT);
}
pinMode(speakerPin, OUTPUT);
randomSeed(analogRead(0));
}
int randNum;
/**************************************************************
** Main routine
***************************************************************/
void loop() {
// for(int i = 0; i < maxLeds; i++) {
// digitalWrite(leds[i], HIGH); // turn the LED on (HIGH is the voltage level)
// delay(500); // wait for a second
// digitalWrite(leds[i], LOW); // turn the LED off by making the voltage LOW
// delay(500); // wait for a second
// }
for (int i = 0; i < length; i++) {
randNum = random(0,4);
Serial.println(randNum);
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
digitalWrite(leds[randNum], HIGH);
delay(100);
}
// pause between notes
delay(tempo / 2);
digitalWrite(leds[randNum], LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment