User:Fabien Labeyrie/Clapping Arduino Modulo

From XPUB & Lens-Based wiki
< User:Fabien Labeyrie
Revision as of 12:23, 2 November 2010 by Fabien Labeyrie (talk | contribs) (Created page with "==Clapping music on arduino (2 Speakers)== '''Description''' *Tone function in arduino can't play note simultaneously, so we have to trick human sound perception in order to mak...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Clapping music on arduino (2 Speakers)

Description

  • Tone function in arduino can't play note simultaneously, so we have to trick human sound perception in order to make us believe two very close sounds are played at the same time.

Code

int speakerHigh = 8;
int speakerLow = 7;
int noteValue = 3000;

int theDelay = 10;
 
char pat[] = "xxx xx x xx ";
int patlen = strlen(pat);
 
void setup(){}
 
void loop (){

  for (int iLine=0; iLine<patlen; iLine++){
    
    // The following loop will be performed completly first, then launched again by the previous one.
    for (int iBeat=0; iBeat<patlen; iBeat++){
    
        // Each beat will be assigned a number value
        int iNote = iBeat+iLine;
        
        
          // When the number value is bigger than 11, it's been resetted to 0 
          if (iNote > (patlen-1)){
            iNote = iBeat-patlen+iLine;
          }

        // Channel one 
        if (pat[iBeat] == 'x') {
          // We have to shut the other speaker in order to make this one play
          noTone(speakerHigh);
          tone(speakerLow, noteValue, theDelay);
          delay(theDelay);
        }
        // The second channel is played 10 milliseconds after the first, simulating a simultaneous playing by using a higher pitched note.
        if (pat[iNote] == 'x') {
          noTone(speakerLow);
          tone(speakerHigh, noteValue, theDelay);
        }
 
    delay(theDelay*theDelay);    
    }  
  }
}