I want to set a series of LEDs to a potentiometer

Alright, I’m pretty new to Arduino. I have fiddled around with mine before, but I’m finally getting into the programming. I have a series of 15 LEDs, and as the Potentiometer goes up, the LEDs will turn off, starting with the outermost and descending into the centermost. Once you turn past the centermost, a second set of Red LEDs will turn on. (If you want to know what this is for, it is going to be like the shield bar in the Halo series of games). I copied the base of this code from someone who did something similar with only three LEDs. I am trying to use it, but it sets off the wrong LEDs at the wrong values, and most of the time all of them are ON. I am testing the circuit using tinkercad, so i know that im not having a hardware issue. Plz help.

HALO_HELMET_BAR.ino (3.32 KB)

/*
  AnalogReadSerial

  Reads an analog input on pin 0, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/

int LED1 = 2;
int LED2 = 3;
int LED3 = 4;
int LED4 = 5;
int LED5 = 6;
int LED6 = 7; 
int LED7 = 8;
int LED8 = 9;
int LED9 = 10;
int potPin = A0;
int val;

void setup() {
   pinMode(LED1, OUTPUT);
   pinMode(LED2, OUTPUT);
   pinMode(LED3, OUTPUT);
   pinMode(LED4, OUTPUT);
   pinMode(LED5, OUTPUT);
   pinMode(LED6, OUTPUT);
   pinMode(LED7, OUTPUT);
   pinMode(LED8, OUTPUT);
   pinMode(LED9, OUTPUT);
}


void loop() {
  val = analogRead(potPin);
  val = map(val, 0, 1023, 0, 180);
  
  if (val == 0) {
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(LED4, HIGH);
  digitalWrite(LED5, HIGH);
  digitalWrite(LED6, HIGH);
  digitalWrite(LED7, HIGH);
  digitalWrite(LED8, HIGH);
  digitalWrite(LED9, LOW);
  } else if (val > 127.875) {
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(LED4, HIGH);
  digitalWrite(LED5, HIGH);
  digitalWrite(LED6, HIGH);
  digitalWrite(LED7, HIGH);
  digitalWrite(LED8, LOW);
  digitalWrite(LED9, LOW);
    } else if (val > 255.75) {
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(LED4, HIGH);
  digitalWrite(LED5, HIGH);
  digitalWrite(LED6, HIGH);
  digitalWrite(LED7, LOW);
  digitalWrite(LED8, LOW);
  digitalWrite(LED9, LOW);
      } else if (val > 383.625) {
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(LED4, HIGH);
  digitalWrite(LED5, HIGH);
  digitalWrite(LED6, LOW);
  digitalWrite(LED7, LOW);
  digitalWrite(LED8, LOW);
  digitalWrite(LED9, LOW);
      } else if (val > 511.5) {
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(LED4, HIGH);
  digitalWrite(LED5, LOW);
  digitalWrite(LED6, LOW);
  digitalWrite(LED7, LOW);
  digitalWrite(LED8, LOW);
  digitalWrite(LED9, LOW);
        } else if (val > 639.375) {
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, LOW);
  digitalWrite(LED6, LOW);
  digitalWrite(LED7, LOW);
  digitalWrite(LED8, LOW);
  digitalWrite(LED9, LOW);
          } else if (val > 767.25) {
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, LOW);
  digitalWrite(LED6, LOW);
  digitalWrite(LED7, LOW);
  digitalWrite(LED8, LOW);
  digitalWrite(LED9, LOW);
            } else if (val > 895.125) {
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, LOW);
  digitalWrite(LED6, LOW);
  digitalWrite(LED7, LOW);
  digitalWrite(LED8, LOW);
  digitalWrite(LED9, LOW);
    } else {
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, LOW);
  digitalWrite(LED3, LOW);
  digitalWrite(LED4, LOW);
  digitalWrite(LED5, LOW);
  digitalWrite(LED6, LOW);
  digitalWrite(LED7, LOW);
  digitalWrite(LED8, LOW);
  digitalWrite(LED9, HIGH);
      }
    delay(1);
  }

I don't know why you need the map command on line 40. It maps the range of val down to 0-180, but a lot of your if/else if statements are checking for val being much greater than 180.

The limits in the if / else if statements look a bit muddled to me, but once you lose that map command I think you'll be able to work through the rest by experimentation. I don't think the first one should be if (val ==0) for example.

val is declared as an integer. analogRead can only return integers. So what is the point testing val against things like 639.375? It can be up to 639 or 640 or more, nothing in between.

Oh and you can't just use > in your if/else string. Think about it. If the value is 1023 it is > 127. So the first if fires and all the others are skipped.

And that's all provided you get rid of the map() statement that forces val to only be between 0 an 180.

Steve

If you have a bunch of names that only vary by a trailing number, you want an array:

const byte LEDCount = 9;
const byte LEDPins[LEDCount] = {2, 3, 4, 5, 6, 7, 8, 9, 10};

const byte potPin = A0;

void setup()
{
  for (byte i = 0; i < LEDCount; i++)
    pinMode(LEDPins[i], OUTPUT);
}

void loop()
{
  int val = analogRead(potPin);

  for (byte i = 0; i < LEDCount; i++)
  {
    // Calculate which LEDs are lit
    bool lit = false;  // Default to off
    if (val >= 512)  // Midpoint or greater
    {
      int step = map(val, 512, 1023, 0, 4);
      if (i >= (4-step) && i <= (4+step))
        lit = true;
    }
    digitalWrite(LEDPins[i], lit);
  }
}