I have some code for a 16 step sequencer but I’m having problems with triggering the events.
I’ll post 2 versions of the code. In the first version the problem is that the event is triggert multiple times but it should only print once to the console:
int savedTime;
int totalTime = 250;
// An array of buttons
Button[] buttons = new Button[80];
int mc = 0;
int rx = -1;
int mx= 0;
void setup() {
size(1200, 400);
savedTime = millis();
// A loop to evenly space out the buttons along the window
for (int j = 0; j < 5; j++) {
for (int i = 0; i < 16; i++) {
buttons[mc] = new Button(i*55+25, j*55+25, 50, 50);
mc += 1;
}
}
}
void draw() {
background(255);
// Show all the buttons
for (int i = 0; i < buttons.length; i++) {
buttons[i].display();
}
// Calculate how much time has passed
int passedTime = millis() - savedTime;
// Has time passed?
if (passedTime > totalTime) {
rx += 1;
mx= rx % 16;
savedTime = millis();// Save the current time to restart the timer!
}
if (buttons[mx].on == true){
println ("EVENT A" +mx);}
if (buttons[mx+16].on == true){
println ("EVENT B" +mx);}
if (buttons[mx+32].on == true){
println ("EVENT C" +mx);}
if (buttons[mx+48].on == true){
println ("EVENT D" +mx);}
if (buttons[mx+64].on == true){
println ("EVENT E" +mx);}
fill(255,0,0);
rect(mx*55+25,0,50,20);
}
void mousePressed() {
// When the mouse is pressed, we must check every single button
for (int i = 0; i < buttons.length; i++) {
buttons[i].click(mouseX, mouseY);
}
}
class Button {
// Button location and size
float x;
float y;
float w;
float h;
// Is the button on or off?
boolean on;
// Constructor initializes all variables
Button(float tempX, float tempY, float tempW, float tempH) {
x = tempX;
y = tempY;
w = tempW;
h = tempH;
on = false; // Button always starts as off
}
void click(int mx, int my) {
// Check to see if a point is inside the rectangle
if (mx > x && mx < x + w && my > y && my < y + h) {
on = !on;
}
}
// Draw the rectangle
void display() {
rectMode(CORNER);
stroke(0);
// The color changes based on the state of the button
if (on) {
fill(175);
} else {
fill(0);
}
rect(x,y,w,h);
}
}
In the second version I put the trigger if statement into the coundown timers loop. That makes the event only being triggered once, but then the whole sequencer is one step behind. I’d like to keep my code as simple as possible but i just can’t get my head around this.
It would be verry nice if someone could help me with this issues. Thanks.
int savedTime;
int totalTime = 250;
// An array of buttons
Button[] buttons = new Button[80];
int mc = 0;
int rx = -1;
int mx= 0;
void setup() {
size(1200, 400);
savedTime = millis();
// A loop to evenly space out the buttons along the window
for (int j = 0; j < 5; j++) {
for (int i = 0; i < 16; i++) {
buttons[mc] = new Button(i*55+25, j*55+25, 50, 50);
mc += 1;
}
}
}
void draw() {
background(255);
// Show all the buttons
for (int i = 0; i < buttons.length; i++) {
buttons[i].display();
}
// Calculate how much time has passed
int passedTime = millis() - savedTime;
// Has time passed?
if (passedTime > totalTime) {
if (buttons[mx].on == true){
println ("EVENT"+1 +mx);}
if (buttons[mx+16].on == true){
println ("EVENT"+2 +mx);}
if (buttons[mx+32].on == true){
println ("EVENT"+3 +mx);}
if (buttons[mx+48].on == true){
println ("EVENT"+4 +mx);}
if (buttons[mx+64].on == true){
println ("EVENT"+5 +mx);}
rx += 1;
mx= rx % 16;
savedTime = millis();
} // Save the current time to restart the timer!
fill(255,0,0);
rect(mx*55+25,0,50,20);
}
void mousePressed() {
// When the mouse is pressed, we must check every single button
for (int i = 0; i < buttons.length; i++) {
buttons[i].click(mouseX, mouseY);
}
}
class Button {
// Button location and size
float x;
float y;
float w;
float h;
// Is the button on or off?
boolean on;
// Constructor initializes all variables
Button(float tempX, float tempY, float tempW, float tempH) {
x = tempX;
y = tempY;
w = tempW;
h = tempH;
on = false; // Button always starts as off
}
void click(int mx, int my) {
// Check to see if a point is inside the rectangle
if (mx > x && mx < x + w && my > y && my < y + h) {
on = !on;
}
}
// Draw the rectangle
void display() {
rectMode(CORNER);
stroke(0);
// The color changes based on the state of the button
if (on) {
fill(175);
} else {
fill(0);
}
rect(x,y,w,h);
}
}