Multiple Array in Arduino

Happy new year LMR,

I am trying to modify the buzzer code from arduino sample. I wanna try to keep it simple and easy to call a function to manage different tones. For example:

________________________________________________

char notes1[] = "cdefgab "; // a space represents a rest
int beats1[] = { 1, 1, 1, 1, 1, 1, 3}
int length1 = 7; // the number of notes
char notes2[] = "bagfedc "; // a space represents a res
int beats2[] = { 1, 1, 1, 1, 1, 1, 3}
int length2 = 7; // the number of notes
char notes3[] = "ccccc "; // a space represents a rest
int beats3[] = { 1, 1, 1, 1, 1};

int length3 = 5; // the number of notes

______________________________________________

Is it possible to have all these 3 element(note, beat,length) in an array so i can call playTrack(2) and it will play something like:

void playTrack(int sid){
  for (int i = 0; i < length5; i++) {
    if (note[sid] == ' ') {
      delay(beat[sid] * tempo); // rest
    } else {
      playNote(note[sid], beat[sid] * tempo);
    }
    delay(tempo / 2); 
  }
}

void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(BuzzerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(BuzzerPin, 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);
}
}
}

I know there’s a tone() library but somehow it conflict with the code I am using now, beside I don’t need fancy music so. Anyone any suggestions?

BTW, is there a tone MAP out there? like 1915=c, 1700=d…956=C and so on? 

Thanks for any helps~

 

You can have an array of

You can have an array of arrays in Arduino. This is good for mapping, but you have to remember the indexes are reversed, like map[y][x] instead of map[x][y]. Here is a the map of my living room:

 

//Map data

/

             // x axis index  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17     // y axis index

//    LivingRoomMap[y][x]   0-20, 40, 60, 80,100,120,140,160,180,200,220,240,260,280,300,320,340,360 cm // cm

byte LivingRoomMap[33][18]={{100,100,100,100,100,100,100,100,100,100,255,255,255,255,255,200,200,200},  // 20   0

                            {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,210,200,200,200,200,200,200,200},  // 40   1

                            {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,200,255,255,255,255,255,200,200},  // 60   2

                            {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,200,255,255,255,255,255,200,200},  // 80   3

                            { 40,  0,  0,  0,  0,  0,  0,  0,  0,  0,200,255,255,255,255,255,200,200},  //100   4

                            {100,  0,  0,  0,  0,  0,  0,  0,  0,  0,200,255,255,255,255,255,200,200},  //120   5

                            {100,  0,  0,  0,  0,  0,  0,  0,  0,  0,200,255,255,255,255,255,200,200},  //140   6

                            {100,  0,  0,  0,  0,  0,  0,  0,  0,  0,200,200,200,200,200,200,200,200},  //160   7

                            {100,250,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,100},  //180   8

                            {100,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},  //200   9

                            {100,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},  //220  10

                            {100,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},  //240  11

                            {100,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0},  //260  12

                            {222,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,100},  //280  13

                            {255,255,  0,  0,  0,200,200,200,200,200,200,200,  0,  0,  0,  0,  0,100},  //300  14

                            {255,255,  0,  0,  0,200,200,200,200,200,200,200,  0,  0,255,255,255,255},  //320  15

                            {255,255,  0,  0,  0,200,200,200,200,200,200,200,  0,  0,255,255,255,255},  //340  16

                            {100,  0,  0,  0,  0,200,200,255,255,255,200,200,  0,  0,255,255,255,255},  //360  17

                            { 50,  0,  0,  0,  0,200,200,255,255,255,200,200,  0,  0,255,255,255,255},  //380  18

                            {  0,  0,  0,  0,  0,200,200,255,255,255,200,200,  0,  0,255,255,255,255},  //400  19

                            {  0,  0,  0,  0,  0,200,200,255,255,255,200,200, 80,  0,255,255,255,255},  //420  20

                            {  0,  0,  0,  0,  0,200,200,255,255,255,200,200,  0,  0,255,255,255,255},  //440  21

                            {  0,  0,  0,  0,  0,200,200,255,255,255,200,200,  0,  0,255,255,255,255},  //460  22

                            {  0,  0,  0,  0,  0,200,200,200,200,200,200,200,  0,  0,255,255,255,255},  //480  23

                            {  0,  0,  0,  0,  0,200,200,200,200,200,200,200,  0,  0,255,255,255,255},  //500  24

                            {  0,  0,  0,  0,  0,200,200,200,200,200,200,200,  0,  0,255,255,255,255},  //520  25

                            {  0,  0,  0,  0,  0,  0,  0,  0, 60,  0,  0,  0,  0,  0,  0,  0,  0,255},  //540  26

                            {  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 70,  0,  0,255,255},  //560  27

                            {  0,  0,  0,  0,  0,255,255,255,255,255,255,255,  0,  0,  0,  0,255,255},  //580  28

                            {  0,  0,  0,  0,  0,255,255,255,255,255,255,255,  0,255,255,  0,255,255},  //600  29

                            {  0,  0,  0,  0,  0,255,255,255,255,255,255,255,  0,255,255,  0,255,255},  //620  30

                            {  0,  0,  0,  0,  0,255,255,255,255,255,255,255,  0,  0,  0,  0,255,255},  //640  31

                            {100,  0,  0,  0,  0,255,255,255,255,255,255,255,255,255,255,255,255,255}}; //660  32

 

// 40 = wayPoint1    = LivingRoomMap[4][0]

// 50 = wayPoint2    = LivingRoomMap[18][0]

// 60 = loveSeat     = LivingRoomMap[26][8]

// 70 = computer     = LivingRoomMap[27][13]

// 80 = couch        = LivingRoomMap[20][12]

// 90 = 

//100 = nearWall

//200 = carpet

//210 = toyDrop      = LivingRoomMap[1][10]

//222 = charger      = LivingRoomMap[13][0]

//250 = flowerPot    = LivingRoomMap[8][1]

//255 = fixedObject

/

 

Let us know when you have good working code!

Here is the code I am

Here is the code I am running on my current T-3 project. Most of it is adapted from the Melody example file given with Arduino IDE. You can easily adapt this into a library if needed. The way I have it running in main all I do is call speak(1); and it plays the first track. Hope this helps. 


/
 
The calculation of the tones is made following the mathematical
 * operation:
 
 
       timeHigh = period / 2 = 1 / (2 * toneFrequency)
 
 
where the different tones are described as in the table:
 
 
note     frequency     period     timeHigh
 * c             261 Hz             3830     1915    
 * d             294 Hz             3400     1700    
 * e             329 Hz             3038     1519    
 * f             349 Hz             2864     1432    
 * g             392 Hz             2550     1275    
 * a             440 Hz             2272     1136    
 * b             493 Hz             2028    1014   
 * C            523 Hz            1912     956
 
 
http://www.arduino.cc/en/Tutorial/Melody
 /
 
int speakerPin = 3;


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);
    }
  }
}

void songStructure(int length, char notes[], int beats[], int tempo)
{
  for (int i = 0; i < length; i++)
  {
     if (notes[i] == ’ ')
     {
       delay(beats[i] * tempo); // rest
     }
     else
     {
      playNote(notes[i], beats[i] * tempo);
     }
    // pause between notes
  delay(tempo / 2);
  }
}

void continiousSongStructure(int length, char notes[], int beats[], int tempo)
{
  for (int i = 0; i < length; i++)
  {
     if (notes[i] == ’ ')
     {
       delay(beats[i] * tempo); // rest
     }
     else
     {
      playNote(notes[i], beats[i] * tempo);
     }
  }
}

/

1 - Scale
2 - Twinkle Little Star
3 - Happy Birthday
4 - Backing Up
5 - Robot Noises
6 - Gradient
*/

void speak(int song)
{
 
  if(song==1)
  {
   int length = 15;
   char notes[] = “cdefgabCbagfedc”;
   int beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4 };
   int tempo = 300;

   songStructure(length, notes, beats, tempo);
   delay(2000);
  }
 
  else if(song==2)
  {
   int length = 15;
   char notes[] = "ccggaagffeeddc ";
   int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
   int tempo = 300;

   songStructure(length, notes, beats, tempo);
  }
 
  else if(song==3)
  {
   int length = 13;
   char notes[] = "ccdcfeccdcgf ";
   int beats[] = { 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 4 };
   int tempo = 300;

   songStructure(length, notes, beats, tempo);
  }
 
  else if(song==4)
  {
   int length = 3;
   char notes[] = "fa ";
   int beats[] = { 2, 3, 6};
   int tempo = 120;

   continiousSongStructure(length, notes, beats, tempo);
  }
 
  else if(song==5)
  {
   int length = 5;
   char notes[] = "cbff ";
   int beats[] = { 1, 1, 1, 1, 4};
   int tempo = 200;

   continiousSongStructure(length, notes, beats, tempo);
  }
 
  else if(song==6)
  {
     for (int i = 2500; i > 1200; i-=3)
     {
        playTone(i, 1);
     }
    
     delay(600);
    
     for (int i = 3000; i > 1600; i-=3)
     {
        playTone(i, 1);
     }
    
     delay(5000);
  }
}


void setup()
{
  pinMode(speakerPin, OUTPUT);
}


void loop()
{
  for(int i=1; i < 7; i++)
     speak(i);
}

Thanks!!

Nice one my friend. This is exactly what I was looking for. Yeah, it would be better to make this in its own library to make the main code cleaner! Thanks!!

This is cool! Never thought

This is cool! Never thought I could map the environment this way. I am gonna try this method! Thanks robotX~