Hello everyone,

I've been trying to apply three counter buttons on Sc with success. Although, I want to improve the trigger procedure, which is always late because I'm using a task to evaluate my counter. It means that my message has to follow for another message to be triggered. Does someone has some idea of better implementation on counter buttons on Sc language?

The code follows below:

s.waitForBoot({
	//synths examples
	
	SynthDef(\mic, {| inChan = 0,inPin, outPin| 
		var in, outLed, button;
		in = SoundIn.ar(inChan);
		in.poll;
		
		button = DigitalIn.ar(inPin);
		outLed = DigitalOut.ar(outPin, button); //Light  Digital pin 1 - Button
	    
	    Out.ar(0,in!2);
	    
	}).send(s);
	
	SynthDef (\shh, {
			arg out=~outBack, t_trig=0;
			var sound, env;
			sound = Mix(LFPulse.ar(110*[1,5/2],0.0,0.5,0.2));
			env = EnvGen.ar(Env.perc(0.02,4), t_trig);
			sound = Pan2.ar(sound * env,0.0);
			Out.ar(out, sound);
		},[\tr]).send(s);

  s.sync;
  

	~buttonMonitor = {
			 var d1 = DigitalIn.kr(1); // Digital pin 1 - Button
			 var d2 = DigitalIn.kr(2); // Digital pin 2 - Button
			 var d3 = DigitalIn.kr(3); // Digital pin 3 - Button
			
		
	//Analog	(in the future)
	// ~ctrl = {
	// 	var a0 = AnalogIn.ar(0) * 0.5;
	// 	var a1 = AnalogIn.ar(1) * 0.5;
	// 	SendReply.kr(Impulse.kr(10), '/ctrl', [a0, a1]);
	// }.play;

	// ~dur = 0.2;
	// ~amp = 0.8;
	// ~pitch = 300;
	// OSCdef('listen', {arg msg;
	// 	~amp = msg[3].linexp(0.0, 1.0, 0.1, 0.8);
	// 	~dur = msg[4].linexp(0.1, 1.0, 1.0, 0.01);
	// 	~pitch = msg[4].linlin(0.0, 1.0, 80, 800);
	// 	//[~amp, ~dur, ~pitch].postln;
	// }, '/ctrl');


	
SendReply.kr(Changed.kr(d1+d2+d3), '/buttonMonitor', [ d1, d2, d3]);
		}.play;


//counter list
a = Routine.new({ 1.yield; 2.yield;  3.yield; 4.yield; 5.yield; 6.yield; 7.yield; 8.yield; 9.yield; 10.yield});
b = Routine.new({ 1.yield; 2.yield;  3.yield; 4.yield; 5.yield; 6.yield; 7.yield; 8.yield; 9.yield; 10.yield});
c = Routine.new({ 1.yield; 2.yield;  3.yield; 4.yield; 5.yield; 6.yield; 7.yield; 8.yield; 9.yield; 10.yield});


		~button1 = 0;  
		~button2 = 0;   
		~button3 = 0;   


// Listen to the buttons
		OSCdef('listenToButtons', {
			arg msg;
			// Buttons
			~trigger1 = msg[3].asInteger;
			~trigger1.postln;
			~trigger2 = msg[4].asInteger;
			~trigger3 = msg[5].asInteger;
			
			
		//counter
			
			if(msg[3] == 1,{ ~button1 = a.next;  });  //["Scene: " ++ ~button1].postln; 
			if(msg[4] == 1,{ ~button2 = b.next;  });
			if(msg[5] == 1,{ ~button3 = c.next;  }); //msg[5].asInteger;

	
			
			//Clocker to check result and restart in 1.5 SuperCollider

			(
			t = Task({
				
				2.wait;
				if(msg[3] == 1,{
				~resultbutton1 = ~button1;
				["Scene: " ++ ~resultbutton1].postln;
				});
				if(msg[4] == 1,{
				~resultbutton2 = ~button2;
				["Synth: " ++ ~resultbutton2].postln; 
				});
				if(msg[5] == 1,{
				~resultbutton3 = ~button3;
				["Parameters: " ++ ~resultbutton3].postln
				});
				
			    2.yield;
				    a.reset;
				    b.reset;
				    c.reset;
			    });
			t.start;
			);
		
		
		~resultbutton1.postln;
		
		//working
		// q = Synth(\shh);
    	// 	q.set(\t_trig, ~trigger1);
 		
 				if(~resultbutton1 == 1, {
				q = Synth(\shh);
 				q.set(\t_trig, ~trigger1)
			    });
 		
		}, '/buttonMonitor');