Obcrov-1


#1
//OBCROV-1 (On-Board Controller for ROV-1)
//Created by Eric Stackpole for OpenROV
//This code allows an Arduino microcontroller to command three motors, a light, and a servo based on serial input sent though it's USB plug from an external source
//This program works with "TSCROV-1 (Top-Side Controller for ROV-1)" which can be found on OpenROV.com
//To see a video of the code working, watch the YouTube Video at http://www.youtube.com/watch?v=8r7Kt1p31vs

//OBCROV-1 by Eric Stackpole is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
//For more informatino, please visit OpenROV.com

#include <Servo.h> //Allow the Arduino to understand code for controlling a servo

// Arduino pins that each variable will control
// Upper Case for Direction, Lower Case for PWM
const int V = 8; //Vertical direction
const int P = 12; //Port (left) direction
const int S = 13; //Starbord (right) direction
const int v = 3; //Vertical PWM (speed)
const int p = 5; //Port (left) PWM (speed)
const int s = 6; //Starbord (right) PWM (speed)
const byte l = 11; //Light PWM (intensity)

// Set inital angle of servo (intended to control tilt angle of a camera)
Servo tilt;
int pos = 80;

void setup(){

// initialize the serial communication:
Serial.begin(9600);

// initialize the pins as an outputs:
pinMode(V, OUTPUT);
pinMode(P, OUTPUT);
pinMode(S, OUTPUT);
pinMode(v, OUTPUT);
pinMode(p, OUTPUT);
pinMode(s, OUTPUT);
pinMode(l, OUTPUT);

// attach the servo on pin 14 (analog 0) to the servo object, "tilt"
tilt.attach(14);
}

byte input;
byte x = 250;
byte i = 128;

void loop(){
if (Serial.available()){
input = Serial.read();

//Turn motors on/off at speed "x" (lower case) and direction (upper case) based on input of F (Forward), A (Aft), P (Port), S (Starbord), V (Vertical)
switch (input){
case 'F':
digitalWrite(P, HIGH);
digitalWrite(S, HIGH);
analogWrite(p, x);
analogWrite(s, x);
break;
case 'A':
digitalWrite(P, LOW);
digitalWrite(S, LOW);
analogWrite(p, x);
analogWrite(s, x);
break;
case 'P':
digitalWrite(P, LOW);
digitalWrite(S, HIGH);
analogWrite(p, x);
analogWrite(s, x);
break;
case 'S':
digitalWrite(P, HIGH);
digitalWrite(S, LOW);
analogWrite(p, x);
analogWrite(s, x);
break;
case 'U':
digitalWrite(V, HIGH);
analogWrite(v, x);
break;
case 'D':
digitalWrite(V, LOW);
analogWrite(v, x);
break;
case 'h':
i++;
analogWrite(l, i);
break;
case 'l':
i--;
analogWrite(l, i);
break;
case '1':
x = 50;
break;
case '2':
x = 150;
break;
case '3':
x = 250;
break;
case 'q':
pos = 30;
break;
case 'a':
pos = 80;
break;
case 'z':
pos = 150;
break;
default:
analogWrite(s, 0);
analogWrite(v, 0);
analogWrite(p, 0);
//pos = 80;
}

// set servo position according to value of pos
tilt.write(pos);
}
}

#2

Consider adding a sensor bank for magnetic compass, accelerometer, and depth/temp.