Created
August 12, 2014 21:41
-
-
Save philipp-spiess/f1e74fa5a57070b19336 to your computer and use it in GitHub Desktop.
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
#include <Arduino.h> | |
short status_leds[4] = {7, 8, 5, 4}; | |
short status_leds_count = 4; | |
short status_leds_active = 1; | |
short dance_leds[2] = {13,12}; | |
short dance_leds_count = 2; | |
short buzzer = 3; | |
short button = 2; | |
short red_pin = 6; | |
short blue_pin = 10; | |
short yellow_pin = 9; | |
typedef struct { | |
short length; | |
short frequencies[13]; | |
} notation; | |
typedef struct { | |
short length; | |
short tempo; | |
byte hex[117]; | |
} song; | |
notation notations[2] = { | |
{ 11, {0, 262, 294, 330, 349, 392, 440, 467, 494, 523, 587} }, | |
{ 13, {0, 277, 294, 330, 369, 392, 440, 467, 494, 554, 587, 659, 739} } | |
}; | |
song songs[2] = { | |
{ 117, 120, {0x17, 0x67, 0x57, 0x47, 0x10, 0x03, 0x1f, 0x1f, 0x17, 0x67, 0x57, 0x47, 0x23, 0x07, 0x07, 0x27, 0x77, 0x67, 0x57, 0x21, 0x97, 0x97, 0x77, 0x57, 0x63, 0x07, 0x17, 0x67, 0x57, 0x47, 0x17, 0x11, 0x67, 0x57, 0x47, 0x23, 0x07, 0x27, 0x27, 0x77, 0x67, 0x57, 0x97, 0x97, 0x97, 0x97, 0xa7, 0x97, 0x77, 0x57, 0x43, 0x07, 0x07, 0x67, 0x67, 0x63, 0x67, 0x67, 0x63, 0x67, 0x97, 0x47, 0x0f, 0x5f, 0x63, 0x07, 0x07, 0x77, 0x77, 0x77, 0x0f, 0x7f, 0x77, 0x67, 0x67, 0x6f, 0x6f, 0x67, 0x57, 0x57, 0x67, 0x57, 0x93, 0x07, 0x07, 0x67, 0x67, 0x63, 0x67, 0x67, 0x63, 0x67, 0x97, 0x47, 0x0f, 0x5f, 0x63, 0x07, 0x07, 0x77, 0x77, 0x77, 0x0f, 0x7f, 0x77, 0x67, 0x67, 0x6f, 0x6f, 0x97, 0x97, 0x77, 0x57, 0x43, 0x07, 0x07} }, | |
{ 70, 120, {0x00, 0x07, 0x67, 0xa7, 0x97, 0xa7, 0x93, 0x03, 0x03, 0x03, 0x03, 0x07, 0x97, 0xb3, 0xa7, 0x87, 0x63, 0x03, 0x03, 0x03, 0x03, 0x07, 0x67, 0xa3, 0x97, 0xa7, 0x93, 0x07, 0x57, 0x77, 0x77, 0x67, 0x07, 0x67, 0x77, 0x67, 0x53, 0x57, 0x47, 0x43, 0x03, 0x03, 0x03, 0x07, 0xc7, 0xc7, 0xc7, 0xc3, 0xb7, 0xa7, 0xa3, 0x77, 0x77, 0x07, 0xa3, 0x07, 0x07, 0xb7, 0xb7, 0xb7, 0xb3, 0xa7, 0x77, 0x73, 0x67, 0x67, 0x07, 0x63, 0x07} } | |
}; | |
int current_song = 0; | |
int frequency(byte note); | |
void analogWriteLeds(int value, short int leds[], int count); | |
bool checkForButton(); | |
short beatDuration(short bpm, int beat); | |
void showRGB(int color); | |
void setup() { | |
int i; | |
for(i = 0; i < status_leds_count; i++) { | |
pinMode(status_leds[i], OUTPUT); | |
} | |
for(i = 0; i < dance_leds_count; i++) { | |
pinMode(dance_leds[i], OUTPUT); | |
} | |
pinMode(buzzer, OUTPUT); | |
pinMode(button, INPUT); | |
pinMode(red_pin, OUTPUT); | |
pinMode(yellow_pin, OUTPUT); | |
pinMode(blue_pin, OUTPUT); | |
Serial.begin(9600); | |
digitalWrite(status_leds[0], HIGH); | |
} | |
int frequency(byte note) { | |
int int_note = (int) note; | |
notation _notation = notations[current_song]; | |
return _notation.frequencies[note]; | |
} | |
void loop() { | |
int i, duration, time_passed = 0; | |
bool skip = false; | |
Serial.print(F("Currently Playing: ")); | |
Serial.println(current_song); | |
for(i = 0; (i < songs[current_song].length) && ! skip; i++) { | |
byte hex = songs[current_song].hex[i]; | |
byte note = (hex & 0xF0) >> 4; | |
int beat = (int) (hex & 0x0F); | |
beat = beat + 1; | |
Serial.print(i); | |
Serial.print(F(": ")); | |
duration = beatDuration(songs[current_song].tempo, beat); | |
Serial.println(duration); | |
if (note == 0) { | |
digitalWrite(dance_leds[0], LOW); | |
digitalWrite(dance_leds[1], LOW); | |
digitalWrite(red_pin, LOW); | |
digitalWrite(blue_pin, LOW); | |
digitalWrite(yellow_pin, LOW); | |
delay(duration); // then pause for a moment | |
} else { | |
int fq = frequency(note); | |
tone(buzzer, fq, duration); | |
if(i%2 == 1) { | |
digitalWrite(dance_leds[0], HIGH); | |
digitalWrite(dance_leds[1], LOW); | |
} else { | |
digitalWrite(dance_leds[0], LOW); | |
digitalWrite(dance_leds[1], HIGH); | |
} | |
if( fq > 0 ) { | |
showRGB( (fq - 250)*2 ); | |
} | |
delay(duration); // wait for tone to finish | |
} | |
//delay(10); | |
time_passed += duration + 10; | |
if(time_passed > 500) { | |
time_passed = 0; | |
skip = checkForButton(); | |
} | |
} | |
} | |
void analogWriteLeds(int value, short int leds[], int count) { | |
int i; | |
for(i = 0; i < count; i++) { | |
analogWrite(leds[i], value); | |
} | |
} | |
bool checkForButton() { | |
if(digitalRead(button) == LOW) { | |
if(status_leds_active < 2) { | |
status_leds_active++; | |
current_song++; | |
} else { | |
status_leds_active = 1; | |
current_song = 0; | |
} | |
analogWriteLeds(0, status_leds, status_leds_count); | |
analogWriteLeds(255, status_leds, status_leds_active); | |
return true; | |
} | |
return false; | |
} | |
short beatDuration(short bpm, int beat) { | |
float spb = ((1.0/((float) (bpm / 60))) * 4000) / beat; | |
return (short) spb; | |
} | |
void showRGB(int color) { | |
int redIntensity, greenIntensity, blueIntensity; | |
if (color <= 255) { | |
redIntensity = 255 - color; // red goes from on to off | |
greenIntensity = color; // green goes from off to on | |
blueIntensity = 0; // blue is always off | |
} else if (color <= 511) { | |
redIntensity = 0; // red is always off | |
greenIntensity = 255 - (color - 256); // green on to off | |
blueIntensity = (color - 256); // blue off to on | |
} else { | |
redIntensity = (color - 512); // red off to on | |
greenIntensity = 0; // green is always off | |
blueIntensity = 255 - (color - 512); // blue on to off | |
} | |
analogWrite(red_pin, redIntensity); | |
analogWrite(blue_pin, blueIntensity); | |
analogWrite(yellow_pin, greenIntensity); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment