Saturday, November 04, 2006

Give and Receive

I made my little sound generator send data to processing to create art based off the frequency and duration.

Arduino Code:

Click here for the Processing Code


#define header 224 //224-239 are pitch bend outputs in Midi

// Variables:
int speakerOut = 9; //Speaker Out
int delTime = 0; //Delay Time
int ranFreq = 0; //Pulse Width (PW)
int maxTime = 10; //Controls the length of Sound
int analogPin = 0; // Digital input pin for a switch
int ledPin = 13; // Digital output pin for on-board LED
int analogValue = 0; // value from the analog input, Analog to Digital Converter (ADC)
int frqValMSB = 0;
int frqValLSB = 0;
int scaledValue = 0; // scaled value for MIDI transmission
byte testbyte = 0;

void setup() { // Here we set the baud rate,
Serial.begin(9600); // Note that midi is just regular serial with a specified
pinMode(speakerOut, OUTPUT);// Sets output Pin
}


void sendData3Byte(char cmd, char data1, char data2, char data3) {
Serial.print(cmd, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE);
Serial.print(data3, BYTE);
}

void loop() {
ranFreq = random (150,2500);
maxTime = random (5,60);
delTime = random (10,500);
//This for loop sets the length of the note
for (int i = 0; i < maxTime; i++){
//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);
}
frqValLSB = ranFreq & 127; //Keep lower 7 bits bits, 0000000011111111 = 127
frqValMSB = (ranFreq >> 7) ; //Need to shift the MSB into to the lower 7 bits
sendData3Byte(header, frqValLSB,frqValMSB, maxTime); //224 LSB MSB = header command, LSB, MSB
//Pauses between notes
delay(delTime);

}

0 Comments:

Post a Comment

<< Home