Servo motor problem (edited)

Hello, i have one problem, last time i fixed my previous problem and it worked perfectly, my previous problem was to connect 5 different LEDs and as i spin potentiometer they turn on and off in order from 1st one to last one, but now i wanna put here a servo motor, but this problem is that servo motor spin to 90 degrees and then he is starting to be “wild” going left and right rapidly :frowning:

I updated code little bit(to be more clear),and removed last one.

#include <Servo.h>

int anOutPin1 = 3;
int anOutPin2 = 5;
int anOutPin3 = 6;
int anOutPin4 = 9;
int anOutPin5 = 10; 

Servo servo1;

void setup() {
 servo1.attach(2);

}

void loop() {

  int outputValue = analogRead(A0);
  int kutnaVrijednost = map(outputValue, 0, 1023, 0, 180);
  servo1.write(kutnaVrijednost);
  
  delay(10);
  if (kutnaVrijednost >= 0 && kutnaVrijednost <= 16)
  {
    led0();
  }
  if (kutnaVrijednost >= 16 && kutnaVrijednost <= 36)
  {
    led1();
  }
   if (kutnaVrijednost >= 36 && kutnaVrijednost <= 72)
  {
    led2();
  }
   if (kutnaVrijednost >= 72 && kutnaVrijednost <= 108)
  {
    led3();
  }
   if (kutnaVrijednost >= 108 && kutnaVrijednost <= 144)
  {
    led4();
  }
   if (kutnaVrijednost >= 155 && kutnaVrijednost <= 180)
  {
    led5();
  }
  
}

//prepoznavanje ledica radi lakseg nacina rada
void led0()
{
  analogWrite(anOutPin1, LOW);
  analogWrite(anOutPin2, LOW);
  analogWrite(anOutPin3, LOW);
  analogWrite(anOutPin4, LOW);
  analogWrite(anOutPin5, LOW);
}  
void led1()
{
  analogWrite(anOutPin1, HIGH);
  analogWrite(anOutPin2, LOW);
  analogWrite(anOutPin3, LOW);
  analogWrite(anOutPin4, LOW);
  analogWrite(anOutPin5, LOW);
}  
void led2()
{
  analogWrite(anOutPin1, HIGH);
  analogWrite(anOutPin2, HIGH);
  analogWrite(anOutPin3, LOW);
  analogWrite(anOutPin4, LOW);
  analogWrite(anOutPin5, LOW);
}  
void led3()
{
  analogWrite(anOutPin1, HIGH);
  analogWrite(anOutPin2, HIGH);
  analogWrite(anOutPin3, HIGH);
  analogWrite(anOutPin4, LOW);
  analogWrite(anOutPin5, LOW);
}  
void led4()
{
  analogWrite(anOutPin1, HIGH);
  analogWrite(anOutPin2, HIGH);
  analogWrite(anOutPin3, HIGH);
  analogWrite(anOutPin4, HIGH);
  analogWrite(anOutPin5, LOW);
}  
void led5()
{
  analogWrite(anOutPin1, HIGH);
  analogWrite(anOutPin2, HIGH);
  analogWrite(anOutPin3, HIGH);
  analogWrite(anOutPin4, HIGH);
  analogWrite(anOutPin5, HIGH);
}  


//kraj

Thanks in advance!

Have you tried the sweep example? Because I don’t see anything wrong.

See a lot of redundant programming thou. For example the statement, why check the range for all? Just use a else if.

if(angle <= 16){
  led0();
}
//if it's not that you already KNOW it's bigger then 16..
else if(angle <= 36){
  led1()
}
//etc

And using analogWrite to just High LOW is baaaaaddd.

Also, declare pins output in setup.

Quick clean up. Only thing is, i made the leds scale linear. Don’t know if you want that.

#include <Servo.h>

const byte LedPins[] = {3, 5, 6, 9, 10};
const byte ServoPin = 2;

Servo myServo;

void setup() {
  myServo.attach(ServoPin);
  //make al led pins outputs
  for(byte i =0; i < sizeof(LedPins)){
    pinMode(LedPins[i], OUTPUT);
  }

}

void loop() {
  int outputValue = analogRead(A0);
  
  //move servo between 0 and 180
  myServo.write(map(outputValue, 0, 1023, 0, 180));
  
  //turn of the right led between 0 and the number of leds
  led(map(outputValue, 0, 1023, 0, sizeof(LedPins)));
 
}

void led(byte number){
  //i == number is only true (so turns on) when i is equal to number
  for(byte i = 0; i < sizeof(LedPins); i++){
    digitalWrite(LedPins[i], i == number);
  }
}

Thank you very much for your reply,i found it very useful, and ill improve and better organize my codes. :) But, code you posted dont works for me, LEDs and Servo motor dont work. I want to make voltmeter to 5v with servo motor 180 degrees scale and one led at each +1V so when servo motor turn on for example on 1v LED which is on 1v glows, so there i described my project lil bit better.

please don't cross post. I've answered your post in the other thread. Dwight