We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › problems with serial to arduino using classes
Page Index Toggle Pages: 1
problems with serial to arduino using classes (Read 1376 times)
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 undefined

correct 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
Re: problems with serial to arduino using classes
Reply #1 - Oct 22nd, 2009, 1:06am
 
You are trying to open the serial port several times. Once in each instance of the class. This is not possible. The solution here is to share the serial port between the classes.

One option:

Class definition:
class Button
{
 Serial myPort;
 Button(Serial port)
 {
    myPort = port;
 }
 // ... rest of class
}

Creating the instances:

Serial port = new Serial(...);
Button button1 = new Button(port);
Button button2 = new Button(port);
Button button3 = new Button(port);
Button button4 = new Button(port);

This should work...
Page Index Toggle Pages: 1