Pages: [1]   Go Down
Author Topic: Is it possible to Controll a CR servo using Keyboard Commands?  (Read 857 times)
0 Members and 1 Guest are viewing this topic.
St. Lucia, West Indies
Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to control a Continuous Rotation Servo using the Arduino 2560 and keyboard commands sent to the board using the USB interface. Is this possible. Any help is much appreciated. THANK YOU! smiley
Logged

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

Zoomkat will be along shortly
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.

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The below is for testing servos using the serial monitor.

Code:
// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h>
Servo myservo;  // create servo object to control a servo

void setup() {
  Serial.begin(9600);
  myservo.writeMicroseconds(1500); //set initial servo position if desired
  myservo.attach(7);  //the pin for the servo control
  Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}

void loop() {
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }

  if (readString.length() >0) {
    Serial.println(readString);  //so you can see the captured string
    int n = readString.toInt();  //convert readString into a number

    // auto select appropriate value, copied from someone elses code.
    if(n >= 500)
    {
      Serial.print("writing Microseconds: ");
      Serial.println(n);
      myservo.writeMicroseconds(n);
    }
    else
    {   
      Serial.print("writing Angle: ");
      Serial.println(n);
      myservo.write(n);
    }

    readString=""; //empty for next input
  }
}

Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

St. Lucia, West Indies
Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the code! Ideally what I want to accomplish is using letters on the keyboard to control a CR servo.

So that this could happen;

Keyboard letter A when pressed would allow servo to spin in one direction

Keyboard letter B when pressed would allow servo to spin in opposite direction.
Logged

St. Lucia, West Indies
Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello

What I am trying to do is control a CR servo using keyboard commands.

So this can be performed;

When A on the keyboard is pressed the servo would spin in one direction.

When B on the keyboard is pressed the servo would spin in the opposite direction.

Any Idea as to how this can be accomplished?

Much help is appreciated.

Thank You! smiley
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How is the keyboard connected? If it's connected to a computer and then to the arduino, you'll have to write a computer program to send the commands over serial. If you're using something like the PS2 library, it's a lot easier.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So this can be performed;
Does your Arduino have a keyboard attached? Mine doesn't.

The keyboard is attached to some other device. That device needs to tell the Arduino that a key was pressed.

That device being a PC, it is dumb as a box of rocks. You need to write an application, and run it, that detects key presses and sends them, via the serial port, to the Arduino.

The Arduino needs to read serial data, and do something with it.

I'd guess that there are something in the neighborhood of about 14 bazillion examples around.
Logged

St. Lucia, West Indies
Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And how do i go about writing an application to tell the Arduino a key is being pressed?

Sorry for being a noob. smiley-sad

And yes the Keyboard is on the Computer.
« Last Edit: November 13, 2011, 01:12:48 pm by ravrichard0 » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And how do i go about writing an application to tell the Arduino a key is being pressed?
There are dozens of ways. If you said something about what programming language(s) you are familiar with the "how to" could be a lot more specific.

None is an acceptable answer. Not a good one, but OK.
Logged

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

Yes, you can do that.
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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Ideally what I want to accomplish is
posting in every conceivable forum?
Logged

St. Lucia, West Indies
Offline Offline
Newbie
*
Karma: 0
Posts: 43
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I know Basic quite well. And im in the process of learning Processing.

Quote
posting in every conceivable forum?

Im just trying to see what other thoughts persons have. Sorry if I did anything wrong. smiley-sad
Logged

Pages: [1]   Go Up
Jump to: