Example Code for Arduino-Keyboard Control
Upload a program that gives keyboard control over the Cherokey. By observing the robot's motion, you can debug the motor directions so that each motor is spinning the correct way. The program provides feedback via the serial monitor for predefined directions.
Software Preparation
Use Arduino IDE to upload the sketch. Keep the USB cable plugged in after uploading. Open the Arduino IDE serial monitor and set the bottom panels to "No line ending" and the baud rate to 9600.
Other Preparation Work
- Keep the USB cable plugged in after uploading the code.
- Make sure the Cherokey's switch is ON and that a power supply (e.g., a LiPo battery) is connected.
- Open the Arduino IDE serial monitor. Set the bottom panels to "No line ending" and the baud rate to 9600. This is important for communication between the microcontroller and your computer.
Sample Code
/*
# Edited by: Matt
# Date: 2015.09.06
# Version: 1.1
# Product: Cherokey 4WD Mobile Platform
# SKU: ROB0102/ROB0117
# Description:
# Drive 2 motors with this Cherokey 4WD Mobile Platform
# Connect D4,D5,D6,D7,GND to UNO digital 4,5,6,7,GND
*/
//Motor Definitions
int E1 = 5; //M1 Speed Control
int E2 = 6; //M2 Speed Control
int M1 = 4; //M1 Direction Control
int M2 = 7; //M2 Direction Control
//DIRECTIONS
//STOP
void stop(void)
{
digitalWrite(E1, 0);
digitalWrite(M1, LOW);
digitalWrite(E2, 0);
digitalWrite(M2, LOW);
}
//ADVANCE
void advance(char a, char b)
{
analogWrite (E1, a);
digitalWrite(M1, HIGH);
analogWrite (E2, b);
digitalWrite(M2, HIGH);
}
//MOVE BACKWARDS
void back_off (char a, char b)
{
analogWrite (E1, a);
digitalWrite(M1, LOW);
analogWrite (E2, b);
digitalWrite(M2, LOW);
}
//TURN LEFT
void turn_L (char a, char b)
{
analogWrite (E1, a);
digitalWrite(M1, LOW);
analogWrite (E2, b);
digitalWrite(M2, HIGH);
}
//TURN RIGHT
void turn_R (char a, char b)
{
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(9600); //Set Baud Rate
Serial.println("hello. w = forward, d = turn right, a = turn left, s = backward, x = stop, z = hello world"); //Display instructions in the serial monitor
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
Serial.println("going forward");
advance (255, 255); //move forward at max speed
delay (1000);
stop();
break;
case 's'://Move Backward
Serial.println("going backward");
back_off (255, 255); //move backwards at max speed
delay (1000);
stop();
break;
case 'a'://Turn Left
Serial.println("turning left");
turn_L (255, 255);
delay (1000);
stop();
break;
case 'd'://Turn Right
Serial.println("turning right");
turn_R (255, 255);
delay (1000);
stop();
break;
case 'z':
Serial.println("hello world!");
break;
case 'x':
Serial.println("stopping");
stop();
break;
}
}
else stop();
}
}
Result
- When you press "W", the Cherokey should move forward for 1 second, and then stop.
- When you press "D", the Cherokey should turn to the right 90 degrees and then stop.
- When you press "A", the Cherokey should turn to the left 90 degrees and then stop.
- When you press "S", the Cherokey should move backwards for 1 second, and then stop.
- When you press "Z", the Arduino IDE serial monitor should print: "hello world!".
Additional Information
If motors are spinning incorrectly, there are two methods to change this:
- Changing the polarity of the motors by changing the positive and negative wiring. (which works, but isn't an elegant solution)
- Changing lines in the code, covered in the section below
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 Cherokey 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 Cherokey'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?
