User:Fabien Labeyrie/Clapping Arduino Modulo: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
Line 1: Line 1:
__NOTOC__
==Clapping Arduino==
==Clapping Arduino==


The following exercise puts [http://en.wikipedia.org/wiki/Steve_Reich Steve Reich] into an [http://en.wikipedia.org/wiki/Arduino italian chip], using its ''tone'' functionality. As a result, it should be able to play the [http://en.wikipedia.org/wiki/Clapping_Music clapping music] by itself.   
The following exercise puts [http://en.wikipedia.org/wiki/Steve_Reich Steve Reich] into an [http://en.wikipedia.org/wiki/Arduino italian chip], using its [http://arduino.cc/en/Reference/Tone ''tone''] functionality. As a result, it should be able to play the [http://en.wikipedia.org/wiki/Clapping_Music clapping music] by itself.   


===Version I :  two speakers and two loops===
===Version I :  two speakers and two loops===
Line 11: Line 12:


=====Steps=====
=====Steps=====
*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.
#We are defining the pattern
#Each speaker will act as a different channel
#The pattern is being repeated and shifted
#Because the ''tone'' function in Arduino cannot play several notes simultaneously, we are tricking human sound perception : two sounds played one after the other very quickly (10 milliseconds in this example) will be interpreted by our brain as simultaneous.


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


'''Code'''
<source lang="c">


<source lang="c">
// This is telling the board which pin to use as an output
int speakerHigh = 8;
int speakerHigh = 8;
int speakerLow = 7;
int speakerLow = 7;
// This will be the frequency of the notes
int noteValue = 3000;
int noteValue = 3000;


// This will be the duration of the notes and the pause between them
int theDelay = 10;
int theDelay = 10;
 
// This is the pattern
char pat[] = "xxx xx x xx ";
char pat[] = "xxx xx x xx ";
// This is the pattern lenght
int patlen = strlen(pat);
int patlen = strlen(pat);
   
   
void setup(){}
void setup(){}  
  void loop (){
void loop (){
 
  for (int iLine=0; iLine<patlen; iLine++){
      
      
     // The following loop will be performed completly first, then launched again by the previous one.
     // This tells the program to begin a new measure after twelve notes have been played
     for (int iBeat=0; iBeat<patlen; iBeat++){
     for (int iLine=0; iLine<patlen; iLine++){
      
      
        // Each beat will be assigned a number value
      // The following loop will be completed first, then launched again by the previous one.
        int iNote = iBeat+iLine;
      for (int iBeat=0; iBeat<patlen; iBeat++){
       
     
       
          // This gives a number value to each note
          // When the number value is bigger than 11, it's been resetted to 0  
          int iNote = iBeat+iLine;
          if (iNote > (patlen-1)){
         
            iNote = iBeat-patlen+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.
        // Channel one
          if (pat[iNote] == 'x') {
        if (pat[iBeat] == 'x') {
            noTone(speakerLow);
          // We have to shut the other speaker in order to make this one play
            tone(speakerHigh, noteValue, theDelay);
          noTone(speakerHigh);
          }
          tone(speakerLow, noteValue, theDelay);
 
           delay(theDelay);
      delay(theDelay*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);     
     }   
     }   
   }
   }

Revision as of 12:44, 7 November 2010

Clapping Arduino

The following exercise puts Steve Reich into 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

How does it work ?

The Clapping music consists in one pattern being played and shifted by two people. We will use two speakers to differentiate the two musical lines and to produce a stereo effect. The Arduino board will dispatch the tone information to the speakers thanks to two nested for loops and two if conditions. [1]

Technologies involved

The Arduino program is very close to C.

Steps
  1. We are defining the pattern
  2. Each speaker will act as a different channel
  3. The pattern is being repeated and shifted
  4. Because the tone function in Arduino cannot play several notes simultaneously, we are tricking human sound perception : two sounds played one after the other very quickly (10 milliseconds in this example) will be interpreted by our brain as simultaneous.

Source code

// This is telling the board which pin to use as an output
int speakerHigh = 8;
int speakerLow = 7;

// This will be the frequency of the notes
int noteValue = 3000;

// This will be the duration of the notes and the pause between them
int theDelay = 10;

// This is the pattern 
char pat[] = "xxx xx x xx ";

// This is the pattern lenght
int patlen = strlen(pat);
 
void setup(){} 
  void loop (){
    
    // This tells the program to begin a new measure after twelve notes have been played 
    for (int iLine=0; iLine<patlen; iLine++){
    
      // The following loop will be completed first, then launched again by the previous one.
      for (int iBeat=0; iBeat<patlen; iBeat++){
      
          // This gives a number value to each note
          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.
          if (pat[iNote] == 'x') {
            noTone(speakerLow);
            tone(speakerHigh, noteValue, theDelay);
          }
   
      delay(theDelay*theDelay);    
    }  
  }
}
  1. This code couldn't have been completed without the help of Birgit.