User:Fabien Labeyrie/Clapping Arduino Modulo
< User:Fabien Labeyrie
Revision as of 12:18, 7 November 2010 by Fabien Labeyrie (talk | contribs)
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
- 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);
}
}
}