SuperCollider Firmata 2
2017-01-02 18:12
supercollider
+2 years ago I put up a simple example of how to use firmata with Arduino and SuperCollider. See /f0blog/supercollider-firmata/. That code still works but it only shows how to read a single analogue input on the Arduino.
Here is how one can read both A0 and A1 and map those to synth parameters in SuperCollider...
//how to read pins A0 and A1 with SCFirmata...
//tested with Arduino1.8.0 and SC3.8.0
//first in Arduino IDE:
// * select File / Examples / Firmata / StandardFirmata
// * upload this example to an Arduino
//then in SC install the SCFirmata classes
// * download zip file https://github.com/blacksound/SCFirmata
// * extract files and put them in your SC application support directory
// * recompile SC
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
s.boot
(
Ndef(\snd, {|freq1= 400, freq2= 500, amp= 0.5| SinOsc.ar([freq1, freq2].lag(0.08), 0, amp.lag(0.08)).tanh}).play;
f.reportAnalogPin(0, true); //start reading A0
f.reportAnalogPin(1, true); //start reading A1
f.analogPinAction= {|num, val|
[num, val].postln;
switch(num,
0, {
Ndef(\snd).set(\freq1, val.linexp(0, 1023, 400, 800)); //A0 mapped to freq1
},
1, {
Ndef(\snd).set(\freq2, val.linexp(0, 1023, 400, 800)); //A1 mapped to freq2
}
);
};
)
(
Ndef(\snd).stop;
f.reportAnalogPin(0, false); //stop reading A0
f.reportAnalogPin(1, false); //stop reading A1
f.end;
f.close;
)
And to read all six analogue inputs (A0-A5) one can do...
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
s.boot
~numberOfAna= 6; //number of analogue inputs (here A0-A5)
(
var freqsArr= 0!~numberOfAna;
Ndef(\snd2, {|amp= 0.5| Splay.ar(SinOsc.ar(\freqs.kr(freqsArr, 0.05), 0, amp.lag(0.08)).tanh)}).play;
~numberOfAna.do{|i|
f.reportAnalogPin(i, true); //start reading A0-Anum
};
f.analogPinAction= {|num, val|
[num, val].postln;
freqsArr.put(num, val);
Ndef(\snd2).setn(\freqs, freqsArr);
};
)
(
Ndef(\snd2).stop;
~numberOfAna.do{|i|
f.reportAnalogPin(i, false); //stop reading A0-Anum
};
f.end;
f.close;
)