I'm using this code to move my Rover 5
//Standard PWM DC control
int E1 = 5; //M1 Speed Control
int E2 = 6; //M2 Speed Control
int M1 = 4; //M1 Direction Control
int M2 = 7; //M1 Direction Control
///For previous Romeo, please use these pins.
//int E1 = 6; //M1 Speed Control
//int E2 = 9; //M2 Speed Control
//int M1 = 7; //M1 Direction Control
//int M2 = 8; //M1 Direction Control
void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,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=6;i<=9;i++)
pinMode(i, OUTPUT);
Serial.begin(9600); //Set Baud Rate
}
void loop(void)
{
char val = Serial.read();
if(val!=-1)
{
switch(val)
{
case 'w'://Move Forward
advance (100,100); //PWM Speed Control
break;
case 's'://Move Backward
back_off (100,100);
break;
case 'a'://Turn Left
turn_L (100,100);
break;
case 'd'://Turn Right
turn_R (100,100);
break;
}
delay(40);
}
else stop();
}
I also did a code using Perl language, so i execute my Perl code and press 'w','a',s', and 'd,' and I move my Rover 5 is like video game.
#!/usr/bin/perl
use strict;
use warnings;
use Device::SerialPort;
use Term::ReadKey;
ReadMode 4; # Turn off controls keys
$| = 1;
my $port = Device::SerialPort->new("/dev/ttyUSB0");
# 19200, 81N on the USB ftdi driver
$port->baudrate(9200);
$port->databits(8);
$port->parity("none");
$port->stopbits(0);
sub get_key {
my $key;
while ( not defined( $key = ReadKey(-1) ) ) {
}
return $key;
}
while (1) {
my $char = $port->lookfor();
my $key = get_key();
if ($key) {
exit(0) if $key !~ /w|a|s|d|t/i;
$port->write($key);
}
if ($char) {
print "Recieved character: $char \n";
}
# Uncomment the following lines, for slower reading,
# but lower CPU usage, and to avoid
# buffer overflow due to sleep function.
# $port->lookclear;
# sleep (1);
sleep(0.1);
}
To use it,
perl this_file.pl
and press the keyboard w,a,s and d.