Here's the First edition of the code, I'm waiting for my other joystick module to show up, as my 2nd one doesn't seem to function properly, to test everything so far.
If anyone sees a problem or has any suggestions please let me know. I believe I'm going to change the joystick press function to a simple High/Low single pin function for a relay, But best to have 2 working joysticks to test what I have first.
Parts List so far for ROV control:
1 - Arduino Mega (I'm sure a Uno would be just fine, just change the pin numbers to work for your board)
2 - L298N Dual H-Bridge for Arduino
4 - Bilge pumps 1100gph
2 - Dual-axis Joystick Modules for Arduino
1 - LM2596 DC to DC Voltage Regulator 4-40V to 1.5-35V (for powering board)
1 - DC single pole switch (for turning power to Board and H-Bridges on/off so you don't fry your PC when connecting the USB to upload)
lots - Breadboard Jumper Wires Ribbon Cables, Male to Female, Male to Male, Female to Female
1- 12v Battery (I like the ones for alarm systems, small and simple)
1 - pre-wired inline Blade Fuse and fuse ( safety first)
A couple - LED's and 330 resistors (for testing and resistors so you don't burn out the leds)
2 - DC 5V Relay Module for Arduino
4 - RC boat props (2 right 2 Left)
4 - 3.17mm to 4mm Brass Shaft Coupling Joint Connector
4 - 4mm bolts to cut and shape to connect props to bilge pumps
1 - 100' Cat5e or Cat6
2 - RJ45 couplers
1 - short Cat5e or Cat6 cable (to cut in half to solder pins onto for connections)
......Hopefully that's everything......
*********Remember this is a work in progress and I'm still learning********* (All tested and working so far with my 100' Cat5e cable setup)
/*
Arduino code CROC from
https://www.instructables.com/Build-You ... h/#discuss was referenced and used. Big thank you to them for making this avalible
Feel free to reuse/share/improve this code.
Parts:
DC Motor Vertical axis Up/Down (MotorV)
DC Motor Strafe axis Left/Right (MotorS)
DC Motor Horizontal plane Right (MotorHR)
DC Motor Horizontal plane Left (MotorHL)
Light A (Light1)
Light B (Light2)
Inputs:
JoystickA X axis : analogic
JoystickA Y axis : analogic
JoystickA Z axis : analogic (button Press) (usually digital)
JoystickB X axis : analogic
JoystickB Y axis : analogic
JoystickB Z axis : analogic (button press) (usually digital)
Outputs:
Light A : digital
Light B : digital
MotorHR in1 : PWM
MotorHR in2 : PWM
MotorHR enable : PWM (usually digital)
MotorHL in1 : PWM
MotorHL in2 : PWM
MotorHL enable : PWM (usually digital)
MotorV in1 : PWM
MotorV in2 : PWM
MotorV enable : PWM (usually digital)
MotorS in1 : PWM
MotorS in2 : PWM
MotorS enable : PWM
*/
#include <VirtualWire.h>
double HL(int X, int Y)
{
int x=max(X,-511);
x=min(x,511);
int y=max(Y,-511);
y=min(y,511);
double res=0;
if (x+y>0)
res=min(x+y,512);
else
res=max(x+y,-512);
return res/512;
}
double HR(int X, int Y)
{
int x=max(X,-511);
x=min(x,511);
int y=max(Y,-511);
y=min(y,511);
double res=0;
if (y-x>0)
res=min(y-x,512);
else
res=max(y-x,-512);
return res/512;
}
// Inputs = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #
// Joysticks
int pinJoyAX = A14; // analog
int pinJoyAY = A13; // analog
int pinJoyAZ = A15; // analog
int pinJoyBX = A11; // analog
int pinJoyBY = A12; // analog
int pinJoyBZ = A10; // analog
// Motors
// Horizontal Right
int pinMotHR1 = 2; // PWM
int pinMotHR2 = 3; // PWM
int pinMotHREn = 4; // PWM
// Vertical
int pinMotV1 = 5; // PWM
int pinMotV2 = 6; // PWM
int pinMotVEn = 7; // PWM
// Strafe
int pinMotS1 = 8; // PWM
int pinMotS2 = 9; // PWM
int pinMotSEn = 10; // PWM
// Horizontal Left
int pinMotHL1 = 11; // PWM
int pinMotHL2 = 12; // PWM
int pinMotHLEn = 13; // PWM
// Lights
int pinLightA1 = 23; // digital
int pinLightB1 = 22; // digital
// VARIABLES
int valJoyAX = 0;
int valJoyAY = 0;
int valJoyAZ = 0;
int valJoyBX = 0;
int valJoyBY = 0;
int valJoyBZ = 0;
double valHL = 0;
double valHR = 0;
int valJoyAZPrev = 0;
int valJoyBZPrev = 0;
int calJoyAX = 0;
int calJoyAY = 0;
int calJoyBX = 0;
int calJoyBY = 0;
int lightState = 0;
void setup()
{
calJoyAX = analogRead(pinJoyAX);
calJoyAY = analogRead(pinJoyAY);
calJoyBX = analogRead(pinJoyBX);
calJoyBY = analogRead(pinJoyBY);
pinMode(pinJoyAZ, INPUT_PULLUP);
pinMode(pinLightA1, OUTPUT);
pinMode(pinJoyBZ, INPUT_PULLUP);
pinMode(pinLightB1, OUTPUT);
Serial.begin(9600);
}
void loop()
{
// READ INPUTS = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #
valJoyAX = analogRead(pinJoyAX);
valJoyAY = analogRead(pinJoyAY);
valJoyAZPrev = valJoyAZ;
valJoyAZ = digitalRead(pinJoyAZ);
valJoyBX = analogRead(pinJoyBX);
valJoyBY = analogRead(pinJoyBY);
valJoyBZPrev = valJoyBZ;
valJoyBZ = digitalRead(pinJoyBZ);
// DEBUG = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #
/*
Serial.println("-------------");
Serial.print("Joystick A : ");
Serial.print(valJoyAX);
Serial.print(", ");
Serial.print(valJoyAY);
Serial.print(", ");
Serial.println(valJoyAZ);
Serial.print("Joystick B : ");
Serial.print(valJoyBY);
Serial.print(", ");
Serial.print(valJoyBX);
Serial.print(", ");
Serial.println(valJoyBZ);
*/
// LIGHTS = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #
if(valJoyAZ==0 && valJoyAZPrev==1 && lightState==0)
{
digitalWrite(pinLightA1,HIGH);
lightState = 1;
}
else
{
if(valJoyAZ==0 && valJoyAZPrev==1 && lightState== 1)
{
digitalWrite(pinLightA1,LOW);
lightState = 0;
}
}
if(valJoyBZ==0 && valJoyBZPrev==1 && lightState==0)
{
digitalWrite(pinLightB1,HIGH);
lightState = 1;
}
else
{
if(valJoyBZ==0 && valJoyBZPrev==1 && lightState== 1)
{
digitalWrite(pinLightB1,LOW);
lightState = 0;
}
}
// MOTORS = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #
// Manette inversee
//valHL = HL(valJoyAY-calJoyAY, valJoyAX-calJoyAX);
//valHR = HR(valJoyAY-calJoyAY, valJoyAX-calJoyAX);
valHL = HL(calJoyAY-valJoyAY, calJoyAX-valJoyAX);
valHR = HR(calJoyAY-valJoyAY, calJoyAX-valJoyAX);
// Motor HL = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #
if(valHL>0)
{
analogWrite(pinMotHL1,255);
analogWrite(pinMotHL2,0);
analogWrite(pinMotHLEn,255*valHL);
}
else
{
analogWrite(pinMotHL1,0);
analogWrite(pinMotHL2,255);
analogWrite(pinMotHLEn,-255*valHL);
}
// Motor HR = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #
if(valHR>0)
{
analogWrite(pinMotHR1,255);
analogWrite(pinMotHR2,0);
analogWrite(pinMotHREn,255*valHR);
}
else
{
analogWrite(pinMotHR1,0);
analogWrite(pinMotHR2,255);
analogWrite(pinMotHREn,-255*valHR);
}
// Motor Vertical = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #
if(valJoyBY - calJoyBY > 10)
{
analogWrite(pinMotV1,255);
analogWrite(pinMotV2,0);
analogWrite(pinMotVEn,min(valJoyBY-calJoyBY,255));
}
else
{
if(valJoyBY - calJoyBY < -10)
{
analogWrite(pinMotV1,0);
analogWrite(pinMotV2,255);
analogWrite(pinMotVEn,min(calJoyBY-valJoyBY,255));
}
else // switch off the vertical motor
{
analogWrite(pinMotV1,0);
analogWrite(pinMotV2,0);
analogWrite(pinMotVEn,0);
}
}
// Motor Strafe = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = # = #
if(valJoyBX - calJoyBX > 10)
{
analogWrite(pinMotS1,255);
analogWrite(pinMotS2,0);
analogWrite(pinMotSEn,min(valJoyBX-calJoyBX,255));
}
else
{
if(valJoyBX - calJoyBX < -10)
{
analogWrite(pinMotS1,0);
analogWrite(pinMotS2,255);
analogWrite(pinMotSEn,min(calJoyBX-valJoyBX,255));
}
else // switch off the Strafe motor
{
analogWrite(pinMotS1,0);
analogWrite(pinMotS2,0);
analogWrite(pinMotSEn,0);
}
}
delay(200);
}