Pages: [1]   Go Down
Author Topic: 2 servos controlled with mouse in processing  (Read 1580 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi there
i am using this code which has 2 servos on digital pins 9, 10. i found this code from another post in the arduino forums
 code is
Code:
#include <Servo.h>

Servo servo1; Servo servo2;

void setup() {
  servo1.attach(9);  
  servo2.attach(10);

  Serial.begin(19200);
  Serial.println("Ready");
}

void loop() {

  static int v = 0;

  if ( Serial.available()) {
    char ch = Serial.read();

    switch(ch) {
      case '0'...'9':
        v = v * 10 + ch - '0';
        break;
      case 's':
        servo1.write(v);
        v = 0;
        break;
      case 'w':
        servo2.write(v);
        v = 0;
        break;
      case 'd':
        servo2.detach();
        break;
      case 'a':
        servo2.attach(10);
        break;
    }
  }
}

and my processing code which i found on http://www.arduino.cc/playground/Learning/SingleServoExample
is
Code:
/**
 * Servocontrol (derived from processing Mouse 1D example.)
 *
 * Updated 24 November 2007
 */


// Use the included processing code serial library
import processing.serial.*;        


int gx = 15;
int gy = 35;
int spos=90;

float leftColor = 0.0;
float rightColor = 0.0;
Serial port;                         // The serial port



void setup()
{
  size(720, 720);
  colorMode(RGB, 1.0);
  noStroke();
  rectMode(CENTER);
  frameRate(100);

  println(Serial.list()); // List COM-ports

  //select second com-port from the list
  port = new Serial(this, Serial.list()[1], 19200);
}

void draw()
{
  background(0.0);
  update(mouseX);
  fill(mouseX/4);
  rect(150, 320, gx*2, gx*2);
  fill(180 - (mouseX/4));
  rect(450, 320, gy*2, gy*2);
}

void update(int x)
{
  //Calculate servo postion from mouseX
  spos= x/4;

  //Output the servo position ( from 0 to 180)
  port.write("s"+spos);



  // Just some graphics
  leftColor = -0.002 * x/2 + 0.06;
  rightColor =  0.002 * x/2 + 0.06;

  gx = x/2;
  gy = 100-x/2;

}

now the problem is that only the servo on pin 9 works. the servo on pin 10 does not move at all.
i have also checked my connections by attaching the 2nd servo on pin 10 to pin 9 to see if the connections are wrong and it worked.

could you please help me get both servos working??

many thanks in advance!

M.
Logged

0
Offline Offline
God Member
*****
Karma: 39
Posts: 986
Get Bitlash: http://bitlash.net
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You might give Bitlash a try to debug the servos from the command line.  There's a blog post here with examples: http://www.entropymouse.com/blog/blog:bitlash_1.1_and_servo_mayhem

Bitlash is here: http://bitlash.net

Once you have the servos happy you can either modify your Processing sketch to send Bitlash commands, or go back to the code you have in hand and continue debugging.

Happy hacking,

-br
http://bitlash.net
http://entropymouse.com
Logged

NYC
Offline Offline
Full Member
***
Karma: 0
Posts: 125
The singularity is near!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Switching the motors was smart. You proved the motor is wired right. Of course there could be some issue with pin 10 or using two adjacent pins.

The next clever thing to do is run the sketch from the Arduino serial monitor. Also have the sketch echo back what it is doing:
case 'w':
         Serial.write("servo2.write("); Serial.write(v);Serial.writeln(");
        servo2.write(v);
        v = 0;
        break;

Think of testing with the serial monitor as unit testing the Arduino sketch. You want to make sure that works as expected before introducing Processing. Check that the servo is attached to pin 10 and the value in Serial.write(v).

After this, I would try a different port or a different servo library. Define the ports at the top of your sketch so you can  change them easily. But first make sure your Arduino and the library supports pwm on the pin. Early versions of Servo only supported pins 9 and 10.
Logged

Europe, Estonia
Offline Offline
Full Member
***
Karma: 0
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

m56789, if you get the code working, please post it here.
I have also been trying to do that kind of project, so far no luck.

I am trying to do something like this:
[media]http://www.youtube.com/watch?v=pyMsH3jwMUw[/media]
[media]http://www.youtube.com/watch?v=FymWVFZpYj0&NR=1[/media]

« Last Edit: June 12, 2010, 07:55:18 am by Fredx » Logged

Europe, Estonia
Offline Offline
Full Member
***
Karma: 0
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is something: http://www.arduino.cc/playground/ComponentLib/Ps2mouse. On that page it is written - "There are NO external components needed: you can wire the mouse directly to the arduino. "

I have an old optical mouse which i'd like to use... BUT i have heard, that Arduino can handle current max 40 mA. Behind my optical mouse there is written 5V - 100mA. So is it safe to connect it directly to Arduino?



Does anyone know if mice with ball consume less current (mA)?

« Last Edit: June 12, 2010, 07:55:37 am by Fredx » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24474
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
BUT i have heard, that Arduino can handle current max 40 mA. Behind my optical mouse there is written 5V - 100mA

But you're not likely to power the mouse from an I/O pin (that's the 40mA limit), so what's the problem?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Europe, Estonia
Offline Offline
Full Member
***
Karma: 0
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So is it ok when i power it from 5V pin?


Optical mouse pins:
   1. - Data
   2. - Not connected
   3. - Ground
   4. - Vcc (+5V) (corrent 100mA)
   5. - Clock
   6. - Not connected
« Last Edit: June 12, 2010, 03:58:44 pm by Fredx » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 241
Posts: 24474
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think the Duemilanove's regulator is rated at 800mA, so as long as you're not running your servos off it too, then the mouse load should be OK.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Europe, Estonia
Offline Offline
Full Member
***
Karma: 0
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, i am planning to connect:


Device5VGNDDigital
Servo1yesyes9
Servo2yesyes10
Laseryesyes
LaserButtonyes11
Mouseyesyes5/6

Is it bad that everything is connected to the same 5V pin?
At the moment only 2 servos and mouse are connected..
« Last Edit: June 12, 2010, 05:46:45 pm by Fredx » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16565
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is it bad that everything is connected to the same 5V pin?
At the moment only 2 servos and mouse are connected..

The Arudino board can only supply 1/2 amp max when on USB power and maybe 700-800ma when using the boards external power connector. You load is probably too much for the board as many people find they can't even power two servos (it depends of the specific servos used and the amount of mechancial load on them) without having the board reset intermittenly due to power droop. You would be better off using an external regualted 5vdc supply rated at 2-3 amps to power all those loads.

Lefty
« Last Edit: June 12, 2010, 06:21:02 pm by retrolefty » Logged

Europe, Estonia
Offline Offline
Full Member
***
Karma: 0
Posts: 208
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it true, that when i add too many devices to Arduino it may stop working / overheat?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16565
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is it true, that when i add too many devices to Arduino it may stop working / overheat?

Yes it is. However not likely to cause permanent damage as the USB power is protected with a 500ma thermofuse and and when powered through the external power connector the internal 5volt regulator with go into shutdown mode automatically if too much current is drawn or if the package gets too hot.

Lefty
Logged

Pages: [1]   Go Up
Jump to: