‹ OptoforceSuperCollider Firmata 2 ›

SuperCollider Firmata 3

2017-01-03 21:45 supercollider

Reading digital inputs from an Arduino with the SCFirmata is a little bit more complicated than needed.

Here an example that reads 6 analogue and 6 digital at the same time.

NOTE: use resistors (10K) to pull up or pull down the digital inputs. (I couldn't figure out how to activate the built-in pull-ups.)

SerialPort.devices;
d= SerialPort.devices[0]; // or d= "/dev/tty.usbserial-A1001NeZ" - edit number (or string) to match your Arduino
f= FirmataDevice(d);//if it works it should post 'Protocol version: 2.5' after a few seconds

(
~analog= [0, 1, 2, 3, 4, 5];  //A0-A5
~digital= [2, 3, 4, 5, 6, 12];  //some digital input pins
s.latency= 0.05;
s.waitForBoot{
  var freqsArr= 0!~analog.size;
  var ampsArr= 0!~digital.size;
  Ndef(\snd3, {Splay.ar(SinOsc.ar(\freqs.kr(freqsArr, 0.05), 0, \amps.kr(ampsArr.lag(0.01))).tanh)}).play;
  ~analog.do{|x|
    f.reportAnalogPin(x, true);  //start reading analog pins
  };
  f.analogPinAction= {|num, val|
    //[num, val].postln;
    freqsArr.put(~analog.indexOf(num), val);
    Ndef(\snd3).setn(\freqs, freqsArr);
  };
  ~digital.do{|x|
    f.setPinMode(x, \INPUT);
  };
  f.reportDigitalPort(0, true);
  f.reportDigitalPort(1, true);
  f.digitalPortAction= {|port, mask|
    var dig;
    //[port, mask, mask.asBinaryString].postln;
    dig= ~digital.collect{|x| ((mask<<(port*8))&(1<<x)==(1<<x)).binaryValue};
    Ndef(\snd3).set(\amps, dig.postln);
  };
};
)

(
Ndef(\snd3).stop;
~analog.do{|i|
  f.reportAnalogPin(i, false);  //stop reading A0-Anum
};
f.reportDigitalPort(0, false);
f.reportDigitalPort(1, false);
f.end;
f.close;
)

Previous articles...

/f0blog/supercollider-firmata-2

/f0blog/supercollider-firmata

‹ OptoforceSuperCollider Firmata 2 ›