User:Fabien Labeyrie/Clapping Arduino Modulo

From XPUB & Lens-Based wiki

Clapping Arduino

The following exercise puts Steve Reich in an italian chip, using its tone functionality. As a result, it should be able to play the clapping music by itself.

Version I : two speakers and two loops

This code got a few inspiration

  • 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.

It's fucking too late to do some wiki stuff so I'm gonna continue tomorrow

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