Im trying to store melodies for a piezo as arrays in functions so that when PinA is high function A is run and melody A is played.
this is the code that im using (for single melodies)
int notes[13] = {1915, ((1000000 / 277) / 2), 1700, ((1000000 / 311) / 2), 1519, 1432, ((1000000 / 370) / 2), 1275, ((1000000 / 415) / 2), 1136, ((1000000 / 466) / 2), 1014, 956,};
int c = notes[0];
int csharp = notes[1];
int d = notes[2];
int dsharp = notes[3];
int e = notes[4];
int f = notes[5];
int fsharp = notes[6];
int g = notes[7];
int gsharp = notes[8];
int a = notes[9];
int asharp = notes[10];
int b = notes[11];
int C = notes[12];
const byte totalnotes = 15;
int tones[totalnotes] = {g, asharp, f, e, f, e, f, g, asharp, f, e, f, e, f, g};
int beatnote[totalnotes] = {2, 2, 2, 2, 2, 1, 5, 2, 2, 2, 2, 2, 2, 5, 8};
void setup() {
pinMode(9, OUTPUT);
pinMode(14, INPUT);
pinMode(2, INPUT);
}
void loop() {int tempo = analogRead(14); tempo = map(tempo, 0, 1024, 1, 5);
for(byte i = 0; i < totalnotes; i++) {int constantbeat = (100000 / (tones[i])) * (beatnote[i]) * tempo; for(int time = 0; time < constantbeat; time++) {digitalWrite(9, HIGH); delayMicroseconds(tones[i]); digitalWrite(9, LOW); delayMicroseconds(tones[i]);delay(1);}}}
pin 2 was an input either high or low for the if() condition .
The code is basically just the example player. the code plays just one melody in a loop. in order to store more than one melody, i first tried an if(condition) {funcA (i)}... i would include 3 functions per melody. 1 for the tones, 1 for the totalnotes, and 1 for the beatnote. these would take the value of byte i and would return the corresponding index for their array (just a value for totalnotes). Howver this didnt work. can anyone explain or give correct method? i did some research and i read that arrays cant be used in functions. is this true?