Example Code for Arduino-Keyboard Control

Last revision 2026/01/13

This article presents a comprehensive guide to using Arduino code for keyboard-controlled movements of the Devastator Tank. It covers essential hardware and software setups, including Arduino IDE configuration and motor direction adjustments, enabling users to effectively manage robot navigation through keyboard commands.

Hardware Preparation

Name Model/SKU Quantity Purchase Link
Devastator Tank Platform ROB0128 1 DFRobot
Arduino UNO A000066 1 Arduino
USB Cable - 1 -
LiPo Battery (7.5V) - 1 -

Software Preparation

  • Development Tool: Arduino IDE (version 1.8.x or later)
  • Download link: Arduino IDE

Other Preparation Work

  1. Keep the USB cable plugged in after uploading.
  2. Ensure the Devastator's power switch is ON and a power supply (e.g., LiPo battery) is connected.
  3. Open the Arduino IDE Serial Monitor.
  4. Set the Serial Monitor to "No line ending" and 9600 baud rate.

Sample Code

/*
 # Editor : Phoebe
 # Date   : 2014.11.6
 # Ver    : 0.1
 # Product: Devastator Tank Mobile Platform
 # SKU    : RBO0112
 # Description:
 # Connect the D4,D5,D6,D7,GND to UNO digital 4,5,6,7,GND

*/

int E1 = 5;     //M1 Speed Control
int E2 = 6;     //M2 Speed Control
int M1 = 4;     //M1 Direction Control
int M2 = 7;     //M1 Direction Control

void stop(void)                    //Stop
{
  digitalWrite(E1,0);
  digitalWrite(M1,LOW);
  digitalWrite(E2,0);
  digitalWrite(M2,LOW);
}
void advance(char a,char b)          //Move forward
{
  analogWrite (E1,a);      //PWM Speed Control
  digitalWrite(M1,HIGH);
  analogWrite (E2,b);
  digitalWrite(M2,HIGH);
}
void back_off (char a,char b)          //Move backward
{
  analogWrite (E1,a);
  digitalWrite(M1,LOW);
  analogWrite (E2,b);
  digitalWrite(M2,LOW);
}
void turn_L (char a,char b)             //Turn Left
{
  analogWrite (E1,a);
  digitalWrite(M1,LOW);
  analogWrite (E2,b);
  digitalWrite(M2,HIGH);
}
void turn_R (char a,char b)             //Turn Right
{
  analogWrite (E1,a);
  digitalWrite(M1,HIGH);
  analogWrite (E2,b);
  digitalWrite(M2,LOW);
}

void setup(void)
{
  int i;
  for(i=4;i<=7;i++)
    pinMode(i, OUTPUT);
  Serial.begin(19200);      //Set Baud Rate
  Serial.println("Run keyboard control");
  digitalWrite(E1,LOW);
  digitalWrite(E2,LOW);

}

void loop(void)
{
  if(Serial.available()){
    char val = Serial.read();
    if(val != -1)
    {
      switch(val)
      {
      case 'w'://Move Forward
        advance (255,255);   //move forward in max speed
        break;
      case 's'://Move Backward
        back_off (255,255);   //move back in max speed
        break;
      case 'a'://Turn Left
        turn_L (100,100);
        break;
      case 'd'://Turn Right
        turn_R (100,100);
        break;
      case 'z':
        Serial.println("Hello");
        break;
      case 'x':
        stop();
        break;
      }
    }
    else stop();
  }

}

Once the code has uploaded, keep the USB cable plugged in. Make sure the Devastator's switch is ON and that a power supply is connected - e.g.: a lipo battery.

Open the Arduno IDE serial monitor. Set the bottom panels to "No line ending" and the baud rate to 9600. This is important as the microcontroller needs to communicate with your computer for this program to work properly.

Result

When using the keyboard to control the Devastator:

  • Press "W": Moves forward.
  • Press "S": Moves backward.
  • Press "A": Turns left.
  • Press "D": Turns right.
  • Press "X": Stops the robot.
  • Press "Z": Prints "Hello" in the Serial Monitor.

Changing the Motor Direction in Code

Let's examine some of the global variables in the program:

int M1 = 4;     //M1 Direction Control
int M2 = 7;     //M2 Direction Control

Digital pins 4 and 7 have been assigned as the motor direction control pins. By setting each either HIGH or LOW (i.e. on or off), we can control which way the motor will turn.

Let's examine another section of code:

//TURN LEFT
void turn_L (char a, char b)
{
  analogWrite (E1, a);
  digitalWrite(M1, LOW);
  analogWrite (E2, b);
  digitalWrite(M2, HIGH);
}

This is a function that tells the Devastator to turn left. M1 and M2 are set as LOW and HIGH respectively. This means that the left-side wheels will turn backwards and the right-side wheels turn forwards.

Conversely, turning right has the motor pins set like so:

//TURN RIGHT
void turn_R (char a, char b)
{
  analogWrite (E1, a);
  digitalWrite(M1, HIGH);
  analogWrite (E2, b);
  digitalWrite(M2, LOW);
}

Therefore, if you find that your Devastator's wheels are going in a direction you don't intend them to, try changing the motor pins signal. If they are going the wrong way and the direction pin is set to HIGH, try changing it to LOW, and vice versa. You can use the keyboard control program to debug and verify these settings.

Was this article helpful?

TOP