Hello
I have Googled this and spent time looking on MSDN and places like StackOverflow and while I have found examples I don't quite understand them. Also it seems like the data usage is different than what I am trying to do so the examples don't really 'fit'.
I am new to programming and Arduino. So right now I am just playing with LEDs and a pizzo and seeing what I can do with these simple building blocks.
Here is what I have so far.
The Fritzing breadboard is bigger than mine and I used 5 colors of LEDs instead of all red ones. But the wiring colors are the same.
//Set led pins
int led1 = A5;
int led2 = 12;
int led3 = 11;
int led4 = 10;
int led5 = 8;
int led6 = 7;
int led7 = 6;
int led8 = 5;
int led9 = 4;
int led10 = 3;
int led11 = A4;
int led12 = A0;
int led13 = A1;
int led14 = A2;
int led15 = A3;
//Set notes to rounded hertz values
int E3 = 165;
int a3 = 220;
int C4 = 262;
int D4 = 294;
int E4 = 330;
int F4 = 349;
int G4 = 392;
int Gs4 = 415;
int a4 = 440;
int B4 = 494;
int C5 = 523;
int D5 = 587;
int E5 = 659;
int F5 = 698;
int Fs5 = 740;
//Set values for note and delay lengths
int eight = 220; //8th note
int trip = 145; //tripplet
int lng = eight + 5; //adding 5ms to add a slight gap between notes
int ttt = trip + 5;
//Make 7 part arrays for the notes on value and delays
int notes[] = {eight, trip, trip, trip, eight, eight, eight};
int delays[] = {lng, ttt, ttt, ttt, lng, lng, lng};
//Set chord run arrays the L is the corresponding LED to each note
int aMin[] = {a3, E4, a4, C5, E5, C5, a4};
int aMinL[] = {led2, led5, led9, led11, led13, led11, led9};
int cMaj[] = {C4, E4, G4, C5, E5, C5, G4};
int cMajL[] = {led3, led5, led7, led11, led13, led11, led7};
int dMaj[] = {D4, a4, D5, Fs5, Fs5, D5, a4};
int dMajL[] = {led4, led9, led12, led15, led15, led12, led9};
int fMaj[] = {F4, a4, C5, F5, F5, C5, a4};
int fMajL[] = {led6, led9, led11, led14, led14, led11, led9};
int eMaj[] = {E3, E4, Gs4, B4, E5, B4, Gs4};
int eMajL[] = {led1, led5, led8, led10, led13, led10, led8};
//set sound output pin
int buzzer = 9;
void setup() {
Serial.begin(9600); //turn serial on for debugging
//Set the modes for all the pins
pinMode(buzzer, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT);
pinMode(led8, OUTPUT);
pinMode(led9, OUTPUT);
pinMode(led10, OUTPUT);
pinMode(led11, OUTPUT);
pinMode(led12, OUTPUT);
pinMode(led13, OUTPUT);
pinMode(led14, OUTPUT);
pinMode(led15, OUTPUT);
}
void loop() {
for (int count = 0; count < 7; count++) {
digitalWrite (aMinL[count], HIGH);
tone(buzzer, aMin[count], notes[count]);
delay(delays[count]);
digitalWrite (aMinL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (cMajL[count], HIGH);
tone(buzzer, cMaj[count], notes[count]);
delay(delays[count]);
digitalWrite (cMajL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (dMajL[count], HIGH);
tone(buzzer, dMaj[count], notes[count]);
delay(delays[count]);
digitalWrite (dMajL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (fMajL[count], HIGH);
tone(buzzer, fMaj[count], notes[count]);
delay(delays[count]);
digitalWrite (fMajL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (aMinL[count], HIGH);
tone(buzzer, aMin[count], notes[count]);
delay(delays[count]);
digitalWrite (aMinL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (cMajL[count], HIGH);
tone(buzzer, cMaj[count], notes[count]);
delay(delays[count]);
digitalWrite (cMajL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (aMinL[count], HIGH);
tone(buzzer, aMin[count], notes[count]);
delay(delays[count]);
digitalWrite (aMinL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (eMajL[count], HIGH);
tone(buzzer, eMaj[count], notes[count]);
delay(delays[count]);
digitalWrite (eMajL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (eMajL[count], HIGH);
tone(buzzer, eMaj[count], notes[count]);
delay(delays[count]);
digitalWrite (eMajL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (aMinL[count], HIGH);
tone(buzzer, aMin[count], notes[count]);
delay(delays[count]);
digitalWrite (aMinL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (cMajL[count], HIGH);
tone(buzzer, cMaj[count], notes[count]);
delay(delays[count]);
digitalWrite (cMajL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (dMajL[count], HIGH);
tone(buzzer, dMaj[count], notes[count]);
delay(delays[count]);
digitalWrite (dMajL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (fMajL[count], HIGH);
tone(buzzer, fMaj[count], notes[count]);
delay(delays[count]);
digitalWrite (fMajL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (aMinL[count], HIGH);
tone(buzzer, aMin[count], notes[count]);
delay(delays[count]);
digitalWrite (aMinL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (eMajL[count], HIGH);
tone(buzzer, eMaj[count], notes[count]);
delay(delays[count]);
digitalWrite (eMajL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (aMinL[count], HIGH);
tone(buzzer, aMin[count], notes[count]);
delay(delays[count]);
digitalWrite (aMinL[count], LOW);
}
for (int count = 0; count < 7; count++) {
digitalWrite (eMajL[count], HIGH);
tone(buzzer, eMaj[count], notes[count]);
delay(delays[count]);
digitalWrite (eMajL[count], LOW);
}
}//End of main loop
A quick movie showing the basic setup working
What it does basically is there are 15 different notes used in a little tune(House of the Rising Sun guitar riff) and each note has a specific LED that comes on when that note is played.
The tune is set up with a different chord run for each measure and there are 16 measures.
As can be seen in the code above what I have done is call each chord run in its own for loop and stepped through each chord array for both the notes and LEDs. It works but seems very inefficient.
What I would like to be able to do is place each chord run array into a new array called song.
Something like this...
int song[] {aMin,cMaj,dMaj,fMaj,aMin,cMaj,eMaj,eMaj,aMin,cMaj,dMaj,fMaj,aMin,eMaj,aMin,eMaj };
...and then be able to step through that in one for loop. So it would step through the song array first stepping through each of the seven steps in aMin, then stepping through the seven steps in cMaj and then the seven in dMaj etc for all 16 places in the song array.
Something like this...
for (count = 0; count < 112; count++){
tone (buzzer, song[count]);
}
That of course doesn't work because I don't understand how to place the chord arrays into a song array and step through it in a for loop. The examples I saw talked about pointers and some used * and some used multiple sets of [ ] and it was all very confusing.
So could someone please show me some code using my variables from above to do what I am trying to do? Using my variables would make it clearer to me what is going on.
Thanks
PS: While writing the above I thought about the idea of creating a method for each chord run loop and then calling the methods in the order of the song which would trim down the code a little bit.
But I would still like to learn how to make and use arrays of arrays.