#include /* * Virtual SQRLS Program * * Requires the Metro Arduino libary: http://www.arduino.cc/playground/Code/Metro */ int speakerOnePin = 5; int speakerTwoPin = 6; int activeSpeaker = speakerOnePin; int volumePin = 9; int motionSensorPin = 3; int defaultVolume=12; int b = 0; int bb = 0; int OCC=0; int TSL=0; int TIM=0; boolean logicLock = 0; void setup() { noInterrupts(); pinMode(motionSensorPin, INPUT); pinMode(speakerOnePin, OUTPUT); pinMode(speakerTwoPin, OUTPUT); pinMode(volumePin, OUTPUT); // Set pins 5&6 to a 62500Hz PWM clock TCCR0B = TCCR0B & 0b11111000 | 0x01; TCCR1B = TCCR1B & 0b11111000 | 0x01; TIM = random(200,400); // Sets a starting volume analogWrite(volumePin,defaultVolume); MsTimer2::set(1000, handleLogic); // 10ms period MsTimer2::start(); // interrupt 1 = pin 3 attachInterrupt(1, motionOn, RISING); attachInterrupt(1, motionOff, FALLING); interrupts(); } void loop(){ } void handleLogic() { if(logicLock) { return; } logicLock = 1; TSL++; if(TSL=TIM){ caw(3); } else if (TSL = 2*TIM) { caw(4); caw(5); } else if (TSL = 2*TIM+30) { caw(3); caw(4); caw(5); OCC--; } else if (TSL = 2*TIM+50) { if(OCC<0){ caw(4); caw(5); caw(6); caw(7); barker(6); } } else if(TSL % (2*TIM+50) == 0) { caw(1); caw(2); OCC--; } if(TSL > 10000) { TSL = 0; } logicLock = 0; } void chooseSpeaker() { if(random(5) == 1) { activeSpeaker = speakerTwoPin; } else { activeSpeaker = speakerOnePin; } } void motionOn() { analogWrite(volumePin, 0); if(TSL>200) { OCC++; } } void motionOff() { analogWrite(volumePin, defaultVolume); TSL=0; TIM = random(200,400); } // Caw sound. eg, // caw(1000); // delay(200000); void caw(int ll) { for(int j=1800; j<1900; j+=1000/ll) { if(j<1816){ analogWrite(volumePin,(j-1815)*4/5+12); } if(j>1875){ analogWrite(volumePin,(1875-j)/4+12); } b++; if (b==10){ bb=1; b=0; } sawWave(j/8+500*bb+random(10)); bb=0; } } // Bark sound. eg, // barker(random(3)); void barker(int num) { for(int u=0;u200; j-=3000/ll) { sawWave(j+random(50)); } } // Generic triangle wave sound void triangleWave(int len) { for(int i = 0; i < len; i++) { analogWrite(activeSpeaker, i*255/len); } for(int i = 0; i < len; i++) { analogWrite(activeSpeaker, 255 - i*255/len); } } // Generic saw wave sound void sawWave(int len) { for(int i = 0; i < len; i++) { analogWrite(activeSpeaker, i); } }