Serial control with keyboard

Hi, I am trying to control a servo using directional keys and have been told that this is possible to do through the serial port… Could someone please point me in the right direction as to how to do this please.

thanks

You need to create a program on the Pc that reads the keyboard key presses, converts them to whatever value you want to send to Arduino to control the servo.

If you know a pc programming language check if it supports the serial port. If not, a good option is to use the programming language called Prossesing, its open source, free, and somewhat related to Arduino.

hi, thanks I will check that out ! Do you know any good tutorials that would be of use to me ?

thanks

If you can program Arduino (thats C or C++) then i would go for the programming language Processing, its Java based and not too different from C.

Processing is free, can be downloaded from here:

http://processing.org/learning/

They also have some tutorials online, maybee not exactly what you need but good starters.

Hi, I have started to use processing a bit, but when searching around the internet I found this code

/* 
 * NewSerialServo 
 * -------------- 
 * Servo control from the Serial port 
 * 
 * Alteration of the control interface to use < and > keys 
 * to slew the servo horn left and right.  Works best with 
 * the Linux/Mac terminal "screen" program. 
 * 
 * Created 10 December 2007 
 * copyleft 2007 Brian D. Wendt 
 * http://principialabs.com/ 
 * 
 * Adapted from code by Tom Igoe 
 * http://itp.nyu.edu/physcomp/Labs/Servo 
 */  
  
/** Adjust these values for your servo and setup, if necessary **/  
int servoPin     =  2;    // control pin for servo motor  
int minPulse     =  600;  // minimum servo position  
int maxPulse     =  2400; // maximum servo position  
int turnRate     =  100;  // servo turn rate increment (larger value, faster rate)  
int refreshTime  =  20;   // time (ms) between pulses (50Hz)  
  
/** The Arduino will calculate these values for you **/  
int centerServo;         // center servo position  
int pulseWidth;          // servo pulse width  
int moveServo;           // raw user input  
long lastPulse   = 0;    // recorded time (ms) of the last pulse  
  
  
void setup() {  
  pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin  
  centerServo = maxPulse - ((maxPulse - minPulse)/2);  
  pulseWidth = centerServo;   // Give the servo a starting point (or it floats)  
  Serial.begin(9600);  
  Serial.println("      Arduino Serial Servo Control");  
  Serial.println("Press < or > to move, spacebar to center");  
  Serial.println();  
}  
  
void loop() {  
  // wait for serial input  
  if (Serial.available() > 0) {  
    // read the incoming byte:  
    moveServo = Serial.read();  
  
    // ASCII '<' is 44, ASCII '>' is 46 (comma and period, really)  
    if (moveServo == 44) { pulseWidth = pulseWidth - turnRate; }  
    if (moveServo == 46) { pulseWidth = pulseWidth + turnRate; }  
    if (moveServo == 32) { pulseWidth = centerServo; }  
  
    // stop servo pulse at min and max  
    if (pulseWidth > maxPulse) { pulseWidth = maxPulse; }  
    if (pulseWidth < minPulse) { pulseWidth = minPulse; }  
  
    // print pulseWidth back to the Serial Monitor (uncomment to debug)  
    // Serial.print("Pulse Width: ");  
    // Serial.print(pulseWidth);  
    // Serial.println("us");   // microseconds  
  }  
  
  // pulse the servo every 20 ms (refreshTime) with current pulseWidth  
  // this will hold the servo's position if unchanged, or move it if changed  
  if (millis() - lastPulse >= refreshTime) {  
    digitalWrite(servoPin, HIGH);   // start the pulse  
    delayMicroseconds(pulseWidth);  // pulse width  
    digitalWrite(servoPin, LOW);    // stop the pulse  
    lastPulse = millis();           // save the time of the last pulse  
  }  
}

which I uploaded but it doesn’t seem to work with my servo. can any one tell if the should work or not ??

thanks.

Did you connect your servo to pin 2 ?

How are you sending the keystrokes to Arduino ?

Which keys are you pressing ?

Have you had a look at the servo library?
It means yu don’t have to generate the servo pulses yourself.

hi, answering to mikmo yes I did connect it to servo pin 2 and I am pressing the left and right directional keys, and to groove, that is what I thought as well and am going to try that now I have enough time.

thanks for your help :slight_smile:

ok, now I have attempted to convert the code to the servo library but it still doesn’t work, here is the

#include <Servo.h>

int moveServo;

Servo myservo;

void setup()
{
  
  myservo.attach(10);
  Serial.begin(9600);
  Serial.println("      Arduino Serial Servo Control");
  Serial.println("Press < or > to move, spacebar to center");
  Serial.println();
  
}

void loop () {
  
  if (Serial.available() > 0) {
    moveServo = Serial.read();
    
    if (moveServo == 44) { myservo.write(71); }
    if (moveServo == 46) { myservo.write(10); }
    if (moveServo == 32) { myservo.write(46); }
  }
}

trouble is though, it still doesn’t work and I don’t understand the code enough to be able to convert it :frowning:

can someone help me please ? :stuck_out_tongue:

thanks

What is that “still doesn’t work”?
Define “it works”.
Does the servo not move at all?

Try a simpler sketch that just moves the servo from one extreme to another, and build on that.

#include <Servo.h>
Servo myservo;

void setup()
{
  myservo.attach(10);
}

void loop () {
    myservo.write(10);
    delay (500);
    myservo.write(100);
    delay (500);
}

Wait - what are you using to send the characters?
The serial monitor will only send characters when you hit “return”.

For legibility, can I suggest:

 if (moveServo == ',') { myservo.write(71); }
    if (moveServo == '.') { myservo.write(10); }
    if (moveServo == ' ') { myservo.write(46); }

hi, thanks so much for the Help ! I assumed that the < and > meant left and right arrow keys, but as you said they don’t, they mean , and . Also your advise about pressing return also really helped because I never twigged to click send data even though it’s obvious really !

thanks again