Hello, I am fairly new to Arduino and I need help creating a code to operate my Arduino Tank. What I want to do is to control directly 2 DC motors (not servos) using a Parralax 2-axis Joystick. Each motor controls one track, I need the joystick to control the speed and direction of the tank. If you can help me out with a code I would be grateful.
This is what i have:
Arduino UNO R3
Arduino MotorShield R3
Parrallax 2-axis Joystick
2 DC Motors.
I made this code but it did not work:
/*
Obeject 12
Motor Control Test Program
*/
int channelA = 12; // Attach Channel A direction pin to digital pin 12.
int channelB = 13; // Attach Channel B direction pin to digital pin 13.
int brakeA = 9; // Attach Channel A brake pin to digital pin 9.
int brakeB = 8; // Attach Channel B brake pin to digital pin 8.
int joyX = 0; // Attach U/D to analog pin 0.
int joyY = 1; // Attach L/R to analog pin 1.
int val1 = 0; // Used to store value from analog pin 0.
int val2 = 0; // Used to store value from analog pin 1.
void forward() {
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
digitalWrite(channelA, HIGH);
digitalWrite(channelB, HIGH);
}
void backward() {
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
digitalWrite(channelA, LOW);
digitalWrite(channelB, LOW);
}
void left() {
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
digitalWrite(channelA, LOW);
digitalWrite(channelB, HIGH);
}
void right() {
digitalWrite(brakeA, LOW);
digitalWrite(brakeB, LOW);
digitalWrite(channelA, HIGH);
digitalWrite(channelB, LOW);
}
void brake() {
digitalWrite(brakeA, HIGH);
digitalWrite(brakeB, HIGH);
}
void setup() {
Serial.begin(9600);
pinMode(channelA, OUTPUT);
pinMode(channelB, OUTPUT);
pinMode(brakeA, OUTPUT);
pinMode(brakeB, OUTPUT);
}
void loop() {
val1 = analogRead(joyX);
val2 = analogRead(joyY);
Serial.println(val1);
Serial.println(val2);
if (val1 > 513)
{
forward();
}
if (val1 < 513)
{
backward();
}
if (val2 > 504)
{
left();
}
if (val2 < 504)
{
right();
}
}
Here is a new code I made where each axis controls 1 channel.
/*
Object 12
Motor Control Test Program
The joystick must be turned to a 45 degree angle.
*/
int dirA = 12; // Attach direction pin for Channel A to digital pin 12.
int dirB = 13; // Attach direction pin for Channel B to digital pin 13.
int speedA = 3; // Attach PWM for Channel A to digital pin 3.
int speedB = 11; // Attach PWM for Channel B to digital pin 11.
int brakeA = 9; // Attach brake pin for Channel A to digital pin 9.
int brakeB = 8; // Attach brake pin for Channel B to digital pin 8.
int joyX = A0; // Attach x-axis of joystick to analog pin 0.
int joyY = A1; // Attach y-axis of joystick to analog pin 1.
int valX = 0; // Variable to store value of x-axis.
int valY = 0; // Variable to store value of y-axis.
int valA = 0; // Variable to store value of PWM for Channel A.
int valB = 0; // Variable to store value of PWM for Channel B.
int deadzoneX = 514;
int deadzoneY = 507;
void setup() {
pinMode(dirA, OUTPUT); // Set as an output.
pinMode(dirB, OUTPUT); // Set as an output.
pinMode(speedA, OUTPUT); // Set as an output.
pinMode(speedB, OUTPUT); // Set as an output.
pinMode(brakeA, OUTPUT); // Set as an output.
pinMode(brakeB, OUTPUT); // Set as an output.
pinMode(joyX, INPUT); // Set as an input.
pinMode(joyY, INPUT); // Set as an input.
}
void loop() {
valX = analogRead(joyX); // Read joystick.
valY = analogRead(joyY); // Read joystick.
// Channel A
if (valY > deadzoneY) {
digitalWrite(brakeA, LOW); // Disengage the brakes on Channel A.
digitalWrite(dirA, HIGH); // Channel A go forwards.
// Speed
valA = map(valY, 507, 1023, 0, 255);
analogWrite(speedA, valA);
}
else if (valY < deadzoneY) {
digitalWrite(brakeA, LOW); // Disengage the brakes on Channel A.
digitalWrite(dirA, LOW); // Channel A go backwards.
// Speed
valA = map(valY, 0, 504, 0, 255);
analogWrite(speedA, valA);
}
else {
digitalWrite(brakeA, HIGH); // Engage brakes on Channel A.
}
// Channel B
if (valX > deadzoneX) {
digitalWrite(brakeB, LOW); // Disengage brakes on Channel B.
digitalWrite(dirB, HIGH); // Channel B go forwards.
// Speed
valB = map(valX, 514, 1023, 0, 255);
analogWrite(speedB, valB);
}
else if (valX < deadzoneX) {
digitalWrite(brakeB, LOW); // Disengage brakes for Channel B.
digitalWrite(dirB, LOW); // Channel B go backwards.
// Speed
valB = map(valX, 0, 511, 0, 255);
analogWrite(speedB, valB);
}
else {
digitalWrite(brakeB, HIGH); // Engage brakes on Channel B.
}
delay(50);
}