Need a little guidance... Strange behavior.

New to the Arduino and to programming in general so im just playing around. The goal is to use a potentiometer to control "movement" (sweep side to side) across 6 LEDs.

Here is the code I have so far:

int potPin = 3; 
int potVal = 0; 
int pin[] = {
  3,5,6,9,10,11};
int pinCount = 6;
int pinVal[6];
int fadeAmount = 15;
int timer = 5;
int currPin;

void setup()
{
  for (int x = 0; x > pinCount; x++){
    pinMode(pin[x], OUTPUT); //set pins to OUTPUT
  }
  for (int x = 0; x > pinCount; x++){
    pinVal[x] = 0; //prefill the pinVal array with 0s
  }
  Serial.begin(9600);
}

void loop()
{
  potVal = analogRead(potPin); //read the value of the potetiometer
  currPin = map(potVal, 0, 1023, 0, pinCount); //convert the pot value to 0 through the number of pins

  for (int x = 0; x <= pinCount; x++){
    if (x == currPin){ // if the pot value matches, fade that pin ON
      pinVal[x] = pinVal[x] + (fadeAmount * 2); //fade the pin ON quickly
      if (pinVal[x] >= 254){
        pinVal[x] = 254;
      }
      analogWrite(pin[x], pinVal[x]);
    }   
    else{ // if the pot value does not match, fade OFF
      pinVal[x] = pinVal[x] - fadeAmount; //fades the pin OFF
      if (pinVal[x] <= 0){
        pinVal[x] = 0;
      }
      analogWrite(pin[x], pinVal[x]);
    }
    delay(timer);
  }
}

This code works, kinda. The pot lights up the LEDs incrementally according to position as expected. The LEDs fade in quickly when "active," as expected. The LEDs fade out when not "active," as expected.

The problem is, pin 2 (pin 6 on the board) flashes at the rate of the set delay delay(timer) and for the life of me, I cannot figure out why. It still fades in and out properly but it "blinks" while doing it. The other 5 LEDs do not behave this way.

There has to be something programmatically that I am missing here. Advice/correction is welcomed.

for (int x = 0; x > pinCount; x++){

That needs some attention

AWOL:

for (int x = 0; x > pinCount; x++){

That needs some attention

sigh literal hours spent...

I don't think that's the problem, because
a) The pinMode for analogWrite isn't necessary
b) The zeroing of the array isn't necesssary

Okay, then the question remains. Why do pins (in the code) pin[0],[1],[3],[4],[5] all work as intended but pin[2] flashes to the reate of the delay?

for (int x = 0; x <= pinCount; x++){

How many elements in pinVal?

There are 6. pinVal[] is the array to store the fade values for each associated pin[]

There are six, because pinCount is 6.
But what about the seventh?

It is interesting that you ask that because I knew you would. Originally, the code read:

for (int x = 0; x < pinCount; x++){[code]

When executing, however, the last pin would not initiate.  Only after I added the "<=" would all 6 respond.

[/code]

I think we've established that for loops are at least part of the problem, so why don't you pick all of them and your array bounds apart,and see how it goes?

salute Shall do. I will report back later with my findings.

Just to update, I have updated the code to include the corrected loop for OUTPUT assignment and fixed the main 'for' loop.

The issue has been resolved :stuck_out_tongue:

Thank you AWOL for your assistance and persistence.

And thank you for yours.

Just remember arrays are 0 based, UBound = 5 is actually 6 elements. The pin assignments are 1 based, meaning they start at 1 and go up.

I just noticed the RX pin is labeled as 0....