External sensor to control parameter in a patch

Hello guys,
I am an absolute newbie, so forgive me for my bad coding!

I am trying to use an external sensor to control my patch.

The datas arrived well in SC since I see them in the Post window: ~sensor.postln;

I am having issues to insert in the following patch:

Thanks

//open serial port

~port =SerialPort.new("/dev/ttyACM0",9600);

// read sensor

(
~charArray=[];
~getValues=Routine({
	var ascii;
	{
		ascii = ~port.read.asAscii;
		if (ascii == $e ,{
		~sensor= ~charArray.collect(_.digit).convertDigits;
		~charArray = [];
		});
	}.loop;
}).play;
)

// post sensor output

~sensor.postln;

// SYNTH FOR SOUND

(
{inf.do(
{	var noise,freq,band,pitch,out,amp=1;
	noise = Dust.ar(LFNoise1.kr(10).exprange(10,200));
	band = MouseY.kr(0.0001,0.01);
	pitch = ~sensor.linlin(0,1000,40,1000);
	freq = [300,400,500,600,700,800,900,1000,1200,1400,1800,2000,2600,3200,4800,5400];
	out = Out.ar ([0,1], Mix.ar(BBandPass.ar(noise, freq * pitch, band)) * amp)
	}.play;
	100.wait;
)}.fork
)
pitch = ~sensor.linlin(0,1000,40,1000);

This will update only once, at the beginning of the synth. It seems very common for new users to assume that “I wrote a variable here, so any future change to the variable’s value should be automatically picked up” – variables don’t work that way.

Since you have multiple synths, a better way might be to write the value to a control bus and read it.

//open serial port

~port = SerialPort.new("/dev/ttyACM0", 9600);

~sensorBus = Bus.control(s, 1);

// read sensor

(
~charArray = [];
~getValues = Routine({
	var ascii;
	{
		ascii = ~port.read.asAscii;
		if (ascii == $e) {
			~sensor = ~charArray.collect(_.digit).convertDigits;
			~sensorBus.set(~sensor);
			~charArray = [];
		};
	}.loop;
}).play;
)

// post sensor output

~sensor.postln;

// SYNTH FOR SOUND

(
{
	inf.do {
		{
			var noise,freq,band,pitch,out,amp=1;
			noise = Dust.ar(LFNoise1.kr(10).exprange(10,200));
			band = MouseY.kr(0.0001,0.01);
			pitch = In.kr(~sensorBus).linlin(0,1000,40,1000);
			freq = [300,400,500,600,700,800,900,1000,1200,1400,1800,2000,2600,3200,4800,5400];
			out = Out.ar ([0,1], Mix.ar(BBandPass.ar(noise, freq * pitch, band)) * amp)
		}.play;
		100.wait;
	}
}.fork
)

(Note that the bracketing structure in your synth loop was not correct – I’ve fixed it here.)

hjh

Thanks a lot for your help, I am trying to learn but the path is always treacherous.

Anyway the patch is still not working properly, for sure for my bad reverse engineering.

So, if I understood something this is how I should use the busses with more sensors (and with a simpler SinOsc):

//open serial port

~port = SerialPort.new("/dev/ttyACM0", 9600);

(
~sensorBus = Bus.control(s, 1);
~sensorBus1 = Bus.control(s, 2);
~sensorBus2 = Bus.control(s, 3);
)
// read sensor

(
~charArray = [];
~getValues = Routine({
	var ascii;
	{
		ascii = ~port.read.asAscii;
		if(ascii.isDecDigit,
		{~charArray = ~charArray.add(ascii)});
		if (ascii == $b ,{
		~light= ~charArray.collect(_.digit).convertDigits;
		~sensorBus2.set(~light);
		~charArray = [];
		});
		if (ascii == $e) {
			~touch = ~charArray.collect(_.digit).convertDigits;
			~sensorBus.set(~touch);
			~charArray = [];
		};
		if (ascii == $f ,{
			~touch3= ~charArray.collect(_.digit).convertDigits;
			~sensorBus1.set(~touch3);
			~charArray = [];
		});
	}.loop;
}).play;
)

// post sensor output

(
Routine({
	{
~light,postln;
~touch.postln;
~touch3.postln;
0.4.wait;
	}.loop;
}).play

// TEST FOR SOUND

(
{
	inf.do{
		{SinOsc.ar(In.kr(~sensorBus1).linlin(0,2000,300,4000),mul:0.5) * Line.ar(1,0,dur:0.04,doneAction:2) !2}.play;
0.5.wait;
}
}.fork
)


thanks again!