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.