Getting the Arduino board to react to keystrokes.

I wasn't sure where else this topic belonged.

Hello,

For a school project of mine, I'm trying to get the Arduino board (mounted on a robot body) to be able to react to keyboard presses. However, after searching through Google, the arduino website, and this board, I haven't been able to figure out how to accomplish this and I have no idea where to start (if I've missed it, I apologize). I'm trying to control servos based on the arrow keys (or WASD if that's easier). Is this possible?

For an idea of what I'm trying to do:

int ledPinNorth = 1;
int ledPinSouth = 2;
int ledPinWest = 3;
int ledPinEast = 4;
int ledSelect = 1;
void setup()
{
  pinMode(ledPinNorth, OUTPUT);
  pinMode(ledPinSouth, OUTPUT);
  pinMode(ledPinWest, OUTPUT);
  pinMode(ledPinEast, OUTPUT);
}

void loop()
{
  if (ledSelect = 1) //This would make the robot travel north.
  {
    digitalWrite(ledPinNorth, HIGH);
    digitalWrite(ledPinSouth, LOW);
    digitalWrite(ledPinWest, LOW);
    digitalWrite(ledPinEast, LOW);
  }
  else if (ledSelect = 2) //south
  {
    digitalWrite(ledPinNorth, LOW);
    digitalWrite(ledPinSouth, HIGH);
    digitalWrite(ledPinWest, LOW);
    digitalWrite(ledPinEast, LOW);
  }
  else if (ledSelect = 3) //west
  {
    digitalWrite(ledPinNorth, LOW);
    digitalWrite(ledPinSouth, LOW);
    digitalWrite(ledPinWest, HIGH);
    digitalWrite(ledPinEast, LOW);
  }
  else if (ledSelect = 4) //and east
  {
    digitalWrite(ledPinNorth, LOW);
    digitalWrite(ledPinSouth, LOW);
    digitalWrite(ledPinWest, LOW);
    digitalWrite(ledPinEast, HIGH);
  }
  else 
  {
    delay(10);
  }
}

To give an example controlling LEDs instead of servos. I want to be able to control the LEDs with the keyboard instead of the ledSelect variable.

Thanks in advance.

edit: "digitialWrite", ha. Fixed for spelling.

Would it work for you to use the keyboard to send bytes for W, A, S, and D to the Arduino over serial? Or are you hoping to just have a keyboard and your Arduino with no computer?

  • Ben

Would it work for you to use the keyboard to send bytes for W, A, S, and D to the Arduino over serial?

Yes. The robot is hooked from the Arduino board to the computer via a USB cable. I want to use the keyboard connected to this computer (the one I'm compiling and coding on) to move the servos/light the LEDs. Is this possible?

Thank you for the response.

I'm not sure how sending serial data to the Arduino works using the Arduino IDE as I've never tried it, but it's incredibly straightforward to use a terminal program to send serial data over a COM port (or virtual COM port) to a mega168 (assuming you have a USB to serial adapter, which most Arduinos do, or a RS-232-to-TTL level converter). Typing 'a' in a terminal program like Hyperterm will transmit the byte 'a' to your Arduino, which you can then receive using the serial receive method (I'm not sure of the syntax here as I haven't used it) and respond to accordingly.

  • Ben