# Controlling servo motor with keyboard

**URL:** <https://forum.arduino.cc/t/controlling-servo-motor-with-keyboard/517551>\
**Category:** Programming\
**Created:** [March 31, 2018, 4:50pm UTC](https://forum.arduino.cc/t/controlling-servo-motor-with-keyboard/517551 "2018-03-31T16:50:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lexard](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lexard/32/276330_2.png) [@Lexard](https://forum.arduino.cc/u/Lexard)\
**Post date:** [March 31, 2018, 4:50pm UTC](https://forum.arduino.cc/t/controlling-servo-motor-with-keyboard/517551/1 "2018-03-31T16:50:25Z")

</div>

Hello, the title pretty much explains it all. I want to control my robot using my keyboard. When I press for example "e" it moves to the given coordinates.

This is the code that I'm using right now. I'm a complete noob to arduino, might point that out.

```cpp
#include <Servo.h>
Servo servo1;
Servo servo2;
int pos1 = 0;
void setup()
{
  servo1.attach(3);
  servo2.attach(7);
  
}
void loop()
{

    for(pos1 = 50; pos1 < 140; pos1 += 20)
    {
      servo1.write(pos1);
      servo2.write(pos1);
      delay(1000);
    }
    for(pos1 = 140; pos1>=50; pos1-=20)
    {
      servo1.write(pos1);
      servo2.write(pos1);
      delay(1000);
    }
}

```

---

<div class="post-metadata">

**Author:** ![slipstick](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@slipstick](https://forum.arduino.cc/u/slipstick)\
**Post date:** [March 31, 2018, 4:57pm UTC](https://forum.arduino.cc/t/controlling-servo-motor-with-keyboard/517551/2 "2018-03-31T16:57:28Z")

</div>

What keyboard is that? Most Arduinos don't have a keyboard.

And what robot? All I can see from your code is two servos sweeping back and forth. A robot with just two servos doing that isn't going to move very far.

And what location is "e" that it might want to move to?

I think you may need to provide quite a few more details before we can help.

Steve

---

<div class="post-metadata">

**Author:** ![Lexard](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/lexard/32/276330_2.png) [@Lexard](https://forum.arduino.cc/u/Lexard)\
**Post date:** [March 31, 2018, 5:00pm UTC](https://forum.arduino.cc/t/controlling-servo-motor-with-keyboard/517551/3 "2018-03-31T17:00:20Z")

</div>

> slipstick:  
> What keyboard is that? Most Arduinos don't have a keyboard.
> 
> And what robot? All I can see from your code is two servos sweeping back and forth. A robot with just two servos doing that isn't going to move very far.
> 
> And what location is "e" that it might want to move to?
> 
> I think you may need to provide quite a few more details before we can help.
> 
> Steve

1. I'm talking about my laptop keyboard, I want to control it with my laptop keyboard
2. Yes, this is a simple robot arm which picks up objects, but this is not important. Doesn't matter if it sweeps back and forth.
3. It doesn't matter what location I want it to move it to. I can change the coordinates later. I'll just give a random number, 130.

---

<div class="post-metadata">

**Author:** ![slipstick](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@slipstick](https://forum.arduino.cc/u/slipstick)\
**Post date:** [March 31, 2018, 7:28pm UTC](https://forum.arduino.cc/t/controlling-servo-motor-with-keyboard/517551/4 "2018-03-31T19:28:51Z")

</div>

So your robot is going to trail a USB cable connected to your laptop? Fair enough. So open up the serial monitor, type a character and in the Arduino just do a Serial.read() to get the character.

Then do whatever you need to do to get to wherever your character is telling you to go. When you get there stop doing it.

You might want to look up the switch...case command as a way of doing different things when different characters turn up.

Steve

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [March 31, 2018, 8:13pm UTC](https://forum.arduino.cc/t/controlling-servo-motor-with-keyboard/517551/5 "2018-03-31T20:13:38Z")

</div>

> Lexard:
> 
> 1. I'm talking about my laptop keyboard, I want to control it with my laptop keyboard

Then you need a program for your PC that sends a character or message to the Arduino when you press a key on the laptop keyboard. That program could be the Arduino Serial Monitor or a terminal emulator program such as PuTTY or it could be a program that you write yourself.

For receiving the message in your Arduino have a look at the examples in [Serial Input Basics](http://forum.arduino.cc/index.php?topic=396450.0) - simple reliable ways to receive data.

If you want to write your own PC program this [Python - Arduino demo](http://forum.arduino.cc/index.php?topic=225329.msg1810764#msg1810764) may be of interest. However you could do the same thing with any PC programming language.

...R

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:06pm UTC](https://forum.arduino.cc/t/controlling-servo-motor-with-keyboard/517551/6 "2021-05-05T23:06:18Z")

</div>


