/* Ryan Raffa - Arduino communication to Max MSP sequencer with LED lights Credits: Made use of Arduino2Max.pde Copyleft: use as you like by Daniel Jolliffe Based on a sketch and patch by Thomas Ouellet Fredericks tof.danslchamp.org */ int i; int x = 0; // a place to hold pin values const int buttonPinOne = 2; const int ledPinOne = 9; const int buttonPinTwo = 3; const int ledPinTwo = 10; const int buttonPinThree = 4; const int ledPinThree = 11; const int buttonPinFour = 5; const int ledPinFour = 12; const int buttonPinFive = 6; const int ledPinFive = 13; //From Arduino.cc - State Change example // Variables will change: int buttonPushCounterOne = 0; // the current state of the output pin int buttonStateOne = 0; // the current reading from the input pin int lastButtonStateOne = 0; // the previous reading from the input pin int buttonPushCounterTwo = 0; // the current state of the output pin int buttonStateTwo = 0; // the current reading from the input pin int lastButtonStateTwo = 0; // the previous reading from the input pin int buttonPushCounterThree = 0; // the current state of the output pin int buttonStateThree = 0; // the current reading from the input pin int lastButtonStateThree = 0; // the previous reading from the input pin int buttonPushCounterFour = 0; // the current state of the output pin int buttonStateFour = 0; // the current reading from the input pin int lastButtonStateFour = 0; // the previous reading from the input pin int buttonPushCounterFive = 0; // the current state of the output pin int buttonStateFive = 0; // the current reading from the input pin int lastButtonStateFive = 0; // the previous reading from the input pin void setup() { Serial.begin(115200); // 115200 is the default Arduino Bluetooth speed digitalWrite(ledPinOne,HIGH); ///startup blink digitalWrite(ledPinTwo,HIGH); ///startup blink digitalWrite(ledPinThree,HIGH); ///startup blink digitalWrite(ledPinFour,HIGH); ///startup blink digitalWrite(ledPinFive,HIGH); ///startup blink delay(600); digitalWrite(ledPinOne,LOW); ///startup blink digitalWrite(ledPinTwo,LOW); ///startup blink digitalWrite(ledPinThree,LOW); ///startup blink digitalWrite(ledPinFour,LOW); ///startup blink digitalWrite(ledPinFive,LOW); ///startup blink pinMode(buttonPinOne, INPUT); pinMode(buttonPinTwo, INPUT); pinMode(buttonPinThree, INPUT); pinMode(buttonPinFour, INPUT); pinMode(buttonPinFive, INPUT); pinMode(ledPinOne, OUTPUT); pinMode(ledPinTwo, OUTPUT); pinMode(ledPinThree, OUTPUT); pinMode(ledPinFour, OUTPUT); pinMode(ledPinFive, OUTPUT); } void loop() { if (Serial.available() > 0){ // Check serial buffer for characters if (Serial.read() == 'r') { // If an 'r' is received then read the pins for (int pin= 0; pin<=5; pin++){ // Read and send analog pins 0-5 x = analogRead(pin); sendValue (x); } for (int pin= 2; pin<=13; pin++){ // Read and send digital pins 2-13 x = digitalRead(pin); sendValue (x); } Serial.println(); // Send a carriage returnt to mark end of pin data. delay (5); // add a delay to prevent crashing/overloading of the serial port } } //check pin 2 buttonStateOne = digitalRead(buttonPinOne); if (buttonStateOne != lastButtonStateOne) { if (buttonStateOne == HIGH) { buttonPushCounterOne++; } lastButtonStateOne = buttonStateOne; } if (buttonPushCounterOne % 2 == 0) { digitalWrite(ledPinOne, LOW); } else { digitalWrite(ledPinOne, HIGH); } //check pin 3 buttonStateTwo = digitalRead(buttonPinTwo); if (buttonStateTwo != lastButtonStateTwo) { if (buttonStateTwo == HIGH) { buttonPushCounterTwo++; } lastButtonStateTwo = buttonStateTwo; } if (buttonPushCounterTwo % 2 == 0) { digitalWrite(ledPinTwo, LOW); } else { digitalWrite(ledPinTwo, HIGH); } //check pin 4 buttonStateThree = digitalRead(buttonPinThree); if (buttonStateThree != lastButtonStateThree) { if (buttonStateThree == HIGH) { buttonPushCounterThree++; } lastButtonStateThree = buttonStateThree; } if (buttonPushCounterThree % 2 == 0) { digitalWrite(ledPinThree, LOW); } else { digitalWrite(ledPinThree, HIGH); } //check pin 5 buttonStateFour = digitalRead(buttonPinFour); if (buttonStateFour != lastButtonStateFour) { if (buttonStateFour == HIGH) { buttonPushCounterFour++; } lastButtonStateFour = buttonStateFour; } if (buttonPushCounterFour % 2 == 0) { digitalWrite(ledPinFour, LOW); } else { digitalWrite(ledPinFour, HIGH); } //check pin 6 buttonStateFive = digitalRead(buttonPinFive); if (buttonStateFive != lastButtonStateFive) { if (buttonStateFive == HIGH) { buttonPushCounterFive++; } lastButtonStateFive = buttonStateFive; } if (buttonPushCounterFive % 2 == 0) { digitalWrite(ledPinFive, LOW); } else { digitalWrite(ledPinFive, HIGH); } } void sendValue (int x){ // function to send the pin value followed by a "space". Serial.print(x); Serial.print(32, BYTE); }