Arduino+SC Patterns

I generate this code from YouTube on Arduino++SC; I know that the ~ val controls Synthdef, but I want to know how I can create code in SC that with this particular Arduino code “Water and Soil sensor”, I can control a Pattern?

ARDUINO:

int value;


void setup() {
 Serial.begin(115200);
}

void loop() {
  value = analogRead(0);
  Serial.print(value);
  Serial.print("a");
 delay(1); 
}

SUPERCOLLIDER:

SerialPort.devices;
(
~port = SerialPort.new("/dev/ttyACM0", 115200);
)
(
x = [];
30.do{x = x.add(~port.read)};
x.collect(_.asAscii);
)
~port.close;

(
~charArray = [];
~getValues = Routine.new({
	var ascii;
	{
		ascii = ~port.read.asAscii;
		if(ascii.isDecDigit, {~charArray = ~charArray.add(ascii)});
		if(ascii == $a, {
			~val = ~charArray.collect(_.digit).convertDigits;
			~charArray = [];
		});
	}.loop;
}).play;
)

(
SynthDef.new(\saw, {
	arg cutoff = 1000;
	var sig;
	sig = Saw.ar([50,100]);
	sig = RLPF.ar(sig, cutoff.lag(0.02), 0.25, 0.2);
	Out.ar(0, sig);
}).add;
)

(
~synth = Synth(\saw, [\cutoff, 200]);
~synth.set(\cutoff, 40000);
)

(
~synth.free;
)

(
~synth = Synth(\saw, [\cutoff, ~val.linexp(0, 1023, 80, 4000)]);
)

(
~control = Routine.new({
	{
		~synth.set(\cutoff, ~val.linexp(0, 1023, 80, 4000));
		0.01.wait;
	}.loop;
}).play;
)

I think that before considering using patterns you should think about the logical section of your code’s design.

You are using analogRead() which means that you serial port would be continuously sending values to the sclang. The most “commom” usage would be to map this continuous input directly to a synth parameter (pitch, cutoff frequency, pan, etc).
Therefore using a pattern for, let’s say, controlling the pitch values of a synth would be a little strange since most of the patterns are based on events, which are long term occurrences in discrete time.

So, a more “tipical” scenario would be processing the analogRead to generate triggers (e.g > threshold a finite duration pattern is played) or extract features like Onsets and use them as triggers.

Take a look at Fredrik Olofsson materials regarding Arduino + SuperCollider:
Udk00-audiovisual programming by redFrik more specifically:
GitHub - redFrik/udk13-Remote_control: UdK audiovisual programming course resources 2015_SS

For triggering patterns it is basically taking yourSerialPortVariable > 33 a if and your pattern.

I saw the YouTube material and in the Book of Eli Fieldsteel to construct this code, I see now it is more complex to trigger patterns, and I will review the Olofsson materials, to answer to you in the future, now I dont have the knowledge to understand you answer propperly.

Thanks.

I have an intermediate level in SC and the same in Arduino, thats why.