Monday, April 2, 2012

Arduino Code for Final Design


#include "Wire.h"
#include "BlinkM_funcs.h"
#define blinkm_addr 0x00

struct music {
int length;
int backuplength;
char melody[16];
char backup[16];
int beats[16];
int backupbeats[16];
};
typedef struct music music;

//______VARIABLES DEALING WITH AUDIO_____
int speakerMelody = 2;
int speakerBackup = 3;
int buttonState = 0;
int j=0;
int m=0;
int tempo = 100;
int firstTime=1;

//ALL MUSIC DATA GOES HERE
int LENGTH[2][7]={{8, 8, 7, 8, 13, 9, 7}, // All Melody Lengths
{7, 4, 7, 8, 6, 6, 7}};// All Backup Lengths

char MELODY[14][17]={"dfegfageCCCCCCCC",//Song1 Melody
"CbabagfeCCCCCCCC",// Song2 Melody
"cagfegfCCCCCCCCC",// Song3 Melody
"cgaedCbCCCCCCCCC",// Song4 Melody
"cegbagecdfaCbCCC",// Song5 Melody
"defgfecedCCCCCCC",// Song6 Melody
"ffeeddcCCCCCCCCC",// Song7 Melody
"CCaagggCCCCCCCCC",// Song1 Backup
"cegcCCCCCCCCCCCC",// Song2 Backup
"ccedcffCCCCCCCCC",// Song3 Backup
"CCaaCCggCCCCCCCC",// Song4 Backup
"egaabeCCCCCCCCCC",// Song5 Backup
"ecbcedCCCCCCCCCC",// Song6 Backup
"ffeeddcCCCCCCCCC"};//Song7 Backup

int BEATS[14][16]={{3, 1, 1, 3, 3, 1, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0}, //song1 melody speed
{ 1, 1, 1, 1, 1, 1, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0}, //song2 melody speed
{ 4, 2, 1, 1, 2, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //song3 melody speed
{ 2, 2, 2, 3, 1, 2, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0}, //song4 melody speed
{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 0, 0, 0}, //song6 melody speed
{ 1, 1, 4, 1, 1, 4, 2, 2, 4, 0, 0, 0, 0, 0, 0, 0}, //song5 melody speed
{ 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //song7 melody speed
{ 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //song1 backup speed
{ 2, 2, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //song2 backup speed
{ 1, 2, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //song3 backup speed
{ 4, 6, 2, 4, 4, 6, 2, 4, 0, 0, 0, 0, 0, 0, 0, 0}, //song4 backup speed
{ 1, 1, 2, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //song5 backup speed
{ 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //song6 backup speed
{ 1, 1, 1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0}};//song7 backup speed







void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerMelody, HIGH);
digitalWrite(speakerBackup,HIGH);
delayMicroseconds(tone);
digitalWrite(speakerMelody, LOW);
digitalWrite(speakerBackup,LOW);
delayMicroseconds(tone);
}
}

void playMelody(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
int tones[] = { 956, 851, 758, 716, 638, 568, 514, 478 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}

/*void playBackup(char note, int duration) {
char names[] = { 'j', 'k', 'l', 'm', 'n', 'h', 'i', 'J' };
int tones[] = { 956, 851, 758, 716, 638, 568, 1014, 478 };

// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
*/

// ______________LIGHT______________________

int i = 0;
int w = 0;
int p = 0;
int buttonsDown = 0;
byte r,g,b;
int delay_time = 10000; // time betwen colors, 10x00 milliseconds = 1 second
//int num_colors = 7; // how many colors are there the list below

byte color_list[][3] = {
{ 0xff, 0x00, 0x00 }, // red
{ 0x00, 0xff, 0x00 }, // green
{ 0x00, 0x00, 0xff }, // blue
{ 0xff, 0xff, 0x00 }, // red+green
{ 0x00, 0xff, 0xff }, // green+blue
{ 0xff, 0x00, 0xff }, // red+blue
{ 0xff, 0xff, 0xff }, // white
{ 0x00, 0x00, 0x00 }, // off
};






//_______________BUTTON VARIABLES________________

int buttonstate[7]={0,0,0,0,0,0,0};
int buttonPin[7] = {4,5,6,7,8,9,10};




struct music conductor(music SONG,int i, int j){
int m;

SONG.length=LENGTH[0][i];
SONG.backuplength=LENGTH[1][i];
for (m=0;m<16;m++){
SONG.melody[m]=MELODY[i][m];
SONG.backup[m]=MELODY[j][m];
SONG.beats[m]=BEATS[i][m];
SONG.backupbeats[m]=BEATS[j][m];
}
return SONG;

}


struct music song1, song2, song3, song4, song5, song6, song7;

void musicTime(music song){
//Serial.println(song.length, DEC);
for (int i = 0; i < song.length; i++) {
//Serial.println(song.melody[i], HEX);
if (song.melody == " ") {
delay(song.beats[i] * tempo); // rest
} else {
playMelody(song.melody[i], song.beats[i] * tempo);
}

// pause between notes
delay(tempo / 2); }
}


void setup()
{

Serial.begin(9600);


/*
conductor(song1, 0, 7);
conductor(song2, 1, 8);
conductor(song3, 2, 9);
conductor(song4, 3, 10);
conductor(song5, 4, 11);
conductor(song6, 5, 12); */





//_______EVERYTHING DEALING WITH SOUND______

pinMode(speakerMelody, OUTPUT);





//__________________LIGHT_______________________
BlinkM_doFactoryReset();
BlinkM_beginWithPower();
BlinkM_stopScript( blinkm_addr ); // turn off startup script
BlinkM_setFadeSpeed( blinkm_addr, 30);

//_________________BUTTONS_____________________
pinMode(buttonPin[0], OUTPUT);
pinMode(buttonPin[1], OUTPUT);
pinMode(buttonPin[2], OUTPUT);
pinMode(buttonPin[3], OUTPUT);
pinMode(buttonPin[4], OUTPUT);
pinMode(buttonPin[5], OUTPUT);
pinMode(buttonPin[6], OUTPUT);

}

void loop()
{
buttonsDown = 0;
for(i=0; i<7; i++){
buttonstate[i] = digitalRead(buttonPin[i]);
buttonsDown += buttonstate[i];
}

if(buttonsDown !=0){
for(w=0;w<7;w++){
Serial.println(w, DEC);

if(buttonstate[w]==1){
Serial.println(digitalRead(buttonPin[w]), DEC);

r = color_list[w][0];
g = color_list[w][1];
b = color_list[w][2];
BlinkM_fadeToRGB( blinkm_addr, r,g,b );
switch(w){
case 0:
song1.length=LENGTH[0][w];
song1.backuplength=LENGTH[1][w];
for (m=0;m<16;m++){
song1.melody[m]=MELODY[w][m];
song1.backup[m]=MELODY[w+7][m];
song1.beats[m]=BEATS[w][m];
song1.backupbeats[m]=BEATS[w+7][m];
}
BlinkM_fadeToRGB( blinkm_addr, r,g,b );
musicTime(song1);
break;
case 1:
song2.length=LENGTH[0][w];
song2.backuplength=LENGTH[1][w];
for (m=0;m<16;m++){
song2.melody[m]=MELODY[w][m];
song2.backup[m]=MELODY[w+7][m];
song2.beats[m]=BEATS[w][m];
song2.backupbeats[m]=BEATS[w+7][m];
}
BlinkM_fadeToRGB( blinkm_addr, r,g,b );
musicTime(song2);
break;
case 2:
song3.length=LENGTH[0][w];
song3.backuplength=LENGTH[1][w];
for (m=0;m<16;m++){
song3.melody[m]=MELODY[w][m];
song3.backup[m]=MELODY[w+7][m];
song3.beats[m]=BEATS[w][m];
song3.backupbeats[m]=BEATS[w+7][m];
}
BlinkM_fadeToRGB( blinkm_addr, r,g,b );
musicTime(song3);
break;
case 3:
song4.length=LENGTH[0][w];
song4.backuplength=LENGTH[1][w];
for (m=0;m<16;m++){
song4.melody[m]=MELODY[w][m];
song4.backup[m]=MELODY[w+7][m];
song4.beats[m]=BEATS[w][m];
song4.backupbeats[m]=BEATS[w+7][m];
}
BlinkM_fadeToRGB( blinkm_addr, r,g,b );
musicTime(song4);
break;
case 4:
song5.length=LENGTH[0][w];
song5.backuplength=LENGTH[1][w];
for (m=0;m<16;m++){
song5.melody[m]=MELODY[w][m];
song5.backup[m]=MELODY[w+7][m];
song5.beats[m]=BEATS[w][m];
song5.backupbeats[m]=BEATS[w+7][m];
}
BlinkM_fadeToRGB( blinkm_addr, r,g,b );
musicTime(song5);
break;
case 5:
song6.length=LENGTH[0][w];
song6.backuplength=LENGTH[1][w];
for (m=0;m<16;m++){
song6.melody[m]=MELODY[w][m];
song6.backup[m]=MELODY[w+7][m];
song6.beats[m]=BEATS[w][m];
song6.backupbeats[m]=BEATS[w+7][m];
}
BlinkM_fadeToRGB( blinkm_addr, r,g,b );
musicTime(song6);
break;
case 6:
song7.length=LENGTH[0][w];
song7.backuplength=LENGTH[1][w];
for (m=0;m<16;m++){
song7.melody[m]=MELODY[w][m];
song7.backup[m]=MELODY[w+7][m];
song7.beats[m]=BEATS[w][m];
song7.backupbeats[m]=BEATS[w+7][m];
}
BlinkM_fadeToRGB( blinkm_addr, r,g,b );
musicTime(song7);
break;
}
delay(200); // wait a bit because we don't need to go fast
BlinkM_fadeToRGB( blinkm_addr, 0xff, 0xff, 0xff);
}
}
}
else{
BlinkM_fadeToRGB( blinkm_addr, 0xff, 0xff, 0xff);
delay(100);
Serial.println("The button is not down : WHITE");
}
}

No comments:

Post a Comment