Onboard Arduino with serial connection

Control Boards, Controllers, Tethers, Ect.
Post Reply
mrtristanplaysguitar

Onboard Arduino with serial connection

Post by mrtristanplaysguitar »

Hello
-As I was looking through my options for control, I thought of using USB extenders over Cat5 and putting the Arduino onboard my ROV. However, I have no idea of how to communicate with the ROV via serial connection. For example, how would I say "if this button is pressed, make this light turn on." (that should be kinda simple. hopefully).

Anyway, I'm hoping to get to the point with my programming skills where I could read another USB device, send data to Arduino and make it happen.

Any pointers to online tutorials, books, and other resources would be greatly appreciated. Constructive criticism is always good too ;)

See ya

- Tristan
eska-dh
Posts: 6
Joined: Jun 12th, 2011, 11:50 am

Re: Onboard Arduino with serial connection

Post by eska-dh »

Here’s my code, it still in development, but it works. I’m planning on using a PS2 controller to transmit controls hooked up to an arduino, and use that arduino to send a serial data stream to the aruduino on the ROV over a Cat5 cable. Three strands of the cat 5 cable are needed to have the two arduinos talk to each other, one pair that hooks up rx/tx of one to the tx/rx of the other and then the third to connect the grounds to both. So far the code has worked over a 100’ of cat 5e cable.

At a minimum what you need to use is the Serial.print code at the bottom of the tranmit code and use almost the entire receive code . This should be good start. Good luck.

***Transmit Code***
#include "PS2X_lib.h" //for v1.6

PS2X ps2x; // create PS2 Controller Class
/*

Thank you Bill @ http://www.billporter.info
PS2 Controller Wiring Scheme
Green (Not Used)
Blue (Pin 13)
Yellow (Pin 10)
Pink [or Red] (+5 v)
Black (& Grey for some) (Ground)
Purple (7v - 9v power source for Rumble)
Orange (Pin 11)
Brown (Pin 12 w/ 10k pullup to +5v)
*/

//right now, the library does NOT support hot pluggable controllers, meaning
//you must always either restart your Arduino after you conect the controller,
//or call config_gamepad(pins) again after connecting the controller.

int LT;
int RT;
int LF;
int RF;
int TF;
int TR;
int LTCalc;
int RTCalc;
int LTF;
int RTF;
int LTR;
int RTR;
int STL;
int STR;
int SRF;
int DIV;
int LEDSET = 0;
int ARM;
int ARMPOS;
int CLAW;
int CLAWPOS;
int error;
int CAMERA;
int i;
byte type;
byte vibrate;
byte vibsmall;
char s;
char n;
char e;
int hls = 0; // Sets switch for halogen lights to off
int upValue = 0; //PWM value forward
int dwnValue = 0; //PWM value reverse
int reading = 128; // zero analog sticks
int LRread = 128; // zero left analog stick
int LED = 255;
int briteAmt = 0;
int dimAmt = 0;

void setup(){
Serial.begin(57600);
//setup pins and settings: GamePad(clock, command, attention, data, Pressures?, Rumble?)
error = ps2x.config_gamepad(13,11,10,12, true, true);
type = ps2x.readType();
switch(type) {
case 0:
//Serial.println("Unknown Controller type");
break;
case 1:
// Serial.println("DualShock Controller Found");
break;
case 2:
//Serial.println("GuitarHero Controller Found");
break;
}
}
void loop(){
/* You must Read Gamepad to get new values
Read GamePad and set vibration values
ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
if you don't enable the rumble, use ps2x.read_gamepad(); with no values
you should call this at least once a second
*/
if(error == 1) //skip loop if no controller found
return;
else { //DualShock Controller
/*If the shutter is only bad when you are not touching the sticks, I’d add code like
reading = ps2x.Analog(PSS_RX);
if(reading > 125 && reading < 131) reading = 128;
*/
ps2x.read_gamepad(vibsmall, vibrate); //read controller and set
//large motor to spin at 'vibrate' speed
if(ps2x.Button(PSB_BLUE)){
TF = ps2x.Analog(PSAB_BLUE);
RTF = TF;
LTF = TF;
RTR = 0;
LTR = 0;
}
else{
RTF = 0;
LTF = 0;
TF = 0;
}
if(ps2x.Button(PSB_CIRCLE)){
TR = ps2x.Analog(PSAB_CIRCLE);
RTF = 0;
LTF = 0;
RTR = TR;
LTR = TR;
}
else{
RTR = 0;
LTR = 0;
TR = 0;
}
if(ps2x.Analog(PSS_LX) <= 100){
LT = map(ps2x.Analog(PSS_LX), 100, 0, 0, 255);
if(TF == 0 && TR == 0){
LTR = LT;
RTF = LT;
}
else if(TF >= 1){
LTF = TF - LT;
if (LTF < 0){
LTR = map(LTF, -1, -255, 1, 255);
LTF = 0;
}
RTF = TF + LT;
if (RTF > 255){
RTF = 255;
}
}
else if (TR >= 1){
LTR = TR - LT;
if (LTR < 0){
LTF = map(LTR, -1, -255, 1, 255);
LTR = 0;
}
RTR = TR + LT;
if (RTR > 255){
RTR = 255;
}
}
}
if(ps2x.Analog(PSS_LX) >= 155){
RT = map(ps2x.Analog(PSS_LX), 155, 255, 0, 255);
if(TF == 0 && TR == 0){
LTF = RT;
RTR = RT;
}
else if(TF >= 1){
LTF = TF + RT;
if (LTF > 255){
LTF = 255;
}
RTF = TF - RT;
if (RTF < 0){
RTR = map(RTF, -1, -255, 1, 255);
RTF = 0;
}
}
else if (TR >= 1){
LTR = TR + RT;
if (LTR > 255){
LTR = 255;
}
RTR = TR - RT;
if (RTR < 0){
RTF = map(RTR, -1, -255, 1, 255);
RTR = 0;
}
}
}
if(ps2x.Button(PSB_PAD_UP)){
briteAmt = map(ps2x.Analog(PSAB_PAD_UP), 0, 255, 0, 25);
LED = LED + briteAmt;
if(LED >= 255){
LED = 255;
}
}
if(ps2x.Button(PSB_PAD_DOWN)){
dimAmt = map(ps2x.Analog(PSAB_PAD_DOWN), 0, 255, 0, 25);
LED = LED - dimAmt;
if(LED <= 0){
LED = 0;
}
}
if(ps2x.ButtonPressed(PSB_PAD_LEFT) || ps2x.ButtonPressed(PSB_PAD_RIGHT)){
if(LEDSET == 0)
{
LEDSET = 1;
}
else if(LEDSET == 1){
LEDSET = 2;
}
else if(LEDSET == 2){
LEDSET = 0;
}
}
if(ps2x.Button(PSB_R1)){
STR = ps2x.Analog(PSAB_R1);
STL = 0;
}
else if(ps2x.Button(PSB_L1)){
STL = ps2x.Analog(PSAB_L1);
STR = 0;
}
else{
STL = 0;
STR = 0;
}
if(ps2x.Button(PSB_R2)){
SRF = ps2x.Analog(PSAB_R2);
DIV = 0;
}
else if(ps2x.Button(PSB_L2)){
DIV = ps2x.Analog(PSAB_L2);
SRF = 0;
}
else{
SRF = 0;
DIV = 0;
}
reading = ps2x.Analog(PSS_RY);
if(reading <= 100){
ARMPOS = map(reading, 100, 0, 0, 25);
ARM = ARM - ARMPOS;
if(ARM < 0){
ARM = 0;
}
}
if(reading >= 155) {
ARMPOS = map(reading, 155, 255, 0, 15);
ARM = ARM + ARMPOS;
if(ARM > 255){
ARM = 255;
}
}
int reading2 = ps2x.Analog(PSS_RX);
if(reading2 <= 100){
CLAWPOS = map(reading2, 100, 0, 0, 15);
CLAW = CLAW - CLAWPOS;
if(CLAW < 0){
CLAW = 0;
}
}
if(reading2 >= 155) {
CLAWPOS = map(reading2, 155, 255, 0, 25);
CLAW = CLAW + CLAWPOS;
if(CLAW > 255){
CLAW = 255;
}
}
if(ps2x.ButtonPressed(PSB_GREEN)){
if(hls == 0)
{
hls = 1;
}
else if(hls == 1){
hls = 0;
}
}
if(ps2x.ButtonPressed(PSB_SQUARE)){
if(CAMERA == 0)
{
CAMERA = 1;
}
else if(CAMERA == 1){
CAMERA = 0;
}
}
//Transmission Stream to parse at ROV
Serial.print("s"); //Start stream indicator
Serial.print(","); //std delimiter
Serial.print(LTF, DEC); //ROV Forward 1 (x button - left l analog)
Serial.print(","); //std delimiter
Serial.print(LTR, DEC); //ROV Reverse 1 (circle button + left l analog)
Serial.print(","); //std delimiter
Serial.print(RTF, DEC); //ROV Forward 2 (x button - right l analog)
Serial.print(","); //std delimiter
Serial.print(RTR, DEC); //ROV Reverse 2 (circle button + right l analog)
Serial.print(","); //std delimiter
Serial.print(STR, DEC); //ROV Drift Rt (R1 pressed)
Serial.print(","); //std delimiter
Serial.print(STL, DEC); //ROV Drift Lt (L1 pressed)
Serial.print(","); //std delimiter
Serial.print(SRF, DEC); //ROV Surface (R2 pressed)
Serial.print(","); //std delimiter
Serial.print(DIV, DEC); //ROV Dive (L2 pressed)
Serial.print(","); //std delimiter
Serial.print(hls); //ROV Spot Light On/off
Serial.print(","); //std delimiter (R3 pressed)
Serial.print(LED, DEC); //ROV LED Fade (up/down arrow)
Serial.print(","); //std delimiter
Serial.print(ARM, DEC); //ROV Arm Up/Down (up/down r analog)
Serial.print(","); //std delimiter
Serial.print(CLAW, DEC); //ROV Arm Open/Close (rt/lt r analog)
Serial.print(","); //std delimiter
Serial.print(CAMERA, DEC); //Camera select. IR / Color
Serial.print(","); //std delimiter
Serial.print(LEDSET, DEC);
Serial.print(",");
Serial.println("e"); //End Stream indicator

delay(100);
}
}



******Receive Code*****

/*
parse
source:
http://www.instructables.com/answers/
How-can-i-parse-serial-strings-with-comma-separate/

Thnax
*/
#define MAXVALUES 14
char parse(byte strInput[], int output[MAXVALUES])
{
int strPos; // this variable is used to 'wlk' through the string one char at a time
int outputptr = 0;
// check that the string's first character is 's'
if (strInput[0] != 's')
{
return -1; // Bad format
}
else
{
if (strInput[1] != ',')
return -2;
output[outputptr] = 0;
for (strPos=2; strInput[strPos]!= 'e'; strPos++)
{
if ((strInput[strPos] < '0') || (strInput[strPos] > '14'))
{
if (strInput[strPos] == ',')
{
// This value is complete so begin next.
outputptr = outputptr + 1;
output[outputptr] = 0;
}
else
{
//Serial.println(strInput[strPos]);
return -2; // Invalid Character
}
}
else
{
output[outputptr] = output[outputptr]*10 + (strInput[strPos]-'0');
}
}
if (outputptr<MAXVALUES-1)
{
return -3; // Too Few Parameters
}
else
{
return 0; // Success!
}
}
}

int LTF = 0;
int RTF = 0;
int LTR = 0;
int RTR = 0;
int STL = 0;
int STR = 0;
int SRF = 0;
int DIV = 0;
int LED = 0;
int LEDSET = 0;
int ARM;
int CLAW;
int hls = 0;
int CAMERA;

// The setup() method runs once, when the sketch starts

void setup()
{
Serial.begin(57600);
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop()
{
int outArray[MAXVALUES];
int result;
byte myCmd[128];
int inputSize=0;
byte strParse[128]; //

if (Serial.available() > 0) {
delay(50);
inputSize = Serial.available();
//Serial.print("inputSize=");
//Serial.println(inputSize);

for (int i = 0; i < inputSize; i++){
myCmd = Serial.read();
//Serial.println(myCmd);
}
}


result = parse(myCmd, outArray);

if (result==0)
{

LTF = outArray[0];
LTR = outArray[1];
RTF = outArray[2];
RTR = outArray[3];
STR = outArray[4];
STL = outArray[5];
SRF = outArray[6];
DIV = outArray[7];
hls = outArray[8];
LED = outArray[9];
ARM = outArray[10];
CLAW = outArray[11];
CAMERA = outArray[12];
LEDSET = outArray[13];

Serial.print("LTF = ");
Serial.println(LTF, DEC); //ROV Forward 1 (x button - left l analog)
Serial.print("LTR = "); //std delimiter
Serial.println(LTR, DEC); //ROV Reverse 1 (circle button + left l analog)
Serial.print("RTF = "); //std delimiter
Serial.println(RTF, DEC); //ROV Forward 2 (x button - right l analog)
Serial.print("RTR = "); //std delimiter
Serial.println(RTR, DEC); //ROV Reverse 2 (circle button + right l analog)
Serial.print("STR = "); //std delimiter
Serial.println(STR, DEC); //ROV Drift Rt (R1 pressed)
Serial.print("STL = "); //std delimiter
Serial.println(STL, DEC); //ROV Drift Lt (L1 pressed)
Serial.print("SRF = "); //std delimiter
Serial.println(SRF, DEC); //ROV Surface (R2 pressed)
Serial.print("DIV = "); //std delimiter
Serial.println(DIV, DEC); //ROV Dive (L2 pressed)
Serial.print("lights = "); //std delimiter
Serial.println(hls); //ROV Spot Light On/off
Serial.print("LED brightness = "); //std delimiter (R3 pressed)
Serial.println(LED, DEC); //ROV LED Fade (up/down arrow)
Serial.print("ARM = "); //std delimiter
Serial.println(ARM, DEC); //ROV Arm Up/Down (up/down r analog)
Serial.print("CLAW = "); //std delimiter
Serial.println(CLAW, DEC); //ROV Arm Open/Close (rt/lt r analog)
Serial.print("CAMERA = "); //std delimiter
Serial.println(CAMERA, DEC); //Camera select. IR / Color
Serial.print("LED Set "); //std delimiter
Serial.println(LEDSET, DEC);

}
else
{
//Serial.println("Error Parsing String: ");
//Serial.println(result, DEC);
}

delay(50); // wait a bit

}






[/color]
User avatar
SoakedinVancouver
Posts: 117
Joined: Dec 31st, 2010, 9:38 pm

Re: Onboard Arduino with serial connection

Post by SoakedinVancouver »

Eska-dh, thanks for the whole page of "how-to" on Arduino programming! Much appreciated,
Post Reply