You are on page 1of 2

/*Arduino2Max PIR sensor controller for Max/MSP music playback

This Arduino sketch creates an interface between the four


Infrared Motion Sensors and the Max/MSP environment through
the computer's serial port.

When a change is detected (i.e. one of the sensors is triggered


by someone moving into its range), the HIGH value for that spefific
pin is sent by the Arduino board to Max/MSP through a Serial port
connection, triggering the MAX object that switches on the corresponding
output channel (i.e. speaker).

Based on the 'Arduino2Max with DigOut' Max patch and Arduino sketch by
Daniel Jolliffe*/

//VARIABLE DECLARATION

int x = 0; //Variable used to store pin values, 0 by default

int ledPin1 = 10; //Indicator LEDs output pins 10 to 13


int ledPin2 = 11;
int ledPin3 = 12;
int ledPin4 = 13;

int serialValue; //Variable used to store values from Serial.read

//SETUP FUNCTION

void setup() {
Serial.begin(115200); //Sets the data rate in bits per second (baud) for
//serial data transmission. 115200 is the default
//Bluetooth speed for Arduino.

pinMode(10,OUTPUT); //LED pins declared as outputs


pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);

digitalWrite(10, HIGH); //Initialization LED blink sequence


delay(200);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
delay(200);
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
delay(200);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);
}
//LOOP FUNCTION

void loop() {
if (Serial.available() > 0) { //Checks serial port buffer for
serialValue = Serial.read(); //incoming characters.
if (serialValue == 'r') { //If an 'r' character is received
//the pins are read.

for (int pin = 0; pin<=5; pin++) { //Reads analog pins 0 to 6.


x = analogRead(pin);
sendValue(x);
}

for (int pin = 2; pin<=5; pin++) { //Reads digital pins 2 to 5.


x = digitalRead(pin);
sendValue(x); //Sends pin value LOW (0) or HIGH (1) to Max.
}

Serial.println();//Sends a 'carriage return' message to mark data end


delay(5); //5ms delay is added to avoid overloading the serial port.
}

if(serialValue == 0) { //Receives numbers from Digital Out section


digitalWrite(10, LOW);
} //0 and 1 control LED 1 (0 = OFF, 1 = ON)
else if(serialValue == 1) {
digitalWrite(10, HIGH);
}

if(serialValue == 2) { //2 and 3 control LED 2 (2 = OFF, 3 = ON)


digitalWrite(11, LOW);
}
else if(serialValue == 3) {
digitalWrite(11, HIGH);
}

if(serialValue == 4) { //4 and 5 control LED 3 (4 = OFF, 5 = ON)


digitalWrite(12, LOW);
}
else if(serialValue == 5) {
digitalWrite(12, HIGH);
}

if(serialValue == 6) { //6 and 7 control LED 4 (6 = OFF, 7 = ON)


digitalWrite(13, LOW);
}
else if(serialValue == 7) {
digitalWrite(13, HIGH);
}
}
}

void sendValue (int x){ //Sends pin value followed by a 'space'(32)


Serial.print(x);
Serial.write(32);
}

//END OF CODE

You might also like