Hello everyone,
I am a beginner of Arduino. I am doing a claw machine project using an arduino uno, 2 x joystick modules, 2 x L298N motor shields, 3 dc motors, 1 servo motor and 1 power supply of 12V, 2A to archieve.
I have first written the following codes for using 2 x joystick modules, 2 x L298N motor shields and 3 dc motors to control the x and y axis as well as the up and down motion of the claw (everything works fine):
//Joystick Pins
int x1_key = A0;
int y1_key = A1;
int y2_key = A3;
int x1_pos;
int y1_pos;
int y2_pos;
//Motor Pins
int EN_A = 3; //Enable pin for first motor
int IN1 = 2; //control pin for first motor
int IN2 = 4; //control pin for first motor
int IN3 = 5; //control pin for second motor
int IN4 = 7; //control pin for second motor
int EN_B = 6; //Enable pin for second motor
int EN_C = 9; //Enable pin for first motor
int IN5 = 10; //control pin for first motor
int IN6 = 11; //control pin for first motor
//Initializing variables to store data
int motor_speed;
int motor_speed1;
int motor_speed2;
void setup ( ) {
Serial.begin (9600); //Starting the serial communication at 9600 baud rate
//Initializing the motor pins as output
pinMode(EN_A, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(EN_B, OUTPUT);
pinMode(IN5, OUTPUT);
pinMode(IN6, OUTPUT);
pinMode(EN_C, OUTPUT);
//Initializng the joystick pins as input
pinMode (x1_key, INPUT);
pinMode (y1_key, INPUT);
pinMode (y2_key, INPUT);
}
void loop () {
x1_pos = analogRead (x1_key) ; //Reading the horizontal movement value
y1_pos = analogRead (y1_key) ; //Reading the vertical movement value
y2_pos = analogRead (y2_key) ; //Reading the vertical movement value
if (x1_pos < 400){ //Rotating the left motor in clockwise direction
motor_speed = map(x1_pos, 400, 0, 0, 100); //Mapping the values to 0-255 to move the motor
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(EN_A, motor_speed);
}
else if (x1_pos>400 && x1_pos <600){ //Motors will not move when the joystick will be at center
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
else if (x1_pos > 600){ //Rotating the left motor in anticlockwise direction
motor_speed = map(x1_pos, 600, 1023, 0, 100);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(EN_A, motor_speed);
}
if (y1_pos < 400){ //Rotating the right motor in clockwise direction
motor_speed1 = map(y1_pos, 400, 0, 0, 100);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(EN_B, motor_speed1);
}
else if (y1_pos>400 && y1_pos <600){
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else if (y1_pos > 600){ //Rotating the right motor in anticlockwise direction
motor_speed1 = map(y1_pos, 600, 1023, 0, 100);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(EN_B, motor_speed1);
}
if (y2_pos < 400){ //Rotating the right motor in clockwise direction
motor_speed2 = map(y2_pos, 400, 0, 0, 100);
digitalWrite(IN5, HIGH);
digitalWrite(IN6, LOW);
analogWrite(EN_C, motor_speed2);
}
else if (y2_pos>400 && y2_pos <600){
digitalWrite(IN5, LOW);
digitalWrite(IN6, LOW);
}
else if (y2_pos > 600){ //Rotating the right motor in anticlockwise direction
motor_speed2 = map(y2_pos, 600, 1023, 0, 100);
digitalWrite(IN5, LOW);
digitalWrite(IN6, HIGH);
analogWrite(EN_C, motor_speed2);
}
}
And then when i add the codes for the rest servo motor (x2 axis), the servo motor works but the dc motor controlling the up and down motion shows no response. Is there a way that i can fix the error?
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 5; // variable to store the servo position and set zero
//Joystick Pins
int x1_key = A0;
int y1_key = A1;
int x2_key = A2;
int y2_key = A3;
int x1_pos;
int y1_pos;
int x2_pos;
int y2_pos;
//Motor Pins
int EN_A = 3; //Enable pin for first motor
int IN1 = 2; //control pin for first motor
int IN2 = 4; //control pin for first motor
int IN3 = 5; //control pin for second motor
int IN4 = 7; //control pin for second motor
int EN_B = 6; //Enable pin for second motor
int EN_C = 9; //Enable pin for first motor
int IN5 = 10; //control pin for first motor
int IN6 = 11; //control pin for first motor
//Initializing variables to store data
int motor_speed;
int motor_speed1;
int motor_speed2;
void setup ( ) {
Serial.begin (9600); //Starting the serial communication at 9600 baud rate
//Initializing the motor pins as output
myservo.attach(12); // attaches the servo on pin 12 to the servo object
pinMode(EN_A, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
pinMode(EN_B, OUTPUT);
pinMode(IN5, OUTPUT);
pinMode(IN6, OUTPUT);
pinMode(EN_C, OUTPUT);
//Initializng the joystick pins as input
pinMode (x1_key, INPUT);
pinMode (x2_key, INPUT);
pinMode (y1_key, INPUT);
pinMode (y2_key, INPUT);
}
void loop () {
x1_pos = analogRead (x1_key) ; //Reading the horizontal movement value
y1_pos = analogRead (y1_key) ; //Reading the vertical movement value
x2_pos = analogRead (x2_key) ; //Reading the horizontal movement value
y2_pos = analogRead (y2_key) ; //Reading the vertical movement value
if (x1_pos < 400){ //Rotating the left motor in clockwise direction
motor_speed = map(x1_pos, 400, 0, 0, 100); //Mapping the values to 0-255 to move the motor
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(EN_A, motor_speed);
}
else if (x1_pos>400 && x1_pos <600){ //Motors will not move when the joystick will be at center
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
else if (x1_pos > 600){ //Rotating the left motor in anticlockwise direction
motor_speed = map(x1_pos, 600, 1023, 0, 100);
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(EN_A, motor_speed);
}
if (y1_pos < 400){ //Rotating the right motor in clockwise direction
motor_speed1 = map(y1_pos, 400, 0, 0, 100);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(EN_B, motor_speed1);
}
else if (y1_pos>400 && y1_pos <600){
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else if (y1_pos > 600){ //Rotating the right motor in anticlockwise direction
motor_speed1 = map(y1_pos, 600, 1023, 0, 100);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(EN_B, motor_speed1);
}
if(x2_pos < 400){
for (pos = pos; pos <= 55; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
} else if (x2_pos>400 && x2_pos <600){
myservo.write(pos); } else{
for (pos = pos; pos >= 5; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
if (y2_pos < 400){ //Rotating the right motor in clockwise direction
motor_speed2 = map(y2_pos, 400, 0, 0, 100);
digitalWrite(IN5, HIGH);
digitalWrite(IN6, LOW);
analogWrite(EN_C, motor_speed2);
}
else if (y2_pos>400 && y2_pos <600){
digitalWrite(IN5, LOW);
digitalWrite(IN6, LOW);
}
else if (y2_pos > 600){ //Rotating the right motor in anticlockwise direction
motor_speed2 = map(y2_pos, 600, 1023, 0, 100);
digitalWrite(IN5, LOW);
digitalWrite(IN6, HIGH);
analogWrite(EN_C, motor_speed2);
}
}
Thank you everyone.