Newbie question: step sequencing position with buttons

Hello everybody, i’m new here and English is not my first language so please be gentle with me!

I’m a beginner with SC and i’m studying it at conservatory of Milan, and i’m trying to code a drum machine with a 16-step sequencer with a row of button who indicate the position of the current step.

I know that maybe is not the most efficent or elegant way to do it, but i was thinking on using a routine with a inf.Do function that dictate the infinite step sequencing.
my actual problem is: i want to use an array of button written like this:

b = Array.fill(16, {
	16.do{
		|butt, i|
		Button.new(w, Rect(i*30, 100, 30, 30))
		.states_([
			["", Color.red, Color.red],["", Color.white, Color.white]
		])
	};
	
});

but unfortunally i have no idea to how integrate the .valueAction_ properly to turning on and off the buttons sequentially with the value of the routine.

I know it’s a mere question of estetic and maybe sound easy to a lot of people, but I would be very pleased if someone could help me, thank you all in advance!!

I’d suggest: first, be clear what Array.fill does:

Array.fill(5, { |i| i.postln });

// prints
0
1
2
3
4
-> [ 0, 1, 2, 3, 4 ]

Then, what does do do?

5.do, { |i| i.postln };

// prints
0
1
2
3
4
-> 5  // <-- note, no array result here!
  1. They both loop. So your code example is doing 16x16 = 256 cycles, not only 16.
  2. do throws away the function results so it’s meaningless to create the buttons inside a do.

It’s just this:

b = Array.fill(16, { |i|
	Button.new(w, Rect(i*30, 100, 30, 30))
});

You don’t need valueAction for this; value_ is enough.

With the above, you can access any button by index: b[0] is the first, b[1] is the second etc.

hjh

1 Like

This may be interesting to you:

http://sccode.org/1-5fe

There are two instances of .asInt that should be changed to .asInteger, to remove the deprecated warning.

I don’t personally work with the graphical side of SC very often, and this actually seems a little too complex to suit your needs… but it may be worth checking out, at the very least.

1 Like

This one works… a little closer to an actual step sequencer: [tb303]

It’s still not a solution to what you’re looking for, but it may be of similar interest.

1 Like

Thank you really much, it helped me a lot!!