We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am trying to create an array of buttons which has an integer associated with each button. Then, when a single button is clicked, this value is sent to an Arduino. I left out the serial communication part of the code. I know how to handle that portion. What I am stuck on is coming up with a simple way to associate integers 0-9 with buttons 0-9.
I know I can simply write out a brute force version of the code to associate each element of my array of buttons with a specific interger but I'd like to know how to write this simply, maybe using iteration.
Here is the code I have so far. I commented out the bit of code which I tried. I feel like its on the right track, but missing something crucial. I'm still fairly new to Processing and coding in general.
I'm using the button class from one of the examples in Processing. Not sure which one.
int unit = 40;
int count;
int num;
int butVal;
Button[] buts;
void setup() {
size(400, 200);
colorMode(RGB);
int wide = width/unit;
count = wide;
buts = new Button[count];
int index = 0;
for (int x = 0; x < wide; x++) {
buts[index++] = new Button(x*unit, 50, 25, color(255, 0, 0), color(0, 255, 0), color(0, 0, 255));
}
}
void draw() {
background(0, 0, 0);
for (Button but : buts) {
but.update();
but.display();
/*
for (int v = 0; v <= buts.length; v++) {
butVal[v] = v;
if (but.pressed)println(butVal[v]);
}
*/
}
for ( int t = 0; t <= 10; t ++) {
num = t;
fill(0, 255, 0);
text(num, 40*t, 40);
}
}
void mousePressed() {
for (Button but : buts) {
but.press();
}
}
void mouseReleased() {
for (Button but : buts) {
but.release();
}
}
//_____________________________//
class Button {
int x, y; // The x- and y-coordinates
int size; // Dimension (width and height)
color baseGray; // Default gray value
color overGray; // Value when mouse is over the button
color pressGray; // Value when mouse is over and pressed
boolean over = false; // True when the mouse is over
boolean pressed = false; // True when the mouse is over and pressed
Button(int xp, int yp, int s, color b, color o, color p) {
x = xp;
y = yp;
size = s;
baseGray = b;
overGray = o;
pressGray = p;
}
// Updates the over field every frame
void update() {
if ((mouseX >= x) && (mouseX <= x + size) &&
(mouseY >= y) && (mouseY <= y + size)) {
over = true;
} else {
over = false;
}
}
boolean press() {
if (over == true) {
pressed = true;
return true;
} else {
return false;
}
}
void release() {
pressed = false; // Set to false when the mouse is released
}
void display() {
if (pressed == true) {
fill(pressGray);
} else if (over == true) {
fill(overGray);
} else {
fill(baseGray);
}
stroke(255);
rect(x, y, size, size);
}
}
Answers
Please, see How to format code and text and edit your message to improve it. Thanks.
Now much easier to read!
Cool. Meanwhile, I was looking at your code. The loop is OK. The value of
v
whenbut.pressed
is true is the value you are looking for: you can stop the loop (break) and use this value.Sorry but can you explain what you mean a little more?
Are you saying write a few statements in the loop saying something along the lines of this pseudo-code:
demonstrate how to use this to start different sub sketches inside the sketch:
Wow. This is great. A little overkill for my purposes but definitely something I could use. I can simply associate each state with an integer value. Then when that state is active, the integer value can be added to a string which then can be sent to the Arduino.
The function part is what always gets me. I'm still getting the hang of calling functions and creating classes.
Know of any good literature I can pick up? I'm reading Nature of Code right now.
Nature of Code is very good
Thanks Chrisir. What you posted was a huge help. My code is looking great now.