/* should keep something within a range */ #define CLAMP(X,MIN,MAX) X = ( (XMAX) ? MAX : X ) #define MIN_FREQ 300 #define MAX_FREQ 3000 #define SEQUECE_LENGHT 100 /* how high does it go ? */ int seqFreq[SEQUECE_LENGHT]; /* sequenceFrequency */ int seqDur[SEQUECE_LENGHT]; /* sequence duration */ int Z, Y, X; /* mouse positions */ byte statePin = LOW; /* for blinking */ byte playMode = 0; float modifier = 1.0; /* playbackspeed modifier */ int head, tail, index; /* queue housekeeping */ /* * Pin 5 is the mouse data pin, pin 6 is the clock pin * Pin 6 is connected to the speaker * Pin 13 is used because the dicimillia board has a led on it * Feel free to use whatever pins are convenient. */ #define MDATA 5 #define MCLK 6 #define outpin 3 #define ledPin 13 /* mousestuff */ //#define verbose // for verbose output from the mouse unsigned char msInitSequence[] = { 0xF3, 0xC8, 0xF3, 0x64, 0xF3, 0x50, 0xF2 }; void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); pinMode(5, INPUT); randomSeed(analogRead(0)); mouse_init(); tail = index = 0; head=1; } void loop() { /* blink */ statePin = !statePin; digitalWrite(ledPin, statePin); if (!playMode) { sequence(); /* get frequency or change it */ if (seqFreq[index] == 0 ) { //set to some default values seqFreq[index] = 440; seqDur[index] = 200; } seqFreq[index] = seqFreq[index] + 15 * X + Y; X = Y = 0; // go back to zero CLAMP(seqFreq[index], MIN_FREQ, MAX_FREQ); /* play the sound */ freqout(seqFreq[index], seqDur[index]); } else { /* playmode */ /* adjust index */ index ++; if (index==SEQUECE_LENGHT) index=0; if (index==head) index = tail; /* adjust modifier */ if (Z!=0) modifier += .01*Z; Z=0; //back to zero // CLAMP(modifier, 0.001, 2); freqout(seqFreq[index], seqDur[index] * modifier); } /* a lot of usefull debugging information */ /* showVal(seqFreq[index], "frequency "); */ /* showVal(seqDur[index], "duration "); */ /* showVal((modifier*1000), "modifier "); */ /* showVal(head, "head "); */ /* showVal(tail, "tail "); */ /* showVal(index, "index "); */ /* Serial.println(); */ readMouse(); } void readButtonsAndWheel() { char mstat = mouse_read(); char i; char bit = 0x01; for(i = 0; i<8; i++) { if(mstat&bit) { switch(i) { case 0: /* Serial.print("left button pressed \t"); */ if (!playMode) { seqDur[index] +=20; } else { modifier /=2; } break; case 1: /* Serial.print("Right button pressed \t"); */ if (!playMode) { seqDur[index] -=20; } else { modifier *=2; } break; case 2: /* Serial.print("Middle Button Pressed \t"); */ if (!playMode) { /* Serial.println("Play mode \t"); */ playMode = 1; index = tail; Z=0; modifier = 1.0; } else { /* Serial.println("Sequence mode \t"); */ playMode = 0; index = tail; Z = index; } break; } } bit = bit<<1; } } /* This function adjust the index if we need to change it, if the Z-value is different from index */ void sequence() { if(Z>index) { if (++index == SEQUECE_LENGHT) { index = 0; } addToHead(); } else if (Z>i) & 0x01 ; sendBit(bit); parity = parity ^ (bit); } /* parity */ sendBit(parity); /* stop bit */ sendBit(1); /* wait for mouse to switch modes */ while ((digitalRead(MCLK) == LOW) || (digitalRead(MDATA) == LOW)) ; /* put a hold on the incoming data. */ golo(MCLK); } /* * Get a byte of data from the mouse */ char mouse_read(void) { char data = 0x00; int i; /* start the clock */ gohi(MCLK); gohi(MDATA); /* delayMicroseconds(50); */ readBit(); // startbit for (i=0; i < 8; i++) { data = data | (readBit()<