ArduinoToSCSC02.rtf
v1.0b001 081218

Sample Code for SuperCollider to control SC from an Arduino board.

This is exactly the same as ArduinoToSCSC01.rtf, except that it demonstrates the use of a second control.  So you need two, two variable resistors recommended.  If you use potentiometers (recommended), connect the outer connections to 5v and Ground and the central connections analogue inputs 0 and 1.

In order to use it you must install the Arduino quark.  Evaluate the following to see if it's already there:

Quarks.gui;

If not, you should be able to install it with an internet connection and the following:

Quarks.update("arduino");
Quarks.install("arduino");

See the Quarks helpfile for more information.

// Here you'll need to insert the address of your serial driver (see Arduino/Tools/Serial Port/).
// Here are three examples:
g = ArduinoSMS("/dev/tty.usbserial-A4000PfP", 115200); // My NG 
g = ArduinoSMS("/dev/tty.usbserial-A40015oJ", 115200);  // My Diecimilla
g = ArduinoSMS("/dev/tty.usbserial-A40015oJ", 76800);  // My Diecimilla

// Define a synthdef with an Envelope so we don't end up with thousands of the things!
// Now I'm using a second pot to control the attack level of the envelope below (see above).
// As the values coming in are 0 - 1024, I'm dividing them by 1000 so I don't get VERY long sounds!
(
SynthDef(\EnvSine, 
	{ arg freq = 440, myDur = 0.1; 
	// For info:
	// EnvGen.kr(envelope, gate, levelScale, levelBias, timeScale, doneAction)
	// Env.perc(attackTime, releaseTime, peakLevel, curve)
	Out.ar(0, EnvGen.kr(Env.perc((myDur/1000), 0.1, 0.6, -4), 1.0, doneAction: 2) * SinOsc.ar(freq,0,0.1)) }
	).send(s);
)

//  This is the action that actually occurs when you run the read loop or the task...
//  Send it to a synthdef's frequency argument

(
g.action = { 
| ... msg | 
// The messages are coming in on Arduino Analogue inputs 0 and 2 here...
msg[0].postln; // Just for information, also post the value - you can get rid of these if you want.
msg[1].postln;
Synth(\EnvSine, [\freq, msg[0], \myDur, msg[1]]); // Here's the second, new message.

};
)

// Turn the pot and then evaluate the below - you should get a single 'note':
g.send($r, $a); // $r = receive; $a = analogue input; 2 = analogue input 2;

// thor reading loop
(
fork {
	var a;
	1000.do {
		g.send($r, $a); // $r = receive; $a = analogue input; 2 = analogue input 2;
		0.3.wait; // careful with this - it can crash SC if it's too low!
	}
}
)

// If you've finished, you should close the port:
g.close;

// Then we put it into a task, which we can start, pause and resume if we want...
(
t = Task({ 
		500.do({
			g.send($r, $a);
			0.1.wait 
		}); 
	});
)

t.start;
t.pause;
t.resume;
t.reset;
t.stop;

// You must close the port:
g.close;

// Try this, too!

(
g = ArduinoSMS("/dev/tty.usbserial-A40015oJ", 115200);  // My Diecimilla

Tdef(\arduinoReader).play;

Tdef(\arduinoReader,{
	inf.do {
		g.send($r, $a);
		0.15.wait;
	}
}
)
)

g.action = { |... msg| msg.postln; };

//////////////////////////////////////////////////////
//////////////////////////////////////////////////////

// For information, here's working code for the Arduino...
 /*  
 *  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...
 */

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); 
}

To do: check the baud rate for the Arduino:
The arduino board comes with a 16mhz crystal and therefore 115200 isn't a very good choice.  there are optimal baudrates for different crystals.  9600 is a standard albeit slow one that matches most common crystals.  using 115200 at 16mhz will give you an error of -3.5%.  this can result in dropouts and lost messages.  maybe your linux serial driver is more sensitive to this or something.  for 16mhz crystals: 9600, 19200, 38400, 76800, 250000 are all good <0.2% error rates.  see the atmega8 datasheet for more info/tables.
i never understood why the sms helpfiles insists on 115200 - which suits better for 3.686mhz.