Help Understanding: if / then / else statements, please.

Hello. Thank you all in advance for taking the time to read this and attempt to help me.

I have and Arduino Uno and have 16 LED’s Hooked up to it.
I am using pins:
A0, A1, A2, A3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13.

I want to turn on each light depending on what value my potentiometer is reading.
Ideally I want it to stay on until the value changes, but currently I have it timed, as I don’t know any better.
Could you review my code and see what I am doing wrong.

My current error is: expected identifier before ‘(’ token.
on
and (potPin < 830.7);

Thanks again!

//start of my code

int potPin = A4; // select the input pin for the potentiometer
int ledPin = 2; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor

void setup() {

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
}

void loop() {
val = analogRead(potPin); // read the value from the sensor
if (potPin == 0);

digitalWrite(2, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(2, LOW); // turn the ledPin off
delay(1000); // stop the program for some time

if (potPin > 0);
and (potPin < 63.9);

digitalWrite(3, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(3, LOW); // turn the ledPin off
delay(1000); // stop the program for some time

if (potPin >63.9);
and (potPin < 127.8);

digitalWrite(4, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(4, LOW); // turn the ledPin off
delay(1000); // stop the program for some time

if (potPin >127.8);
and (potPin < 191.7);

digitalWrite(5, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(5, LOW); // turn the ledPin off
delay(1000); // stop the program for some time

if (potPin >191.7);
and (potPin < 255.6);

digitalWrite(6, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(6, LOW); // turn the ledPin off
delay(1000); // stop the program for some time

if (potPin >255.6);
and (PotPin < 319.5);

digitalWrite(7, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(7, LOW); // turn the ledPin off
delay(1000); // stop the program for some time

if (potPin >319.5);
and (potPin < 383.4);

digitalWrite(8, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(8, LOW); // turn the ledPin off
delay(1000); // stop the program for some time

if (potPin >383.4);
and (potPin < 447.3);

digitalWrite(9, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(9, LOW); // turn the ledPin off
delay(1000); // stop the program for some time

if (potPin >447.3);
and (potPin < 511.2);

digitalWrite(10, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(10, LOW); // turn the ledPin off
delay(1000); // stop the program for some time

if (potPin >511.2);
and (potPin < 575.1);

digitalWrite(11, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(11, LOW); // turn the ledPin off
delay(1000); // stop the program for some time

if )potPin >575.1);
and (potPin < 639)

digitalWrite(12, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(12, LOW); // turn the ledPin off
delay(1000); // stop the program for some time

if (potPin >639):
and (potPin < 702.9);

digitalWrite(13, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(13, LOW); // turn the ledPin off
delay(1000); // stop the program for some time

if (potPin >702.9);
and (potPin < 766.8);

digitalWrite(14, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(14, LOW); // turn the ledPin off
delay(1000); // stop the program for some time

if (potPin >766.8);
and (potPin < 830.7);

digitalWrite(15, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(15, LOW); // turn the ledPin off
delay(1000); // stop the program for some time

if (potPin >830.7);

digitalWrite(16, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(16, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
else;
digitalWrite (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16, LOW)

}

The Arduino IDE does not use the BASIC language!

Arduino has two different identifiers for "AND"

Use && for a Logical "AND" (True && False = False)

Use & for a bitwise "AND" (00000011 & 00000001 = 00000001)

There is no ; at the end of the line with the if() statement on it. This will cause the if() statement to do nothing, because when if condition is met, it will execute the next block, or the next statement. ; is a statement, so it will execute the empty statement if the condition is met, and continue from the next line (ie, the result will be the same regardless of whether the condition is true). Same goes for else. (also, there's no "then" keyword in c)

if/else if/else statement should look like

if (condition1==1 && condition2==1) {
//these two lines will execute if both condition1 and condition2 are equal to 1. 
doThis();
doThisToo();
} else if (condition1==1 || condition2==1) { 
//this will execute if both are not 1, but one of them is 
doThisOtherThing();
} else {
//this will be done if neither of the above are met
doSomethingElse(); 
}

Always use the curly braces.

This is technically legal:

if (condition==1) doThis();

but it is poor practice because it's harder to read and recognize mistakes with.

Also, analogRead() returns an integer - there's no need to compare it to floating point numbers.

Also, you are comparing potPin, which is the pin number you're reading. I think you want to be comparing to val, the value you got back from analogRead.

Finally, you can only pass two arguments to digitalWrite() - one pin number, and HIGH or LOW. You can't write more than one pin at a time using digitalWrite() - that else at the end (once you fix everything else) will never be executed, because there is no case where one of the other conditions won't be true - and in any case in all the other cases you've finished by writing the pin LOW, so those pins could never be left HIGH.

if you use else if instead of separate if statements, you only need the one test for each one, since you know that the previous condition wasn't true.

Nickademus420:
My current error is: expected identifier before ‘(’ token.
on
and (potPin < 830.7);

The compiler is telling you, in its indirect way, that you’re missing an argument. A logical *and * statement takes two arguments, as in:

logical condition1 and logical condition 2
argument1 and (potPin < 830.7);.

Additionally, this needs to go to the right of an assignment ( = ). As in:

someBoolean = argument1 and (potPin < 830.7);

OP, here’s some code you can play with. It shows a few concepts, like if/else statements, arrays and bit-shifting, OR and ANDs and it should look pretty cool on your board. It’s got lots of comments to help understand what’s going on.

With a simple change you can turn on all the LEDs below the one turned on now…

//start of my code

//#define DEBUG 1   //uncomment to have val count up automatically

int val = 0;       // variable to store the value coming from the sensor
int mapval;
unsigned int mask;

//make your pin assignments "const" so the 
//compiler will catch if you try to modify them
//as you might with variables
const int potPin = A4;    //potentiometer input

//give the LEDs names for reader clarity
const int LED01 = 2;
const int LED02 = 3;
const int LED03 = 4;
const int LED04 = 5;
const int LED05 = 6;
const int LED06 = 7;
const int LED07 = 8;
const int LED08 = 9;
const int LED09 = 10;
const int LED10 = 11;
const int LED11 = 12;
const int LED12 = 13;
const int LED13 = A0;
const int LED14 = A1;
const int LED15 = A2;
const int LED16 = A3;

//put the LED names into an array. This makes
//it much cleaner to deal with large numbers
//of items like this (see setup() for an example)
const int grpinLED[] = 
{
    LED01, LED02, LED03, LED04,
    LED05, LED06, LED07, LED08,
    LED09, LED10, LED11, LED12,
    LED13, LED14, LED15, LED16
    
};

void setup() 
{
    //for debug messages
    Serial.begin(115200);

    //we use the array created above to set the
    //pinmodes for the LEDs. Using an array
    //and a for-loop saves many lines of code
    for( int i=0; i<16; i++ )
        pinMode( grpinLED[i], OUTPUT );  

}//setup

void loop() 
{
    //if you uncomment the "#define DEBUG 1" line above
    //val will count up each loop and form an "LED chaser"
    //with the line commented, val will be set from the
    //analog channel
#ifdef DEBUG
    val++;
    if( val > 1023 )
        val = 0;
#else        
    val = analogRead(potPin);    // read the value from the sensor    
#endif    
    //the analog read will come back with a number
    //between 0 and 1023 (that's 1024 steps produced
    //by a 10-bit A/D converter.)
    //Here the map() function is used to shrink that
    //to a range from 0-16. This works out nicely
    //as you have 16 LEDs.
    //for val == 0, mapval = 0
    //for val ==512, mapval = 8
    //for val == ...
    //for val == 1023, mapval = 16
    //
    mapval = map( val, 0, 1023, 0, 16 );

    //this variable is used for debug; see the msg printed
    //at the end of loop()
    mask = 0;
    
    //here we go through 0-15 (16 values)
    //think of each value of i as representing an
    //LED position. For values of i less than
    //mapval, we turn the corresponding LED on;
    //(basically, all LED positions below mapval
    //should be turned on)
    //the array of LEDs above comes in handy here too
    for( int i=0; i<16; i++ )
    {
        //if i is the same as mapval, turn on that LED 
        if( i == mapval )
        {
            //mask is built here for the serial msg
            //the '<<' means a bit is shifted left
            //in this case, a '1' is shifted left
            //a mapval number of times; in the debug
            //msgs below this will be the '1' that
            //corresponds to the LED being on
            mask |= (1<<i);
            //use i to index into the the array to 
            //turn on the LED
            digitalWrite( grpinLED[i], HIGH );
        }//if
        else
        {
            //i is not equal to the mapval value so 
            //turn off this LED
            digitalWrite( grpinLED[i], LOW );
        }//else
            
    }//for

    //turn on the serial monitor and set the baud
    //rate to 115200
    //this part is just to show how the above checks
    //work. The '1' in mask corresponds to the LED
    //that is on. All the '0's are LEDs that are off.
    Serial.print( "For analog value " ); 
    Serial.print(val); 
    Serial.print(" LED mask = " );
    for( int i=15; i>=0; i-- )
        Serial.print( (mask&(1<<i)?"1":"0" ) );
    Serial.print( " (" ); 
    Serial.print( mask, HEX ); 
    Serial.print(")"); 
    Serial.print( " Mask was " );
    Serial.println( mapval );

    //delays aren't great but they're simple
    delay(5);
    
}//loop

Thank you everyone for your help.

I tried the code that you provided Blackfin, and it compiled, but didn’t quite do what I was trying for.

Maybe it is jitter from wires so close or something, but here is what I get.

Upload code,
all lights go off.

7 and 8 go on, but i don’t touch potentiometer.
it starts sliding up a few lights on/off, in sequence,
then back down,
sits with 8 on.

I turn potentiometer,
7,8,9 blink on/off weirdly.

Lol.

So, it is way better than before but not quite there.

Any ideas?

So, I tinkered with my original code, and got it to compile!!!

BUT:

It only blinks light 8?

Potentiometer movement does nothing.

What did I do wrong?

int potPin = A4; // select the input pin for the potentiometer
int ledPin = 2; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor

void setup() {

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
}

void loop() {
val = analogRead(A4); // read the value from the sensor

if (val == 0){
digitalWrite(2, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(2, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val>0 && val<63.9){
digitalWrite(3, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(3, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val >63.9 && val<127.8){
digitalWrite(4, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(4, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val >127.8 && val<191.7){
digitalWrite(5, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(5, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val >191.7 && val<255.6){
digitalWrite(6, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(6, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val >255.6 && potPin <319.5){
digitalWrite(7, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(7, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val >319.5 && val<383.4){
digitalWrite(8, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(8, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val >383.4 && val<447.3){
digitalWrite(9, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(9, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val >447.3 && val<511.2){
digitalWrite(10, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(10, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val >511.2 && val<575.1){
digitalWrite(11, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(11, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val >575.1 && val<639){
digitalWrite(12, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(12, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val >639 && val<702.9){
digitalWrite(13, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(13, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val >702.9 && val<766.8){
digitalWrite(14, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(14, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val >766.8 && val<830.7){
digitalWrite(15, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(15, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val >830.7){
digitalWrite(16, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(16, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else;{
digitalWrite (1, LOW);
}
}

Can you try uncommenting the DEBUG line in my code and trying again, reporting what you see?

It sounds like you've either got a wiring problem or your analog input is floating. Are you sure your potentiometer is connected to analog input A4? Is it connected properly to VCC, GND etc?

Are you sure the LEDs are all wired the correct way (anode, cathode etc...)?

Ok, this time I also declared pin A4 as output. now potentiometer yields some results, but not as expected either.
Here’s my updated code.

int potPin = A4; // select the input pin for the potentiometer
int ledPin = 2; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor

void setup() {

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(A0, OUTPUT);
pinMode(A1, OUTPUT);
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
pinMode(A4, INPUT);
}

void loop() {
val = analogRead(A4); // read the value from the sensor

if (val == 0){
digitalWrite(2, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(2, LOW); // turn the ledPin off
//delay(1000); // stop the program for some time
}
else if (val>0 && val<64){ //63.9
digitalWrite(3, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(3, LOW); // turn the ledPin off
//delay(1000); // stop the program for some time
}
else if (val >64 && val<128){//127.8
digitalWrite(4, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(4, LOW); // turn the ledPin off
//delay(1000); // stop the program for some time
}
else if (val >128 && val<192){//191.7
digitalWrite(5, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(5, LOW); // turn the ledPin off
//delay(1000); // stop the program for some time
}
else if (val >192 && val<256){//255.6
digitalWrite(6, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(6, LOW); // turn the ledPin off
//delay(1000); // stop the program for some time
}
else if (val >256 && val<320){//319.5
digitalWrite(7, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(7, LOW); // turn the ledPin off
//delay(1000); // stop the program for some time
}
else if (val >320 && val<384){//383.4
digitalWrite(8, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(8, LOW); // turn the ledPin off
//delay(1000); // stop the program for some time
}
else if (val >384 && val<448){//447.3
digitalWrite(9, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(9, LOW); // turn the ledPin off
//delay(1000); // stop the program for some time
}
else if (val >448 && val<512){//511.2
digitalWrite(10, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(10, LOW); // turn the ledPin off
//delay(1000); // stop the program for some time
}
else if (val >5112 && val<575){//575.1
digitalWrite(11, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(11, LOW); // turn the ledPin off
//delay(1000); // stop the program for some time
}
else if (val >575 && val<639){
digitalWrite(12, HIGH); // turn the ledPin on
//delay(1000); // stop the program for some time
//digitalWrite(12, LOW); // turn the ledPin off
delay(1000); // stop the program for some time
}
else if (val >639 && val<702){//702.9
digitalWrite(13, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(13, LOW); // turn the ledPin off
//delay(1000); // stop the program for some time
}
else if (val >702 && val<767){//766.8
digitalWrite(14, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(14, LOW); // turn the ledPin off
//delay(1000); // stop the program for some time
}
else if (val >767 && val<830){//830.7
digitalWrite(15, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(15, LOW); // turn the ledPin off
//delay(1000); // stop the program for some time
}
else if (val >830.7){ //Up to 1023
digitalWrite(16, HIGH); // turn the ledPin on
delay(1000); // stop the program for some time
digitalWrite(16, LOW); // turn the ledPin off
//delay(1000); // stop the program for some time
}
else;{
digitalWrite (1, LOW);
}
}

Oops, I noticed in my code that I didn’t put in an initialization for the analog pin in setup:

Try replacing the setup() function with this one:

void setup() 
{
    //for debug messages
    Serial.begin(115200);

    pinMode( pinPot, INPUT );

    //we use the array created above to set the
    //pinmodes for the LEDs. Using an array
    //and a for-loop saves many lines of code
    for( int i=0; i<16; i++ )
        pinMode( grpinLED[i], OUTPUT );  

}//setup

All pins are INPUT upon startup, so that doesn't explain it.

Symptoms sound like potPin is floating.

It should be connected to the middle pin of the potentiometer. Of the other two pins on the potentiometer, one should be connected to 5v, the other to Gnd.

Also - it is a potentiometer, not a rotary encoder, right? A potentiometer will generally have a stop at either end, ie, you can only turn it so far in either direction before it stops, while a rotary encoder will turn endlessly in either direction. (if you post a pic of it, we can tell you which it is)

When dealing with a bunch of pins all treated roughly the same it is good to use an array and loops:

const byte PotPin = A4;    // select the input pin for the potentiometer

const byte LEDPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2, A3};
const byte LEDCount = sizeof LEDPins / sizeof LEDPins[0];

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

void loop()
{
  int val = analogRead(PotPin);    // read the value from the sensor

  int index = (val * LEDCount) / 1023;  // Map to 0..LEDCount range
  
  for (int i = 0; i < LEDCount; i++)
  {
    digitalWrite(LEDPins[i], i == index);  // index HIGH, all others LOW
  }
}


Nickademus420
Please read http://forum.arduino.cc/index.php/topic,148850.0.html , in particular item 7. Using code tags makes it easier to read your code and makes it more likely someone will help you.

Thanks everyone for the comments and help. I am a beginner and it is much appreciated.
Blackfin, your code is perfect.
I had to solder wires to my potentiometer and then it worked fine, so it was my connections, not the code.
I am going to sit down for a while and focus on understanding the coding language so that I can make things better, faster, and simpler.
I need to start with arrays and loops.

PS: HERE is blackfin’s final code:

//start of my code

//#define DEBUG 1   //uncomment to have val count up automatically

int val = 0;       // variable to store the value coming from the sensor
int mapval;
unsigned int mask;


//make your pin assignments "const" so the 
//compiler will catch if you try to modify them
//as you might with variables
const int potPin = A4;    //potentiometer input

//give the LEDs names for reader clarity
const int LED01 = 13;
const int LED02 = 12;
const int LED03 = 11;
const int LED04 = 10;
const int LED05 = 9;
const int LED06 = 8;
const int LED07 = 7;
const int LED08 = 6;
const int LED09 = 5;
const int LED10 = 4;
const int LED11 = 3;
const int LED12 = 2;
const int LED13 = A0;
const int LED14 = A1;
const int LED15 = A2;
const int LED16 = A3;

//put the LED names into an array. This makes
//it much cleaner to deal with large numbers
//of items like this (see setup() for an example)
const int grpinLED[] = 
{
   LED01, LED02, LED03, LED04,
   LED05, LED06, LED07, LED08,
   LED09, LED10, LED11, LED12,
   LED13, LED14, LED15, LED16
   
};

void setup() 
{
   //for debug messages
   Serial.begin(115200);

   pinMode( potPin, INPUT );

   //we use the array created above to set the
   //pinmodes for the LEDs. Using an array
   //and a for-loop saves many lines of code
   for( int i=0; i<16; i++ )
       pinMode( grpinLED[i], OUTPUT );  

}//setup

void loop() 
{
   //if you uncomment the "#define DEBUG 1" line above
   //val will count up each loop and form an "LED chaser"
   //with the line commented, val will be set from the
   //analog channel
#ifdef DEBUG
   val++;
   if( val > 1023 )
       val = 0;
#else        
   val = analogRead(potPin);    // read the value from the sensor    
#endif    
   //the analog read will come back with a number
   //between 0 and 1023 (that's 1024 steps produced
   //by a 10-bit A/D converter.)
   //Here the map() function is used to shrink that
   //to a range from 0-16. This works out nicely
   //as you have 16 LEDs.
   //for val == 0, mapval = 0
   //for val ==512, mapval = 8
   //for val == ...
   //for val == 1023, mapval = 16
   //
   mapval = map( val, 0, 1023, 0, 16 );

   //this variable is used for debug; see the msg printed
   //at the end of loop()
   mask = 0;
   
   //here we go through 0-15 (16 values)
   //think of each value of i as representing an
   //LED position. For values of i less than
   //mapval, we turn the corresponding LED on;
   //(basically, all LED positions below mapval
   //should be turned on)
   //the array of LEDs above comes in handy here too
   for( int i=0; i<16; i++ )
   {
       //if i is the same as mapval, turn on that LED 
       if( i == mapval )
       {
           //mask is built here for the serial msg
           //the '<<' means a bit is shifted left
           //in this case, a '1' is shifted left
           //a mapval number of times; in the debug
           //msgs below this will be the '1' that
           //corresponds to the LED being on
           mask |= (1<<i);
           //use i to index into the the array to 
           //turn on the LED
           digitalWrite( grpinLED[i], HIGH );
       }//if
       else
       {
           //i is not equal to the mapval value so 
           //turn off this LED
           digitalWrite( grpinLED[i], LOW );
       }//else
           
   }//for

   //turn on the serial monitor and set the baud
   //rate to 115200
   //this part is just to show how the above checks
   //work. The '1' in mask corresponds to the LED
   //that is on. All the '0's are LEDs that are off.
   Serial.print( "For analog value " ); 
   Serial.print(val); 
   Serial.print(" LED mask = " );
   for( int i=15; i>=0; i-- )
       Serial.print( (mask&(1<<i)?"1":"0" ) );
   Serial.print( " (" ); 
   Serial.print( mask, HEX ); 
   Serial.print(")"); 
   Serial.print( " Mask was " );
   Serial.println( mapval );

   //delays aren't great but they're simple
   delay(5);
   
}//loop

It was working fine since I last posted, and now today when I turned it on, the 1st led (pin 13) blinks when the potentiometer is at 0 (i guess). It used to just have all LEDS off, but now it blinks. What is going on?

Update

Found out it is the USB Port on this HP laptop that was making the light blink when it wasn't supposed to.
I connected the 9v wall adapter and it turned off, and works fine again.
SO weird.