Controlling servo motor with keyboard

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.

#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);
    }
}

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

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.

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

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 - simple reliable ways to receive data.

If you want to write your own PC program this Python - Arduino demo may be of interest. However you could do the same thing with any PC programming language.

...R