Ok, i have been messing with this for a few weeks.. being a total noob to most things electronics related.. and with a minimal knowledge of object oriented programing, this is what i came up with.
This will eventually tie into a larger scale robot art project that i am considering building. The idea is to use the Arduino + Xport to control the robot via the internet.
I am pretty cheap, aka broke, so i have used things that i had access to without spending money.. mainly the xport direct. (Read Making things talk, used it with some of those projects). No, the xport direct is not wireless.. but, i also have a hacked wrt54g linksys router as well.
I have bridged the router to my wireless network.. gave it a larger battery to supply 12v, for a few minutes, and plugged in my xport to it.
I have come up with this code.. that seems to work as a proof of concept if nothing else.
My question to you good people is : How can i make this code smaller, more compact, and maybe make it neater?
I am currently using PuTTY to telnet into my xport and issue keystrokes, am working on a processing sketch to act as a client that will have a neater GUI for control.. doing away with PuTTY.
Here is my code.. and yes, it does work.. not sure why.. but it does..
/*
Lantronix xport remote
This sketch is written to be loaded on an arduino that is connected to a mobile
robot platform. The arduino gains internet connectivity via an xport direct wired to a
linksys wrt54g wireless router. (It has internet connectivity to any wired ethernet device i plug into it
by being bridged into my pre-exsisting wireless network.)
To control motion, you will use the standard "gaming" keys, "w, s, a, d, and x"
w=forward
s=reverse
a=left
d=right
x= stop all routines
Xport Connections:
Lantronix TX --> arduino digital pin 2
Lantronix RX --> Arduino digital pin 3
Lantronix Reset --> Arduino digital pin 4
Hbridge Connections:
arduino 5 --> hbridge channel 1 enable - you can use pwm on this pin to control speed
arduino 6 --> hbridge channel 2 enable - you can use pwm on this pin to control speed
arduino 7 --> channel 1 forward
arduino 8 --> channel 1 reverse
arduino 9 --> channel 2 forward
arduino 10 --> channel 2 reverse
By Joey Barrett
Created 12/11/08
You may use this code however you want.
i have configured my xport thusly:
*** Channel 1
Baudrate 9600, I/F Mode 4C, Flow 00
Port 10001
Connect Mode : C0
Send '+++' in Modem Mode enabled
Show IP addr after 'RING' enabled
Auto increment source port disabled
Remote IP Adr: --- none ---, Port 00000
Disconn Mode : 00
Flush Mode : 00
to control the module and motors, use PuTTY or another terminal or telnet client,
and telnet to your xport's ip address. Right now, there is no confirmation of login, but there are messages to tell you what actions are being performed... telnet to the ip address and hit the 'w' key to see what i mean.
Simply start pressing your corresponding keys to control the motors.
*/
#include <AFSoftSerial.h>
// include the adafruit softserial library
AFSoftSerial mySerial = AFSoftSerial(3, 2); // provides tx on pin 2 and rx on pin 3
#define deviceResetPin 4 // for our reset of the xport
#define mtr1Enable 5 // motor 1 enable pin
#define mtr2Enable 6 // motor 2 enable pin
#define mtr1Forward 7 // motor 1 forward pin
#define mtr1Reverse 8 // motor 1 reverse pin
#define mtr2Forward 9 // motor 2 forward pin
#define mtr2Reverse 10 // motor 2 reverse pin
char incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600);
delay(200);
Serial.println("Serial Coms Ready!");
mySerial.begin(9600);
pinMode(deviceResetPin, OUTPUT);
resetDevice();
Serial.println("Xport Coms Ready!");
Serial.println("Waiting for connections!");
pinMode(mtr1Enable, OUTPUT);
pinMode(mtr2Enable,OUTPUT);
pinMode(mtr1Forward,OUTPUT);
pinMode(mtr1Reverse,OUTPUT);
pinMode(mtr2Forward,OUTPUT);
pinMode(mtr2Reverse,OUTPUT);
}
void loop() {
if (mySerial.available() > 0) {// read the incoming byte:
incomingByte = mySerial.read();
if (incomingByte == 'w'){
FORWARD(); // say what you got:
Serial.print("I received: ");
Serial.println(incomingByte);
}
else if(incomingByte =='s'){
REVERSE();// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte);
}
else if (incomingByte == 'a'){
LEFT(); // say what you got:
Serial.print("I received: ");
Serial.println(incomingByte);
}
else if (incomingByte == 'd'){
RIGHT(); // say what you got:
Serial.print("I received: ");
Serial.println(incomingByte);
}
else if (incomingByte == 'x'){
stop();
}
}
}
/*
Take the Lantronix device's reset pin low to reset it
*/
void resetDevice() {
digitalWrite(deviceResetPin, LOW);
delay(50);
digitalWrite(deviceResetPin, HIGH);
// pause to let Lantronix device boot up:
delay(2000);
}
void FORWARD(){
Serial.println ("Moving Forward (Maybe)!");
mySerial.println ("Moving Forward (Maybe)!");
digitalWrite(mtr1Enable, HIGH);
digitalWrite(mtr2Enable, HIGH);
digitalWrite(mtr1Forward, HIGH);
digitalWrite(mtr1Reverse,LOW);
digitalWrite(mtr2Forward, HIGH);
digitalWrite(mtr2Reverse, LOW);
}
void REVERSE(){
Serial.println ("Reversing Directions! (maybe)");
mySerial.println ("Reversing Directions! (maybe)");
digitalWrite(mtr1Enable, HIGH);
digitalWrite(mtr2Enable, HIGH);
digitalWrite(mtr1Forward, LOW);
digitalWrite(mtr1Reverse,HIGH);
digitalWrite(mtr2Forward, LOW);
digitalWrite(mtr2Reverse, HIGH);
}
void RIGHT(){
Serial.println ("Turning Right! (maybe)");
mySerial.println ("Turning Right! (maybe)");
digitalWrite(mtr1Enable, HIGH);
digitalWrite(mtr2Enable, HIGH);
digitalWrite(mtr1Forward, HIGH);
digitalWrite(mtr1Reverse, LOW);
digitalWrite(mtr2Forward, LOW);
digitalWrite(mtr2Reverse, HIGH);
}
void LEFT(){
Serial.println ("Turning Left! (maybe)");
mySerial.println ("Turning left! (maybe)");
digitalWrite(mtr1Enable, HIGH);
digitalWrite(mtr2Enable, HIGH);
digitalWrite(mtr1Forward, LOW);
digitalWrite(mtr1Reverse,HIGH);
digitalWrite(mtr2Forward, HIGH);
digitalWrite(mtr2Reverse, LOW);
}
void stop(){
Serial.println ("Stopping Movement! (maybe)");
mySerial.println ("Stopping Movement! (maybe)");
digitalWrite(mtr1Enable, LOW);
digitalWrite(mtr2Enable, LOW);
digitalWrite(mtr1Forward, LOW);
digitalWrite(mtr1Reverse,LOW);
digitalWrite(mtr2Forward, LOW);
digitalWrite(mtr2Reverse, LOW);
}
Can you improve the code while leaving the functionality that i need?
The main thing is to be able to send basic commands, forward, reverse, left, right, and to be able to turn off and on a few other things that have not been implemented at this time.
Any advice or help would be greatly appreciated.