Led's won't light up

hey guys
i'm pretty sure the problem is in the code ... no matter how simple it is I always find a way to make it not work with the setup....
here it is

int sensorPin = 1;
int ledPins[] = {0, 1, 2, 3, 4, 5, 6};
int val = 0;

void setup() {
pinMode(ledPins[0], OUTPUT);
pinMode(ledPins[1], OUTPUT);
pinMode(ledPins[2], OUTPUT);
pinMode(ledPins[3], OUTPUT);
pinMode(ledPins[4], OUTPUT);
pinMode(ledPins[5], OUTPUT);
pinMode(ledPins[6], OUTPUT);
Serial.begin(9600);
}

void loop()
{

val = analogRead(sensorPin);
val = map(val, 0, 1023, 0, 1200);
{ while (val>=0 && val <=200 )
digitalWrite(ledPins[0,1], HIGH);}

{while (val>=201 && val <=400 )
digitalWrite(ledPins[2,3], HIGH); }

{while (val>=401 && val <=600 )
digitalWrite(ledPins[4,5,6], HIGH);}
Serial.println(val);
}

overview of the led's on their coresponing pins-
0,1 - blue ;
2,3 - red;
4,5,6 - green; (had a extra green one lol)

so the basic idea is to read the Pot pin - analog0, then map it out for 1200 value (from 1024). Then according to it , on intervals of 200, light up the different color led's and turn the other clrs off, that leaves 3 more intervals of 200 each for mixing colors :slight_smile: or 6 ints if they are 100 each.
why aren't any of them lighting up? NONE of them turn on and the serial read on the pot pin reads >nothing<, and i'm pretty sure it's connected properly. it might be the "while" functions ... i thought of using "if"-s but ... idk, it felt weird to use it.
any suggestions ... or overall help would be awesome :wink:

A bunch of things:

First your loop will execute many thousand times each second so you probaby need to put a delay in there somewhere otherwise you will be sending out many thousand bytes over the serial at 9600.

Next where you are using While statements you really shoul be using if statements.

And you also need to switch the two groups of LED's not needed for a given reading of, otherwise all the LEd 's will always be on if the have been turned on just once.

How did you connect everthing ?

If the serial print of VAL shows nothing then something must be wrong.

One problem is that some Arduinos, including the Duemilanove, use digital I/O pins 0 and 1 for serial I/O. On the Duemilanove, they are connected to the USB interface chip.

I suggest you pick different pins for those two LEDs.

Your program is probably hanging in one of those 'while' statements that MikMo pointed out should be 'if' statements.

One way to troubleshoot a problem like this is to start with a program that turns all the LEDs on, delays for a second ( delay(1000); ), then turns all of the LEDs off. That way, you can check your connections.

Regards,

-Mike

thanks for the replies guys, i'll rewrite it with if's. uhm.. one more thing though. i suppose it'll look something like "if val >=something and <= something, turn leds 1,2,3 high; else turn leds 1,2,3 low". uhm.. can I actually refer to them as "led 1,2,3" or do i have to type them out one by one like
led 1;
led 2;
led 3;
cuz... i cant think up of a syntax for 3 if's with the same condition, and that's like 6 lines of code for each color (not that length is important in any way but why should it be long if the same can be done in 2 lines?).
and anyway, having the multiple if's with the same condition usually causes "misfires" (at least for me), so I try to keep them as few as I can.
and about the delay for the serial - i would have added it as soon as it had ran for the first time (i hope i dont sound too cocky), i just don't like numbers spilling out and I would have added at least a 150ms one :wink: also thanks for the heads up on pins 0 and 1 Mike, i'll definately look out for them from now on.

Anything that is repetitious, like:

digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);

is a candidate for creating a function:

void setLeds(int state)
{
   digitalWrite(led1, state);
   digitalWrite(led2, state);
   digitalWrite(led3, state);
}

Then, you can turn all the LEDs on:

setLeds(HIGH);

and off:

setLeds(LOW);

Rather than write the same code over and over, look for ways to do it in a function.

The Arduino does provide a way to turn several pins on, or off, at once. The process is documented in Arduino Reference - Arduino Reference, but you are not ready to do that, yet.

digitalWrite(ledPins[4,5,6], HIGH);

This will not work to turn on multiple LEDs. You can't have multiple numbers in the array index. The compilers probably won't complain because it thinks you are using the "," operator with three constant expressions. The "," operator returns the value of only the last expression, in this case "6". Also, the digitalWrite function only accepts two parameters: the single pin to write to, and the HIGH/LOW value to write. So you need a separate digitalWrite call for each LED.