Use Processing keypress to control servo speed

Hi everybody. Total newbie here. I'm just getting started with Arduino. I know a little bit of Processing.

I'm working on a quick prototype that uses a keypress, actually two keypresses, from Processing to increase and decrease the servo speed while the key is pressed. I tried searching this forum, but I'm afraid I'm missing some of the lingo to put together an effective search.

I found a link but it's for mouse control over the servo. I can't post the link until I submit my first post. I will post again after a response.

Thanks for your help!

You should be able to just take the mouse control Processing sketch and replace the references to the mouse x-y coords with your keypress code.

Andrew

Remember that you can't directly control servo speed, only position. So you can only slow down a servo by asking it to move a small amount, then pause very briefly, then move again.. and so on.

Thanks very much.

Here is the mouse control code:

http://www.arduino.cc/playground/Learning/SingleServoExample

I tried to run it but it kept giving me an error. Refresh is not a member of Servo, etc. I think that means my Servo library doesn't have the correct files in the Hardware folder. But I followed the instructions pretty carefully. And my knob and Sweep worked fine...Anyone ever have this problem??

Blurg! A classmate just told me its probably because I'm using Arduino 13 and the code is for version 10. I'd rather find different code than switch software. Let's see what I find...

this should work with the servo library in 0013:

#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;
    }
  }
}

the new library does not need or use the refresh method and it only works with pins 9 and 10.

Hi, I would like to know why there is a minus '0' in the switch case. What is that for? I really can't figure that out. It seems to work without it too doesn't it?

......

        switch(ch) {
      case '0'...'9':
        v = v * 10 + ch - '0';
        break;

.....

I would like to know why there is a minus '0' in the switch case

To convert ASCII digits to decimal.

'0' == 0x30, '1' == 0x31 etc.

v = v * 10 + ch - '0'; If you print the value of v after that expression you can see how it accumulates as each digit is received.

The digits are received as characters in the variable ch. The value of ch is the ASCII value. The character '0' has a value of 48, '1' a value of 49 etc. So to convert the ASCII value to its numerical value, 48 is subtracted from each character by subtracting '0' The expression could also be written: v = v * 10 + ch - 48; but using '0' hints that this expression is about converting ASCII values.

Thank you! that was a very good explanation :)