 /*  
 *  ArduinoToSC
 *  Send pin values from Arduino to MAX/MSP and SuperCollider
 *  
 *  ArduinoToSC.pde
 *  ------------  
 *  Latest update: November 2007
 *  ------------
 *  Copyleft: use as you like
 *  by djmatic 
 *  Based on a sketch and patch by Thomas Ouellet Fredericks  tof.danslchamp.org
 *  Modified by Richard Hoadley
 *  
 * 
 * 
 * Don't worry about the LED (which you can connect to digital 13 if you want) as it doesn't contribute anything new.
 * Attach a potentiometer to 5v and Ground (two outer connections), and the sensor (central connection) to Analogue in 5.
 * 
 * In fact, the code below reads ALL the analogue pins, so you can use any...
 */


#include "WProgram.h"
void setup();
void loop();
void sendValue (int x);
int x = 0;                              // a place to hold pin values
int ledpin = 13;

void setup()
{
  Serial.begin(115200);               // 115200 is the default Arduino Bluetooth speed
  digitalWrite(13,HIGH);              ///startup blink
  delay(600);
  digitalWrite(13,LOW);
  pinMode(13,INPUT);
}



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
  
  }

 }
}

void sendValue (int x){              // function to send the pin value followed by a "space". 
 Serial.print(x);
 Serial.print(32, BYTE); 
}


int main(void)
{
	init();

	setup();
    
	for (;;)
		loop();
        
	return 0;
}

