Interfacing more than 2 servos using processing ?

I am in a project of robotic arm in my and I want it to control via usb mouse using processing but it seems that i can control two servos. But I need to control three more. I have driven two servos successfully using this code

import processing.serial.*;
Serial port;



void setup()
{
  size(180, 180);
  println("Available serial ports:");
  println(Serial.list());
  port = new Serial(this, Serial.list()[1], 9600);
}

void draw()
{
   port.write(mouseX); //this drives the servo in any angle between 0 to 180
   port.write(mouseY); //this drives the servo in any angle between 0 to 180
}

Is there anyway where i can draw 5 rectangle and control them individually to drive the servos ????

Is there anyway where i can draw 5 rectangle

Yes. In the draw() function.

and control them individually to drive the servos ????

How do you intend to "control them individually"? How does the size, location, or orientation of a rectangle control the position of a servo? What will the inputs to the size, location, or orientation of the rectangles be?

You've only got one mouse.

Here in my code if the move the mouse along x axis the base servo changes and along Y axis the elbow changes but i need to control other servos too. So i thought that i could draw 5 rect and use them to control each servos. That is each rect can move only between 0 to 180 px to control the angle of servo

So, you want the mouse position inside a rectangle, and the number of the rectangle, to be what is used to determine which servo(s) to move and where to move them too.

Should be easy enough. The coordinates of the mouse relative to those of a rectangle can be used to determine if the mouse is inside the rectangle, or not. If it is, the distance from one corner is easy to calculate, and the rectangle number is known.

Yes but i can't find any easy way to do this :(. I was successful in using one rect() and moved it using the mouse cursor to control the servo. If you could help me then it would be great :slight_smile:

I was successful in using one rect() and moved it using the mouse cursor to control the servo.

The rects that you create should NOT move with the mouse. The rect() function takes 4 arguments - one corner, a length, and a width. They are actually shown only to provide a visual reference.

If you have a rect() that starts at 0,0, has a width of 100 and a height of 80, and the mouse is at 200, 60, it is clear that the mouse pointer is not in the rectangle, right?

If you have two rects, one that starts at (0, 0) while the other starts at (100, 0), each having a width of 100 and a height of 80, and the mouse is at 180, 60, you can determine that the mouse is not in the first rectangle, and that it is in the second rectangle, right?

You can determine that the mouse is 80 units to the right of and 60 units up (or down) from the origin, right? So, given a mouse position of 180, 60, you can determine that the values that you want to send are 2, 80, and 60, to move the second pair of servos to 80, 60.

It should be clear, then, that rectangles of 180 by 180 are the ideal size, and that you need one per pair of servos.

Can you see how to compute which rectangle the mouse is in? Can you see how to compute where in the rectangle the mouse is?