I'm building a motor crane and the motors don't work

Here is my code:

#include <Servo.h>

// Joystick pins
const int joy1_vrx = A0; // Joystick 1 X-axis (Servo 2)
const int joy2_vry = A1; // Joystick 2 Y-axis (DC motor)
const int joy1_vry = A2; // Joystick 1 Y-axis (Servo 1)

// Servo pins
const int servo1Pin = 11; // Servo 1
const int servo2Pin = 12; // Servo 2

// LED pins
const int greenLED1 = 7; // Servo 1 movement
const int greenLED2 = 8; // Servo 2 movement
const int redLED1 = 9; // DC motor movement
const int redLED2 = 10; // System active
const int redLED3 = 2; // Tracking sensor indicator

// Pushbutton and piezo
const int buttonPin = 3; // Active HIGH
const int piezoPin = 6; // Piezo buzzer

// L298N DC Motor
const int motorPin1 = 5; // IN3
const int motorPin2 = 4; // IN4

// Tracking sensor
const int trackingSensorPin = 13; // Digital output

// Servo objects
Servo servo1;
Servo servo2;

// State variables
unsigned long motorDelayUntil = 0;
unsigned long lastBeep = 0;

void setup() {
// Attach servos
servo1.attach(servo1Pin);
servo2.attach(servo2Pin);

// Set up LEDs
pinMode(greenLED1, OUTPUT);
pinMode(greenLED2, OUTPUT);
pinMode(redLED1, OUTPUT);
pinMode(redLED2, OUTPUT);
pinMode(redLED3, OUTPUT);

// Set up motor pins
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);

// Set up button and piezo
pinMode(buttonPin, INPUT); // Active HIGH
pinMode(piezoPin, OUTPUT);

// Tracking sensor
pinMode(trackingSensorPin, INPUT);

Serial.begin(9600);
}

void loop() {
// System always active
digitalWrite(redLED2, HIGH);

// Piezo beeps every 4 seconds
if (millis() - lastBeep >= 4000) {
tone(piezoPin, 1000, 200); // 1kHz beep for 200ms
lastBeep = millis();
}

// Tracking sensor LED (ON only when object detected)
int trackingValue = digitalRead(trackingSensorPin);
digitalWrite(redLED3, trackingValue == HIGH ? HIGH : LOW);

// Read joystick values
int joy1X = analogRead(joy1_vrx); // 0-1023
int joy2Y = analogRead(joy2_vry); // 0-1023
int joy1Y = analogRead(joy1_vry); // 0-1023

// Print to Serial Monitor
Serial.print("Joystick 1 X: "); Serial.print(joy1X);
Serial.print(" | Joystick 1 Y: "); Serial.print(joy1Y);
Serial.print(" | Joystick 2 Y: "); Serial.println(joy2Y);

// Pushbutton: delay all motors if pressed (active HIGH)
if (digitalRead(buttonPin) == HIGH) {
motorDelayUntil = millis() + 3000;
}

if (millis() > motorDelayUntil) {
// --- Servo 1 (Joystick 1 Y, continuous rotation) ---
int servo1Speed = map(joy1Y, 0, 1023, 0, 180);
if (abs(joy1Y - 512) > 50) {
servo1.write(servo1Speed);
digitalWrite(greenLED1, HIGH);
} else {
servo1.write(90); // Stop
digitalWrite(greenLED1, LOW);
}

// --- Servo 2 (Joystick 1 X, continuous rotation) ---
int servo2Speed = map(joy1X, 0, 1023, 0, 180);
if (abs(joy1X - 512) > 50) {
  servo2.write(servo2Speed);
  digitalWrite(greenLED2, HIGH);
} else {
  servo2.write(90); // Stop
  digitalWrite(greenLED2, LOW);
}


// --- DC Motor (Joystick 2 Y) ---
bool dcMoving = false;
if (joy2Y < 400) { // Backward
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  dcMoving = true;
} else if (joy2Y > 600) { // Forward
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  dcMoving = true;
} else {
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, LOW);
}
digitalWrite(redLED1, dcMoving ? HIGH : LOW);

} else {
// Motors off during delay
servo1.write(90);
servo2.write(90);
digitalWrite(greenLED1, LOW);
digitalWrite(greenLED2, LOW);
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(redLED1, LOW);
}
}

I have leds but they should work, My joysticks work in serial, I just want to know if the problem is the code or the physical wiring. I have servo1 and servo2, and a DC motor at the top that lowers string with a magnet. For the DC, I'm using an L298N motor driver. If someone could read over my code for the servos and dc motor I'd appreciate it, Thanks in advance, Thomas *360 degree servos

Using correct tags redo your tópic putting code betwen tags.
See example:

Also post your wiring schematic.

@ilikearduinoyay Welcome to the forum!

To increase your chances of getting assistance, please do the following:

Please open your sketch in the IDE, and then use the following three keyboard shortcuts:

  1. Ctrl+A
  2. Ctrl+T
  3. Ctrl+Shift+C

Next, open a response in this thread, and use the following shortcut:

Ctrl+V

Finally, click the ⮪ Reply button.

In addition, please post a schematic diagram (or drawing) of your circuit.

If you want software to draw the schematic you can get KiCad for free and it is an unlimited full version.

That is a very bad idea. That chip has been obsolete for many decades and is useless in modern systems. This is because it is transistor based and looses about 3V. You need to use something more modern, like a FET based H-bridge or half H-bridge chip.

Are you sure that this is not a continuous "servo" as true 360˚ servos are rare. The only servo I know that will give you true 360˚ is a three and a half turn sail winch servo.

Hi, @ilikearduinoyay
Welcome to the forum.

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Can you please post a link to data/specs of the motors?
What are you using for a power supply?

Tom.... :smiley: :+1: :coffee: :australia: