Issue with an LED lighting project!!!

People,
I am Som, a student studying in grade 10.
I just learn't to code on arduino, and just wrote a simple code for a small project consisting of LED's.
Its basically some LED lighting pattern.
This is the code:


int ledPin=1;
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);
}

void loop() {
for(ledPin=2;ledPin<=10;ledPin++){
     analogWrite(ledPin,HIGH);
     delay(100);
     if(ledPin==10){
       ledPin=2;
     }
     
     }
}

The problem is that the LED's connected to pin 2,4,7,8,10 don't light up.
Also that the loop doesn't run itself again.
Please help me with this petty thing.
P.S.-I'm a novice.

Hello Som.

Please edit your post and put code tags around your sketch so it looks like

this

Better still, use Tools->Auto Format in the IDE and post the result.

You MUST put a series resistor for each led. If you do not, both the Arduino and the leds could be damaged. 220R or similar is ok.

Also try using digitalWrite() instead of analogWrite().

The analogWrite() function should be given a number 0..255, not HIGH/LOW. It also works only on certain pins.

Paul

Does the analogWrite function work only on the pwm pins? If yes, I guess I'll have to write a setup for each of the pins....
BTW thanks Paul.

analogWrite works on any output pin, but on non-PWM pins it only turns on the pin if the value written is something like 128 or above.

Hi SomTambe

Check your board. I see you have ground wire to one side of your strip and the LEDs to the other side. Some boards have a split in the middle of the board. In other words your LEDs might not be grounded.

SomTambe:
the LED's connected to pin 2,4,7,8,10 don't light up.

Those are the pins that do not have PWM. Because HIGH has the value 1 and that is less than 128, the analogWrite() function will not be able to light the led. That is what AWOL explained.

However, pin 10 does have PWM. Are you sure there is not a connection problem with that led? Maybe check the breadboard connections like Naneen suggests.

SomTambe:
Also that the loop doesn't run itself again.

I don't know why that is happening. It should run over and over. Try removing these lines:

     if(ledPin==10){
       ledPin=2;
     }