Submitted by f0 on
+2 years ago i put up a simple example of how to use firmata with arduino and supercollider here. that code still work but it only show how to read a single analog input on the arduino.
here is how one can read both A0 and A1 and map those to synth parameters in supercollider...
//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 analog inputs (A0-A5) one can do...
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 analog 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;
)