Autonomous Mode
One of the engineering goals was to make the robot autonomous, so I set out to achieve that goal soon after I created a working robot. At first, I wanted to emulate button pushes on the controller by hooking up an arduino and using electrical signals to make it seem like certain buttons were being pushed, but I soon realized that I would need to somehow output 12 volts from an arduino, which can only handle 5 volts. After thinking about it more, I remembered that the purpose of the ethernet cable was to just house the internal wires, which were connected to the motors. An ethernet cable has 8 internal wires, and the control board just uses buttons to close a circuit, which powers the motors. Using a ethernet breakout board, I was able to break the ethernet wires into it's individual pins, which I soldered the arduino wires to. To solve the 12 volt power issue, I used an L293D H-Bridge Motor driver IC, which allowed me to easily power all the motors from an arduino. Once I figured out which motor was connected to which pin, I easily managed to program movement.
Chain of Command
Since I am using multiple arduinos, I needed to create a chain of command for them to interact properly and avoid problems. The first arduino, known as the "master" will instruct the robot to move a certain way, such as "right". While the robot is supposed to turn right, the master will continuously broadcast that message. The "controller" arduino will read that message and determine how to move the motors to achieve that result. To move right for example, you would need to move the right motor backwards, and the left motor forwards. The arduino will tell the L293D H-Bridge IC to move the motors in their appropriate direction, which will power each motor appropriately.
The full circuit while it is under load. Note that the arduino is also connected to a battery through the wires extending outwards.
Code
#include<AFMotor.h>
AF_DCMotor Dive(1);
AF_DCMotor Left(2);
AF_DCMotor Right(3);
const int diveup = 14;
const int divedown = 15;
const int leftforward = 16;
const int leftback = 17;
const int rightforward = 18;
const int rightback = 19;
int diveupmode = 0;
int divedownmode = 0;
int leftforwardmode =0;
int leftbackmode = 0;
int rightforwardmode = 0;
int rightbackmode = 0;
void setup() {
// put your setup code here, to run once:
Dive.setSpeed(255);
Left.setSpeed(255);
Right.setSpeed(255);
pinMode(diveup,INPUT);
pinMode(divedown,INPUT);
pinMode(leftforward,INPUT);
pinMode(leftback,INPUT);
pinMode(rightforward,INPUT);
pinMode(rightback,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
diveupmode = digitalRead(diveup);
divedownmode = digitalRead(divedown);
leftforwardmode = digitalRead(leftforward);
leftbackmode = digitalRead(leftback);
rightforwardmode = digitalRead(rightforward);
rightbackmode = digitalRead(rightback);
if(diveupmode == HIGH){
Dive.run(FORWARD);
}
else if(divedownmode == HIGH){
Dive.run(BACKWARD);
}
else if(diveupmode == LOW && divedownmode == LOW){
Dive.run(RELEASE);
}
if(leftforwardmode == HIGH){
Left.run(FORWARD);
}
else if(leftbackmode == HIGH){
Left.run(BACKWARD);
}
else if(leftbackmode == LOW && leftforwardmode == LOW){
Left.run(RELEASE);
}
if(rightforwardmode == HIGH){
Right.run(FORWARD);
}
else if(rightbackmode == HIGH){
Right.run(BACKWARD);
}
else if(rightbackmode == LOW && rightforwardmode == LOW){
Right.run(RELEASE);
}
/*
Dive.run(FORWARD);
Left.run(FORWARD);
Right.run(FORWARD);
delay(1000);
Dive.run(RELEASE);
Left.run(RELEASE);
Right.run(RELEASE);
delay(100);
Dive.run(BACKWARD);
Left.run(BACKWARD);
Right.run(BACKWARD);
delay(1000);
Left.run(BACKWARD);
Right.run(FORWARD);
delay(2000);
*/
}
/*
void back(String motor, int time, int speed = 255){
motor.setSpeed(speed);
motor.run(BACKWARD);
delay(time);
}
void stopmotor(String motor, int time){
motor.run(RELEASE);
}
void stopall(int time){
Dive.run(RELEASE);
Left.run(RELEASE);
Right.run(RELEASE);
}
void forward(motor, time, speed = 255){
motor.setSpeed(speed);
motor.run(FORWARD);
delay(time);
}
*/
----------------------------------------------------------------------------
const int diveup = 2;
const int divedown = 3;
const int leftforward = 4;
const int leftback = 5;
const int rightforward = 6;
const int rightback = 7;
void setup() {
// put your setup code here, to run once:
pinMode(diveup,OUTPUT);
pinMode(divedown,OUTPUT);
pinMode(leftforward,OUTPUT);
pinMode(leftback,OUTPUT);
pinMode(rightforward,OUTPUT);
pinMode(rightback,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(diveup, HIGH);
digitalWrite(leftforward, HIGH);
digitalWrite(rightback, HIGH);
delay(2000);
digitalWrite(rightback, LOW);
delay(1000);
digitalWrite(leftforward, LOW);
delay(1000);
digitalWrite(diveup, LOW);
}
/*
void goup(int time){
digitalWrite(diveup, HIGH);
delay(time);
digitalWrite(diveup, LOW);
}
void godown(int time){
digitalWrite(divedown, HIGH);
delay(time);
digitalWrite(divedown, LOW);
}
*/