Lately I've been away from home(and bored) so I decided to start working on a new Arduino project. I want to make a 2 wheel drive robot that I can eventually turn into a telepresence robot. I want to control said robot through a keyboard on the computer where the Arduino is connected. After some research I've come to the conclusion that the best way to control the robot through a keyboard would be to use a different software (I chose to program in Processing). What I did was, in Processing, I listened to the keyboard and through Serial transfered the bytes to the Arduino, which would then handle the information recieved to either go forward, backwards etc... I thought of using an Arduino Motor Shield on my Uno and two motors on a simple platform. (Unfortunately I haven't had the chance to test out the software seen as I don't have my Arduino on me.) I wondered if you guys could check out the code and see if there's something wrong or something that could be improved.
Arduino code:
int incomingByte = 0;
int dirA = 12;
int pwmA = 3;
int brakeA = 9;
int dirB = 13;
int pwmB = 11;
int brakeB = 8;
void setup(){
Serial.begin(9600);
pinMode (dirA, OUTPUT);
pinMode (brakeA, OUTPUT);
pinMode (dirB, OUTPUT);
pinMode (brakeB, OUTPUT);
}
void loop(){
if (Serial.available() > 0){
incomingByte = Serial.read();
if (incomingByte == 119){ //Tecla W carregada, andar para a frente
digitalWrite(dirA, HIGH);
digitalWrite(dirB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
analogWrite(pwmA, 255);
analogWrite(pwmB, 255);
}
if (incomingByte == 115){ //Tecla S carregada, andar para trás
digitalWrite(dirA, LOW);
digitalWrite(dirB, LOW);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
analogWrite(pwmA, 255);
analogWrite(pwmB, 255);
}
if (incomingByte == 97){ //Tecla A carregada, andar para a direita
digitalWrite(dirA, HIGH);
digitalWrite(dirB, LOW);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
analogWrite(pwmA, 255);
analogWrite(pwmB, 255);
}
if (incomingByte == 100){ //Tecla D carregada, andar para a esquerda
digitalWrite(dirA, LOW);
digitalWrite(dirB, HIGH);
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
analogWrite(pwmA, 255);
analogWrite(pwmB, 255);
}
else{
digitalWrite(dirA, HIGH);
digitalWrite(dirB, HIGH);
digitalWrite(brakeA, HIGH);
digitalWrite(brakeB, HIGH);
analogWrite(pwmA, 0);
analogWrite(pwmB, 0);
}
}
Processing code:
import processing.serial.*;
Serial myPort;
void setup (){
println(Serial.list());
myPort = new Serial(this, Serial.list() [1], 9600);
}
void draw(){
while (keyPressed){
while (key == 'w' || key == 'W'){
myPort.write(119);
}
while (key == 's' || key == 'S'){
myPort.write(115);
}
while (key == 'a' || key == 'A'){
myPort.write(97);
}
while (key == 'd' || key == 'D'){
myPort.write(100);
}
}
}
Thanks for your help.