geewood
YaBB Newbies
Offline
Posts: 1
problems with serial to arduino using classes
Oct 12th , 2009, 1:55am
hi all, sorry, i'm a bit new to processing, so the answer might be a bit obvious, but i've been stuck for a few days now. i'm trying to set up a series of boxes that when touched with a mouse send indiviual signals to arduino. the code works for a single box, but when i try to set up 4 using classes the error comes up saying 'the constructor Serial(controls_a.Button, String, int) is undefinedcorrect single code: import processing.serial.*; float buttonx; float buttony; int buttonSize = 20; boolean mouseOverButton = false; Serial port; void setup() { size(200, 200); buttonx = width/2.0; buttony = height/2.0; rectMode(RADIUS); println(Serial.list()); port = new Serial(this, Serial.list()[1], 9600); } void draw() { background(0); // Test if the cursor is over the box if (mouseX > buttonx-buttonSize && mouseX < buttonx+buttonSize && mouseY > buttony-buttonSize && mouseY < buttony+buttonSize) { mouseOverButton = true; // draw a line around the box and change its color: stroke(255); fill(153); port.write(1); } else { stroke(153); fill(153); port.write('L'); mouseOverButton = false; } rect(buttonx, buttony, buttonSize, buttonSize); }code using classes: class Button { float buttonx; float buttony; float signal; int buttonSize = 20; //radius of rectangle boolean mouseOverButton = false; Button (float posBx, float posBy, float sign) { buttonx = posBx; buttony = posBy; signal = sign; import processing.serial.*; Serial myPort; println(Serial.list()); myPort = new Serial(this, Serial.list()[1], 9600); // Open the port that the Arduino board is connected to (in this case #0) // Make sure to open the port at the same speed Arduino is using (9600bps) } void draw() { rectMode(RADIUS); // Test if the cursor is over the box if (mouseX > buttonx-buttonSize && mouseX < buttonx+buttonSize && mouseY > buttony-buttonSize && mouseY < buttony+buttonSize) { mouseOverButton = true; stroke(255); fill(153); myPort.write(signal); } else { mouseOverButton = false; myort.write('S'); stroke(153); fill(153); } rect(buttonx, buttony, buttonSize, buttonSize); } } Button [] Buttons = new Button [4]; void setup() { size (400,200); for (int i = 0; i < Buttons.length; i++) { Buttons[i] = new Button(50+(i*100), 100, i); } } void draw() { background(230); for (int i = 0; i < Buttons.length; i++) { Buttons[i].draw(); }} thanks for your help! G