Having a problem with potentiometer read

Hi everyone, I'm a bit new to using arduino and I'm having a problem. I'm using a potentiometer in my circuit as part of a larger project. I attached the two side pins on the pot to the 5v and gnd pins on the arduino and I connected the wiper pin on the pot to an analog input pin on the arduino. The arduino is supposed do an analog read on the input from the potentiometer and then output to one of five different pins based on the position. I've attached five LEDs to the five output pins. When the pot is all the way to the left it is supposed to turn on the light all the way to the left, when it is on the right it is supposed to turn on the light to the right and so forth. I've programmed in different analog value ranges to tell the arduino which light to turn on based on the numerical value given by the analogRead function. The pot has a built in spring so that it always goes back to the middle position when it is not being pushed on, similar to how a joystick typically works. Now, here's the problem. When I move the stick to the left, the light to the left lights up as it is supposed to, but the middle light representing the center position on the pot also stays partially lit. It glows more dimly but it is still partially on. I can't figure out why it would do that. Is it a problem with the pot or have I overlooked something in my program? What sort of issue could cause that to happen? I assume that since the arduino can't output at anything except 5v it must be some kind of flickering between values, but none of the other lights come on dimly, just one representing the center postion. Please help.

Let’s see your complete code.

Use CTRL T to format your code.
Attach your sketch between code tags
[code]Paste your sketch here[/code]

Show us a good schematic & image of your circuit.
Posting images:
http://forum.arduino.cc/index.php?topic=519037.0

Alright, here are the relevant portions of the code (the whole program is about 1300 lines long and this is just a small piece, but everything else seems to work except for this part).

  int ContLX1 = 44;
  int ContLX2 = 45;
  int ContLX3 = 46;
  int ContLX4 = 47;
  int ContLX5 = 48;
  int LXPin = A0;

void setup(){
  pinMode(ContLX1, OUTPUT);
  pinMode(ContLX2, OUTPUT);
  pinMode(ContLX3, OUTPUT);
  pinMode(ContLX4, OUTPUT);
  pinMode(ContLX5, OUTPUT);
  pinMode(LXPin, INPUT);
}

void loop(){
  if(LXCur() >= 875){
    LStickX(4);
  }
  if((LXCur() < 875) && (LXCur() >= 625)){
    LStickX(3);
  }
  if((LXCur() >= 375) && (LXCur() < 625)){
    LStickX(2);
  }
  if((LXCur() >= 125) && (LXCur() < 375)){
    LStickX(1);
  }
  if(LXCur() < 125){
    LStickX(0);
  }
}

int LXCur(){
  int Read = analogRead(LXPin);
  return Read;
}

void LStickX(int g){
  if(g == 0){
    digitalWrite(ContLX1, HIGH);
    digitalWrite(ContLX2, LOW);
    digitalWrite(ContLX3, LOW);
    digitalWrite(ContLX4, LOW);
    digitalWrite(ContLX5, LOW);
  }
  if(g == 1){
    digitalWrite(ContLX1, LOW);
    digitalWrite(ContLX2, HIGH);
    digitalWrite(ContLX3, LOW);
    digitalWrite(ContLX4, LOW);
    digitalWrite(ContLX5, LOW);
  }
  if(g == 2){
    digitalWrite(ContLX1, LOW);
    digitalWrite(ContLX2, LOW);
    digitalWrite(ContLX3, HIGH);
    digitalWrite(ContLX4, LOW);
    digitalWrite(ContLX5, LOW);
  }
  if(g == 3){
    digitalWrite(ContLX1, LOW);
    digitalWrite(ContLX2, LOW);
    digitalWrite(ContLX3, LOW);
    digitalWrite(ContLX4, HIGH);
    digitalWrite(ContLX5, LOW);
  }
  if(g == 4){
    digitalWrite(ContLX1, LOW);
    digitalWrite(ContLX2, LOW);
    digitalWrite(ContLX3, LOW);
    digitalWrite(ContLX4, LOW);
    digitalWrite(ContLX5, HIGH);
  }
}

Pins 44-48 of the arduino (it's a mega 2560) are each sent to LEDs whose negative terminals are connected to the ground of the arduino. Pin A0 goes to the wiper on the pot. As for a picture, this is part of a larger project so my setup is a lot more complex than just this. I can try to take a picture of it but it could take a lot of work to make the right parts clearly visible and I don't have much time right now, I can try to upload an image tomorrow if you still need it but I'm hoping the problem is just in the code.

Try this.
Leo..

const byte ContLX1 = 44;
const byte ContLX2 = 45;
const byte ContLX3 = 46;
const byte ContLX4 = 47;
const byte ContLX5 = 48;
const byte LXPin = A0;
int rawValue;

void setup() {
  pinMode(ContLX1, OUTPUT);
  pinMode(ContLX2, OUTPUT);
  pinMode(ContLX3, OUTPUT);
  pinMode(ContLX4, OUTPUT);
  pinMode(ContLX5, OUTPUT);
}

void loop() {
  rawValue = analogRead(LXPin) >> 7; // 10-bit to 3-bit
  switch (rawValue) {
    case 0:
      digitalWrite(ContLX1, HIGH);
      digitalWrite(ContLX2, LOW);
      digitalWrite(ContLX3, LOW);
      digitalWrite(ContLX4, LOW);
      digitalWrite(ContLX5, LOW);
      break;
    case 1:
      rawValue = 2;
    case 2:
      digitalWrite(ContLX1, LOW);
      digitalWrite(ContLX2, HIGH);
      digitalWrite(ContLX3, LOW);
      digitalWrite(ContLX4, LOW);
      digitalWrite(ContLX5, LOW);
      break;
    case 3:
      rawValue = 4;
    case 4:
      digitalWrite(ContLX1, LOW);
      digitalWrite(ContLX2, LOW);
      digitalWrite(ContLX3, HIGH);
      digitalWrite(ContLX4, LOW);
      digitalWrite(ContLX5, LOW);
      break;
    case 5:
      rawValue = 6;
    case 6:
      digitalWrite(ContLX1, LOW);
      digitalWrite(ContLX2, LOW);
      digitalWrite(ContLX3, LOW);
      digitalWrite(ContLX4, HIGH);
      digitalWrite(ContLX5, LOW);
      break;
    case 7:
      digitalWrite(ContLX1, LOW);
      digitalWrite(ContLX2, LOW);
      digitalWrite(ContLX3, LOW);
      digitalWrite(ContLX4, LOW);
      digitalWrite(ContLX5, HIGH);
      break;
  }
}

You should have series resistors with the leds too - you haven’t mentioned those ( 270 ohmmis good ) .

Okay, I tried the code you suggested in a separate program and it worked flawlessly. Then I tried it in my actual program and it didn't work. After some time looking over the code I realized that I had placed an else statement at the end of the program (it was left over from a previous version of the project). This statement told the arduino to reset the light to the center position. So the arduino was actually alternating between executing the else statement and executing the command I gave it with the potentiometer. Once I got rid of that, it all worked flawlessly. Thanks for all the help.

btw my LEDs have built in resistors don't worry.

After some time looking over the code I realized that I had placed an else statement at the end of the program (it was left over from a previous version of the project).

This is precisely why we ask you to post the entire sketch, not part of it.

You wouldn't expect a doctor to diagnose an illness just from looking at your finger? Problems
can be anywhere in the code, and the more strange a problem is the more likely its somewhere
you hadn't thought of...