Go Down

Topic: Is it possible to Controll a CR servo using Keyboard Commands? (Read 1 time) previous topic - next topic

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! :)

AWOL

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

zoomkat

The below is for testing servos using the serial monitor.

Code: [Select]

// 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
  }
}

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

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.

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! :)

WizenedEE

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.

PaulS

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.

#7
Nov 13, 2011, 07:08 pm Last Edit: Nov 13, 2011, 07:12 pm by ravrichard0 Reason: 1
And how do i go about writing an application to tell the Arduino a key is being pressed?

Sorry for being a noob. :(

And yes the Keyboard is on the Computer.

PaulS

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.

AWOL

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

PaulS

Quote
Ideally what I want to accomplish is

posting in every conceivable forum?

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

Go Up