Tuesday, October 31, 2006

Coding for Random Sounds

Here is the program:

int speakerOut = 9;
int delTime = 0;
int baseTime = 0;
int ranFreq = 0; //Pulse Width (PW)
int maxTime = 10;

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


void loop(){
ranFreq = random (850,2050);
maxTime = random (5,40);
delTime = random (100,1000);
//This for loop sets the length of the note
for (baseTime =0; baseTime < maxTime; baseTime++){
//This sets the frequence f = 1/t or PW = 1/(2*f) time is in microseconds
digitalWrite(speakerOut,HIGH);
delayMicroseconds(ranFreq);
digitalWrite(speakerOut,LOW);
delayMicroseconds(ranFreq);
}
//Pauses between notes
delay(delTime);
}

0 Comments:

Post a Comment

<< Home