simple array high/low question

just starting out programming arduino. I would like to simplify this code by having an array indexes plugged into the output state of the pins. When pot is turned i have led bar with one led lit at a time depending on resistance on analog input 1. Any suggestions?

code:

int potval = 0; int potpin = 0; int outputval = 0; int ledp1 = 2; int ledp2 = 3; int ledp3 = 4; int ledp4 = 5; int ledp5 = 6; void setup() { Serial.begin(9600); pinMode(ledp1, OUTPUT); pinMode(ledp2, OUTPUT); pinMode(ledp3, OUTPUT); pinMode(ledp4, OUTPUT); pinMode(ledp5, OUTPUT); } void loop() { potval = analogRead(potpin); outputval = map(potval, 0, 1000, 0, 5); if (outputval == 1){ digitalWrite(ledp1, 1 ); digitalWrite(ledp2, 0 ); digitalWrite(ledp3, 0 ); digitalWrite(ledp4, 0 ); digitalWrite(ledp5, 0 ); } if (outputval == 2){ digitalWrite(ledp1, 0 ); digitalWrite(ledp2, 1 ); digitalWrite(ledp3, 0 ); digitalWrite(ledp4, 0 ); digitalWrite(ledp5, 0 ); } if (outputval == 3){ digitalWrite(ledp1, 0 ); digitalWrite(ledp2, 0 ); digitalWrite(ledp3, 1 ); digitalWrite(ledp4, 0 ); digitalWrite(ledp5, 0 ); } if (outputval == 4){ digitalWrite(ledp1, 0 ); digitalWrite(ledp2, 0 ); digitalWrite(ledp3, 0 ); digitalWrite(ledp4, 1 ); digitalWrite(ledp5, 0 ); } if (outputval == 5){ digitalWrite(ledp1, 0 ); digitalWrite(ledp2, 0 ); digitalWrite(ledp3, 0 ); digitalWrite(ledp4, 0 ); digitalWrite(ledp5, 1 ); } if (outputval == 0){ digitalWrite(ledp1, 0 ); digitalWrite(ledp2, 0 ); digitalWrite(ledp3, 0 ); digitalWrite(ledp4, 0 ); digitalWrite(ledp5, 0 ); } }

void loop() {
potval = analogRead(potpin);
outputval = map(potval, 0, 1023, 0, 5);  // why not 1023 here? Something external keeping it from full range??
digitalWrite (ledp1, 0);
digitalWrite (ledp2, 0);
digitalWrite (ledp3, 0);
digitalWrite (ledp4, 0);
digitalWrite (ledp5, 0);

switch (outputval){
case 1:
digitalWrite (ledp1, 0);
delay(25); so can see the LED is on
break;
case 2:
digitalWrite (ledp2, 0);
delay(25); so can see the LED is on
break;
case 3:
digitalWrite (ledp3, 0);
delay(25); so can see the LED is on
break;
case 4:
digitalWrite (ledp4, 0);
delay(25); so can see the LED is on
break;
case 5:
digitalWrite (ledp5, 0);
delay(25); so can see the LED is on
break;
}
}

Or maybe

void loop() {
    potval = analogRead(potpin);
    outputval = map(potval, 0, 1000, 0, 5);
    digitalWrite(ledp1, 0);
    digitalWrite(ledp2, 0);
    digitalWrite(ledp3, 0);
    digitalWrite(ledp4, 0);
    digitalWrite(ledp5, 0);

    digitalWrite(outputval+1, 1);

      delay (25);
}

Rob

maybe add 1 or 2 to that since:

int ledp1 = 2; int ledp2 = 3; int ledp3 = 4; int ledp4 = 5; int ledp5 = 6;

??

Yep, didn't spot that. Code modified.


Rob