10 void sound(char sndpin, float freq, float duration) { //Play bit-bang sound
11 if(duration<=0) return; if(freq<=0) { delay(duration); return; }
12 freq=((1000000/2)/freq); //Convert freq to delay (us)
13 duration*=1000; //Convert duration to us
14 pinMode(sndpin, OUTPUT);
15 for(;duration>0;duration-=2*freq) {
16 digitalWrite(sndpin, HIGH); delayMicroseconds(freq);
17 digitalWrite(sndpin, LOW); delayMicroseconds(freq);
19 pinMode(sndpin, INPUT); //Close pin to avoid noise (optional)
22 void setup() { // run once, when the sketch starts
23 pinMode(sndout, OUTPUT);
24 pinMode(sndout-1, INPUT);
27 void loop() { // run over and over again
28 //delay(10000); return;//Silence! I'll kill you!
29 //float i=0; while(1) { sound(sndout, sin(i+=0.01)*550, 10); sound(sndout, cos(i)*400, 10); }
30 digitalWrite(sndout, digitalRead(sndout-1));
31 /* int octave, duration;
32 char melody[]="CCDEECCFFAAGGE-CCDEGGEFEDDCC-CCDEECCFFAAGGE-CCDEGGEFEDDC"; octave=1; duration=300; //Bob Marley's Redemption song - intro ;D
33 play_melody(sndout, melody, octave, duration); */