« 3 DAY HARD-CORE PROGRESS | Main | P-COMP FINAL STRETCH »

THE PROGRAMMING PROGRESS

Here is the Arduino Code:

int switchPin = 2; // digital input pin for a switch/trigger
int switchState = 0; // the state of the switch
int ultraSoundSignalout = 8; // Ultrasound signal out pin
int ultraSoundSignalin = 7; // Ultrasound signal in pin
int val = 0;
byte ultrasoundValue = 0;
int timecount = 0; // Echo counter
int ledPin = 13; // LED connected to digital pin 13
int IRsensor = 5; //analog pin for analog IR detector
int IRvalue = 0; // value from the analog input
int potPin = 4; // analog pin for potentiometer
int potValue = 0; // value from pot


void setup() {
pinMode(switchPin, INPUT); // set the switch pin to be an input
pinMode(ledPin, OUTPUT); // set the red LED pin to be an output
pinMode(potPin, OUTPUT);
// Set MIDI baud rate:
Serial.begin(31250);

}


void loop() {
IRvalue = analogRead(IRsensor); //read the state of IR
IRvalue /= 8;

timecount = 0;
val = 0;

switchState = digitalRead(switchPin);

//if the switch is pressed then do everything
if(switchState == 1){
digitalWrite(ledPin, HIGH); // turn on the yellow LED
pinMode(ultraSoundSignalout, OUTPUT); // Switch signalpin out to output

/* Send low-high-low pulse to activate the trigger pulse of the sensor
* -------------------------------------------------------------------
*/
digitalWrite(ultraSoundSignalout, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignalout, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignalout, LOW); // Holdoff

/* Listening for echo pulse
* -------------------------------------------------------------------
*/
pinMode(ultraSoundSignalin, INPUT); // Switch signalpin in to input
val = digitalRead(ultraSoundSignalin); // Append signal value to val

while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignalin);
}

while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignalin);
timecount = timecount +1; // Count echo pulse time
}

ultrasoundValue = timecount/8; // Append echo pulse time to ultrasoundValue
//Serial.print(ultrasoundValue, BYTE);
midiOut(176, 1, timecount);

// read the IR input:
IRvalue = 0;
for(int i = 0; i < 10; i++){
IRvalue += analogRead(IRsensor);
}
//divide by ten to get the average value
IRvalue = IRvalue / 10;
//divide by 8 to send a value between 0-127
IRvalue = IRvalue / 4;
midiOut(176, 2, IRvalue);

// read pot value and spit it out to later control volume
potValue = 0;
potValue = analogRead(potPin);
potValue /= 8;
midiOut(176, 3, potValue);
}
//else turn off the led and send a zero
else {
// Serial.print(0,BYTE);
midiOut(176, 1, 0);
midiOut(176, 2, 0);
midiOut(176, 3, 0);
digitalWrite(ledPin, LOW);
}

}
// sends a MIDI message. Doesn't check to see that
// cmd is greater than 127, or that data values are less than 127:
void midiOut(char cmd, char data1, char data2) {
Serial.print(cmd, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE); //sends the values of three switches

}

One Max patch still retains the gun-like sounds and has a good slider effect.

max_final_1.png

The other Max patch uses a polyphonic voice and a crazy slider too.

max_final_2.png

TrackBack

TrackBack URL for this entry:
http://itp.nyu.edu/~bp432/cgi-bin/mt/mt-tb.cgi/38