Moving servo with potentiometer

Does this code look right?

#include <Servo.h>
Servo servoMain;
int potPin = 2; // Analog port of potentiometer
int maxRot = 160; // Max rotation of Servo
int maxReadPot = 1023; // I read up that the analog ports read values from 0(0V) to 1023(5V)
float conv = maxRot/maxReadPot; // Converting the maxReadPot into angle measure for servo
int val = 0;

void setup()
{
servoMain.attach(3); // servo on digital pin ~3
}
void loop()
{
val = analogRead(potPin);
servoMain.write(val*conv);
}

So my issue is that no matter what rotation I turn the pot to, the servo is at 0 degrees.

Note: I'm a beginner, and if this code is correct, I'm going to assume my wiring is funky.

You do not say which Arduino you have, but pin 2 is unlikely to be an analog port. Perhaps you intended A2? It would help if you would:

  • Tell us what Arduino you have
  • Show us in a schematic or diagram how you have hooked things up.

A person who has made four posts should also know the following.

To post code and/or error messages:

  1. Use CTRL-T in the Arduino IDE to autoformat your complete code.
  2. Paste the complete autoformatted code between code tags (the </> button)
    so that we can easily see and deal with your code.
  3. Paste the complete error message between code tags (the </> button)
    so that we can easily see and deal with your messages.
  4. If you already posted without code tags, you may add the code tags by
    editing your post. Do not change your existing posts in any other way.
    You may make additional posts as needed.
  5. Please provide links to any libraries that are used
    (look for statements in your code that look like #include ). Many libraries
    are named the same but have different contents.

Before posting again, you should read the three locked topics at the top of the Programming Questions forum, and any links to which these posts point.

If your project involves wiring, please provide a schematic and/or a wiring diagram and/or a clear photograph of the wiring.

Good Luck!

Serial.println the Val. That will let you know if your pot is wired properly.
In setup, after you attach, servo.write(180) then delay(1000) then servo.write(0) then delay(1000)
That will let you know if your servo is wired properly

Are you powering the servo from the arduino 5v? This is troublesome at best. The arduino cannot provide the current a servo requires. Power the servo separately with common grounds.

Thanks, Vaj, I will take that into consideration on my next post.

I did manage to fix the issue, I changed these lines of code:

val = analogRead(potPin); 
servoMain.write(val*conv);

to:

val = analogRead(potPin);
val = map(val, 0, maxReadPot, 0, maxRot);
servoMain.write(val);

Make life simple.

analogRead() produces a value from 0 to 1023.
servo.Write() needs a value from 0 to 180 - about 1023/ 6
servo.writeMicroseconds() needs a value between 1000 and 2000

so either

val = analogRead(potPin);
servoMain.write(val / 6);

or (more accurate and faster)

val = analogRead(potPin);
servoMain.writeMicroseconds(val + 1000);

Avoid floating point maths wherever you can as it is very slow on an Arduino.

...R

FYI : When you divide 160 by 1023, both integers, the result is 0. When you assign that to the float variable 'conv' it gets changed to a float: 0.0. If you don't want the result to be truncated you have to convert one or both of the int values to float BEFORE the division.