I am building an ROV that will be using either three or four motors. I have created a code for three motors, and was hoping somebody could tell me whether I am heading in the right direction or if I need to research different techniques, etc.
Heres the code:
/*
* This is a three motor control program with forward, reverse, and speed control.
* It will be my longest code as of today.
* Hopefully, using Sam's Arduino, I will be able to test it.
* However, we have to acquire parts first
* Created by Tristan Smith
* 5/22/11
*/
//These are the up/down variables
int uppotPin = A0; //Motor speed potentiometer for vertical thruster
int upbuttonPin = 1; //Button for up motion
int downbuttonPin = 2; //Button for down motion
int upmotorPin = 13; //Pin that the MOSFET for "up" is connected to
int downmotorPin = 12; //Pin that the MOSFET for "down" is connected to
int uppotValue = analogRead(uppotPin); //The vertical potentiometer value
int upreading = digitalRead(upbuttonPin); //Whether the up button is pressed or not
int downreading = digitalRead(downbuttonPin); //Whether the down button is pressed or not
int upspeedvariable = uppotValue/4.0117;
/*
* What value to be the speed of motor (PWM signal output to MOSFETs or transistors).
* Divided by 4.0117 b/c the scale of 0-1023 for analog conversion to 0-255 for digital.
*/
//These are the forward/reverse variables on the port side
int horizontalpotPin = A1; //The motor speed potentiometer for horizontal thrusters
int portfwdbuttonPin = 3; //The button for forward on the port thruster
int portrvsbuttonPin = 4; //The button for reverse on the port thruster
int portfwdmotorPin = 11; //The pin that the MOSFET for forward port thruster is connected to
int portrvsmotorPin = 10; //The pin that the MOMSFET for reverse port thruster is connected to
int portpotValue = analogRead(horizontalpotPin); //The port potentiometer value
int portfwdreading = digitalRead(portfwdbuttonPin);
int portrvsreading = digitalRead(portrvsbuttonPin);
int portspeedvariable = portpotValue/4.0117;
//These are the forward/reverse variables on the starboard side
//They're the same as the port variables but have different names according to being starboard. So in other words, I'm not going to explain each one.
int starboardfwdbuttonPin = 3;
int starboardrvsbuttonPin = 4;
int starboardfwdmotorPin = 11;
int starboardrvsmotorPin = 10;
int starboardpotValue = analogRead(horizontalpotPin);
int starboardfwdreading = digitalRead(starboardfwdbuttonPin);
int starboardrvsreading = digitalRead(starboardrvsbuttonPin);
int starboardspeedvariable = starboardpotValue/4.0117;
void setup()
{
//The up and down control
pinMode(uppotPin, INPUT);
pinMode(upbuttonPin, INPUT);
pinMode(downbuttonPin, INPUT);
pinMode(upmotorPin, OUTPUT);
pinMode(downmotorPin, OUTPUT);
//Now the port forward and reverse control
pinMode(horizontalpotPin, INPUT);
pinMode(portfwdbuttonPin, INPUT);
pinMode(portrvsbuttonPin, INPUT);
pinMode(portfwdmotorPin, OUTPUT);
pinMode(portfwdmotorPin, OUTPUT);
//Now the starboard forward and reverse control
pinMode(horizontalpotPin, INPUT);
pinMode(starboardfwdbuttonPin, INPUT);
pinMode(starboardrvsbuttonPin, INPUT);
pinMode(starboardfwdmotorPin, OUTPUT);
pinMode(starboardfwdmotorPin, OUTPUT);
}
void loop()
{
//Up and down control
if(upreading == HIGH)
{
analogRead(uppotPin);
analogWrite(upmotorPin, upspeedvariable);
delay(30);
}
else
{
digitalWrite(upmotorPin, LOW);
}
if(downreading == HIGH)
{
analogRead(uppotPin);
analogWrite(downmotorPin, upspeedvariable);
delay(30);
}
else
{
digitalWrite(downmotorPin, LOW);
}
//Now for the port forward and reverse control
if(portfwdreading == HIGH)
{
analogRead(horizontalpotPin);
analogWrite(portfwdmotorPin, portspeedvariable);
delay(30);
}
else
{
digitalWrite(portfwdmotorPin, LOW);
}
if(portrvsreading == HIGH)
{
analogRead(horizontalpotPin);
analogWrite(portrvsmotorPin, portspeedvariable);
delay(30);
}
else
{
digitalWrite(portrvsmotorPin, LOW);
}
//Now for the starboard forward and reverse control
if(starboardfwdreading == HIGH)
{
analogRead(horizontalpotPin);
analogWrite(starboardfwdmotorPin, starboardspeedvariable);
delay(30);
}
else
{
digitalWrite(starboardfwdmotorPin, LOW);
}
if(starboardrvsreading == HIGH)
{
analogRead(horizontalpotPin);
analogWrite(starboardrvsmotorPin, starboardspeedvariable);
delay(30);
}
else
{
digitalWrite(starboardrvsmotorPin, LOW);
}
}
int starboardpotValue = analogRead(horizontalpotPin);
int starboardfwdreading = digitalRead(starboardfwdbuttonPin);
int starboardrvsreading = digitalRead(starboardrvsbuttonPin);
These are NOT creating associations between the variable names and reading the sensor input. The variables will be initialized ONCE and will keep the same value until you change them.
If you want shorthand names for reading sensor input you can create functions:
int starboardpotValue() {return analogRead(horizontalpotPin);}
int starboardfwdreading() {return digitalRead(starboardfwdbuttonPin);}
int starboardrvsreading() {return digitalRead(starboardrvsbuttonPin);}
int starboardpotValue = analogRead(horizontalpotPin);
int starboardfwdreading = digitalRead(starboardfwdbuttonPin);
int starboardrvsreading = digitalRead(starboardrvsbuttonPin);
These are NOT creating associations between the variable names and reading the sensor input. The variables will be initialized ONCE and will keep the same value until you change them.
If you want shorthand names for reading sensor input you can create functions:
int starboardpotValue() {return analogRead(horizontalpotPin);}
int starboardfwdreading() {return digitalRead(starboardfwdbuttonPin);}
int starboardrvsreading() {return digitalRead(starboardrvsbuttonPin);}
So if I put functions in like the one you wrote that would give me the shorthand names? And those would go before the setup() and loop(), correct?
tristanplaysguitar:
So if I put functions in like the one you wrote that would give me the shorthand names? And those would go before the setup() and loop(), correct?
Correct. You can call the function anywhere in your program. The function will calculate and return a value.
Your
if(downreading == HIGH)
becomes
if(downreading() == HIGH)
Your upspeedvariable will have to be a function, too, if you want to use the current value of uppotValue(). Otherwise it is like any other initialized variable. You set it when it was created and it will retain that value until you assign it a new value.
Okay. Thanks for catching that - thats the kind of beginners mistake I was hoping I wouldn't make
So to set the shorthand names (functions), I can put them anywhere in my program?
It did not compile, so I made this instead. Look okay to you?
code:
/*
* This is a program for Arduino control of motors.
* Uses four transistors (wired into an H-Bridge), two momentary switches or buttons per motor.
* With this setup the user can control which direction the motor is rotating and control speed.
* Created for protoyping ROV Arduino Control Systems
* Created by Tristan Smith
* May 24th, 2011
*/
//Now the Vertical Motor Control variables
int verticalfwdmotorPin = 13;
int verticalrvsmotorPin = 12;
int verticalpotPin = A0;
int verticalfwdbuttonPin = 1;
int verticalrvsbuttonPin = 2;
//Now the Port Motor Control variables
int portfwdmotorPin = 13;
int portrvsmotorPin = 12;
int horizontalpotPin = A0;
int portfwdbuttonPin = 1;
int portrvsbuttonPin = 2;
//Now the Starboard Motor Control Variables
int starboardfwdmotorPin = 13;
int starboardrvsmotorPin = 12;
int starboardfwdbuttonPin = 1;
int starboardrvsbuttonPin = 2;
void setup()
{
//Now the Vertical pin modes
pinMode(verticalpotPin, INPUT);
pinMode(verticalfwdbuttonPin, INPUT);
pinMode(verticalrvsbuttonPin, INPUT);
pinMode(verticalfwdmotorPin, OUTPUT);
pinMode(verticalrvsmotorPin, OUTPUT);
//Now the Port pin modes
pinMode(horizontalpotPin, INPUT);
pinMode(portfwdbuttonPin, INPUT);
pinMode(portrvsbuttonPin, INPUT);
pinMode(portfwdmotorPin, OUTPUT);
pinMode(portrvsmotorPin, OUTPUT);
//Now the Starboard pin modes
pinMode(horizontalpotPin, INPUT);
pinMode(starboardfwdbuttonPin, INPUT);
pinMode(starboardrvsbuttonPin, INPUT);
pinMode(starboardfwdmotorPin, OUTPUT);
pinMode(starboardrvsmotorPin, OUTPUT);
}
void loop()
{
if(digitalRead(verticalfwdbuttonPin) == HIGH)
{
analogWrite(verticalfwdmotorPin, analogRead(verticalpotPin)/4.0117);
}
else
{
digitalWrite(verticalfwdmotorPin, LOW);
}
if(digitalRead(verticalrvsbuttonPin) == HIGH)
{
analogWrite(verticalrvsmotorPin, analogRead(verticalpotPin)/4.0117);
}
else
{
digitalWrite(verticalrvsmotorPin, LOW);
}
if(digitalRead(portfwdbuttonPin) == HIGH)
{
analogWrite(portfwdmotorPin, analogRead(horizontalpotPin)/4.0117);
}
else
{
digitalWrite(portfwdmotorPin, LOW);
}
if(digitalRead(portrvsbuttonPin) == HIGH)
{
analogWrite(portrvsmotorPin, analogRead(horizontalpotPin)/4.0117);
}
else
{
digitalWrite(portrvsmotorPin, LOW);
}
if(digitalRead(starboardfwdbuttonPin) == HIGH)
{
analogWrite(starboardfwdmotorPin, analogRead(horizontalpotPin)/4.0117);
}
else
{
digitalWrite(starboardfwdmotorPin, LOW);
}
if(digitalRead(starboardrvsbuttonPin) == HIGH)
{
analogWrite(starboardrvsmotorPin, analogRead(horizontalpotPin)/4.0117);
}
else
{
digitalWrite(starboardrvsmotorPin, LOW);
}
}
Starter: please, when posting code, put it in a proper code box.
Go back to you last post, click on "modify", highlight all the code, then click on the "#" (code) icon on the editor's toolbar.
repeats, differing only in the name of the output pin and analogue input pin?
This could all be factored up into a single function, making potential bugs easier to spot.
Also, "analogWrite (x, 0);" performs the same function as "digitalWrite (x, LOW);", potentially further reducing your code size and debug burden.
Also:-
analogRead(verticalpotPin)/4.0117)
might not return the right data type and just in effect divide by 4
int(float(analogRead(verticalpotPin))/4.0117))
might be better, it does no harm if not.
So, i don't have the Arduino I'll be using for this project, so to test I'll be using my friend's. But, I have no idea which pins to use. So really the pin numbers are not important in this (ie, i'll change them when its time to run the program).
int starboardfwdmotorPin = 13; //Transistors controlling forward motor direction
int starboardrvsmotorPin = 12; //Transistors controlling reverse motor direction
int starboardfwdbuttonPin = 1; //Push button (8-Way Arcade joystick) controlling forward transistor
int starboardrvsbuttonPin = 2; //Push button controlling reverse transistor
int horizontalpotPin = A0; //Slide potentiometer to control speed of horizontal motors