Servo motor and joystick issues

I have been trying to make a simple servo that moves when I move my joystick's x-axis. Using the serial monitor, I can deduce that everything works, but my servo does not move at all. I highly doubt its a wiring issue, so can you please check over my code to see if I missed something??


#include <Servo.h>
// Arduino pin numbers
Servo myservo;
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = A0; // analog pin connected to X output
const int Y_pin = A1; // analog pin connected to Y output
int pos = 0;

void setup() 
{
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  myservo.attach(8);
  Serial.begin(9600);
}

void loop() 
{
  if (analogRead(X_pin) >= 600)
  {
    pos = pos - 1;
    delay(100);
  }

  if (analogRead(X_pin) <= 400)
  {
    pos = pos + 1;
    delay(100);
  }

  if (pos < 0)
  {
    pos = myservo.read();
  }

  myservo.write(pos);
  Serial.print(pos);
  Serial.print(" / ");
  Serial.println(analogRead(X_pin));
  delay(500);
 
}

I think the opposite.
Please post schematics.

How is this possible? The problem is probably your wiring. Show a wiring diagram.

Hi, @squeezlol
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.
Not a Fritzy image.

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

Welcome to the forum!

First, your code basically works. Evidence:

So, how about that wiring diagram? And remember, if your servo draws too much current(you haven't told us which one you have, link please), your external power source may fail, OR, your USB may be damaged, though it's more likely to cut power to your port and leave you wondering why your Arduino doesn't work...

Oh, and by the way, your servo moves glacially, because you're taking a 500 ms dirt nap every loop.

yooo nvm gng i figured it out i had the servo on 3.3 v instead of 5v tysm yall

Take a look at that Wokwi anyway, it's a whole lot easier to test code there than to ask here. Not that we mind answering questions, but we do prefer new ones...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.