Go Down

Topic: Use Processing keypress to control servo speed (Read 10719 times) previous topic - next topic

cabraroja

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!

andrew0

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

anachrocomputer

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.

cabraroja

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??

cabraroja

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...

mem

#5
Mar 10, 2009, 08:43 am Last Edit: Mar 10, 2009, 08:45 am by mem Reason: 1
this should work with the servo library in 0013:

Code: [Select]
#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.

Gonras

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?

Code: [Select]

......

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

.....

AWOL

Quote
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.

mem

[font=Courier New] v = v * 10 + ch - '0';[/font]
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:
[font=Courier New] v = v * 10 + ch - 48;[/font]
but using  '0' hints that this expression is about converting ASCII values.

Gonras

Thank you!
that was a very good explanation :)

Go Up