Voltmeter (to 5V) (LEDs and servo motor)

Hello!
Im working on my new shool project since friday and i posted this similar problem before, but now i changed code so i wanna post it again.

My theme is the voltmeter to 5V, and worked like this: a servo motor needle would show a voltage on a hemispherical scale, with 5 LEDs would be deployed to each value to 5V. When the needle shows, for example, 1V, the LED that is located on the 1V lights …

Here is problem:Everything is normal, including sevo motor needle and LEDs works properly to 4V(i watch voltage on serial monitor), but on serial monitor when voltage come to 4V, servo motor needle go to 0 degrees and vibrate, and last 2 diodes which need to indicate 4 and 5 volts dont wants to turn on.

Here is my code:
BTW “napon” means voltage, and “kutnaVrijednost” means angleValue

#include <Servo.h>

float napon = 0;
int outputValue = 0;
int anOutPin1 = 3;
int anOutPin2 = 5;
int anOutPin3 = 6;
int anOutPin4 = 9;
int anOutPin5 = 10; 

Servo servo1;

void setup() {
 Serial.begin(9600);
 servo1.attach(2);

}

void loop() {
  outputValue = analogRead(A0);
  int kutnaVrijednost = map(outputValue, 0, 1023, 0, 180);
  napon = outputValue * (5 / 1023.0);
  servo1.write(kutnaVrijednost);
  Serial.println(napon);
 delay(10);

 if(napon < 1){
  led0();
}

else if(napon < 2){
  led1();
}

else if(napon < 3){
  led2();
}
else if(napon < 4){
  led3();
}
else if(napon < 5){
  led4();
}
else if(napon >= 5){
  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

You need to be careful about using map() and end values.
Print out kutnaVrijednost to see what is going wrong.
you might try 0, 179 instead of 0, 180 to see if that helps
some.
Or, better yet, don’t use map() and do your own math in floating
point with proper limits when converting back to degrees.
Dwight
ps.
I just looked up servo.h. It is likely that the built-in parameter for
full scale pulse width doesn’t match your servo. Sending it to 180
may be off scale of the pot in the servo.
Please look at writeMicroseconds() help page. You may want to use that
instead to have better control of the range used.
You’ll need to do a little experimenting to find the range that works
right for your servo.
I would still advice not using map() as your only throwing away
significant bits when using writeMicroseconds() to integer degrees.
Dwight

It would be much easier to use a common voltmeter and an LM3914. :grinning:

Thanks for your replies! Ive done a little experimenting and hen i removed last two leds from code and the servo motor spinned little bit more further (to 126 degrees,before was to 90 degrees) before the problem again appears, can voltage be the problem? Im using 220 ohm resistor for each led, maybe i can use smaller reistance if voltage is a problem?

I think we need a picture of your layout and a schematic. Dwight

I just scanned your code. You do not want to do analogWrite() to send to your LEDs, I know the pins are listed as analog pins but you are using them as digital pins. Fix all the LED pins as follows:

analogWrite(anOutPin1, HIGH);

should be

digitalWrite(anOutPin1, HIGH);

Also in your setup(), you should do:

pinMode(anOutPin1,OUTPUT);

You need to do this for all of the LED pins. This is a common mistake. The pins have dual functions. I have not worked with servo.h but suspect it used analogWrite() internally. I'm thinking your use of HIGH and LOW to analogWrite() may be change the parameters used by servo.h. I suspect it may be the clock rate is higher for servo.h but all the analog out pins use the same clock. Dwight

PS I did some more digging and found a release note. Here is what it said:

"The Servo library supports up to 12 motors on most Arduino boards and 48 on the Arduino Mega. On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins. On the Mega, up to 12 servos can be used without interfering with PWM functionality; use of 12 to 23 motors will disable PWM on pins 11 and 12. "

Your LEDs 4 and 5 are on analog pins 9 and 10. Nothing was warning you not to use analogWrite() on those pins. I suspect servo.h is using the counters that would be used by these pins, to run the pulse timing needed by servos. ( Nice of them to tell us this! ). You should have no more issues once you correct to digitalWrite() as I have noted above. Most servos take a pulse that can be between 0.5ms and 1.5ms ( as I recall, might be slightly different ). The servos expects this to be repeated about every 20ms to 50ms. This requires counters and interrupts. I suspect it uses the ones needed for analogWrite() and your using analogWrite() rather than digitWrite() on these pins is trashing the counter values. Opps my error, you should define pins: not: int anOutPin1 = 3;

should be:

int anOutPin1 = an3;

for use with digitalWrite()

Dwight PS pictures didn't make it. There is a help for that if you do a search.

I should look things up more often. Having a bad day.

int anOutPin1 = an3;

should be:

int anOutPin1 = A3;

see reference:

https://www.arduino.cc/en/Tutorial/AnalogInputPins

Sorry Dwight

dwightthinker: Having a bad day.

You sure are!

dwightthinker: I have not worked with servo.h but suspect it used analogWrite() internally.

Why would it?

dwightthinker: Most servos take a pulse that can be between 0.5ms and 1.5ms ( as I recall, might be slightly different ).

So you could not use the PWM function on them could you?

And apparently - in another thread - many servos use a much wider, non-standard range of values.

Paul__B: You sure are! Why would it? So you could not use the PWM function on them could you?

And apparently - in another thread - many servos use a much wider, non-standard range of values.

PWM function is not 2ms rep rate but apparently it does use the same counters. Looking at the documents, it does use the counters that would be for two of the PWM pins. It would need the two counters in order for the slower timing to be interrupt driven. That is why you can't use those pins for PWM.

Yes, some servos use different ranges of values. Some use inverted pulses as well. I used to make RC slope gliders and had to modify the timing on servos to match transmitters at times.

I seem to be upsetting you today. Maybe your having a bad day to. I'm maybe not as sharp as you and sometimes make mistakes. I admit to those that I do make. I'm also new to the arduino but I don't think that means I can't help when I see something wrong. I could just go back and edit my post to remove errors but then someone might then not go back and reread the corrected ones. I think it is better to append the corrections. Maybe that offends you. Dwight