| 1 | /* |
| 2 | * Bit-bang sound |
| 3 | * <~~Harvie 2oo8 |
| 4 | */ |
| 5 | |
| 6 | #define sndout 13 |
| 7 | |
| 8 | char mixer; |
| 9 | |
| 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); |
| 18 | } |
| 19 | pinMode(sndpin, INPUT); //Close pin to avoid noise (optional) |
| 20 | } |
| 21 | |
| 22 | void setup() { // run once, when the sketch starts |
| 23 | pinMode(sndout, OUTPUT); |
| 24 | pinMode(sndout-1, INPUT); |
| 25 | } |
| 26 | |
| 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); */ |
| 34 | //delay(2000); |
| 35 | } |
| 36 | |