Why wont my code work???? please help!

Hi everyone just so you know this is my first time messing with arduino projects so i apologize if i seem like a noob but i cant seem to get my servo to move whenever i use an if statement and its driving me crazy!

my goal is to move the servo if there is a voltage detected on the analog pin (for the sake of troubleshooting i also am turning an led on) When i flash the code onto my NodeMCU esp8266 and apply voltage to the A0 pin the serial monitor prints it acurately and the led turns on but the servo does not move...... when i remove the voltage from the A0 pin the led goes off and again the servo does nothing..... i tested the servo with some basic commands and it functions as you would expect so ik its hooked up right. Here is my code any help is greatly appreciated :slight_smile:

#include <Servo.h>

int  sensorValue;
Servo servo_2;

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  pinMode(14, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0);
  // print out the value you read:
  Serial.println(voltage);

  if (voltage > 2) {
      digitalWrite(14,HIGH);
      servo_2.write(90);
      delay(100);

    }else{
      
      digitalWrite(14,LOW);
      servo_2.write(0);
      delay(100);
      }
}

Battery_Charger.ino (706 Bytes)

Would attaching the servo to a pin in the code help, do you think ?

OMG i feel like such an idiot lol that fixed it!!!!!! THANK YOU!!!