Why is the last LED so dim?

I am a math teacher trying to get acquainted with the Arduino Uno and trying to learn a few basic things to teach my students. I am trying to run the sketch below, which is working, however, the first LED light is bright, the second one a bit dimmer, and the third one dimmer still. The brightness cycle repeats each time the loop runs, bright, dimmer, dimmer. I have each one grounded with a 220 resistor to the common column on the breadboard that then connects to the ground on the Arduino. Can anyone explain to me in really simple terms why the lights are successively dimmer and how to fix the code or the set up.

/* Button-Turns on and off three LED's connected to digital pins 10, 9, and 8
when pressing a pushbutton attached to pin 7.
*/
const int buttonPin = 7; // constant value - the number of the pushbutton pin connected to 7
int ledPin = 10; // variable value - the number of the LED pin starts with 10
int buttonState = 0; // variable value - for reading the pushbutton status

void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
if (ledPin >8){
digitalWrite(ledPin, HIGH); // If button is pushed, buttonState reads HIGH, turns starts sequence.
delay(80);
digitalWrite(ledPin, LOW);
delay(80);
ledPin--;
digitalWrite(ledPin, HIGH);
delay(80);
digitalWrite(ledPin, LOW);
delay(80);
ledPin--;
digitalWrite(ledPin, HIGH);
delay(80);
digitalWrite(ledPin, LOW);
delay(80);
ledPin--;
} else {
ledPin = 10;
}
} else {
digitalWrite(ledPin, LOW); // If button is not pushed, buttonState reads LOW, turns LED off.
}
}

(I don't know why that happy face appeared in the code, it is an eight)

Hi, and welcome to the Arduino Forum.

Right off the bat, one issue is you are effectively setting the pinMode on only pin 10 (the number used to initialize the ledPin variable).

What effectively needs to happen is as follows:

void setup {
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
}

I leave it as an exercise for you to alter your code to make that happen [how long have I waited to get my revenge on a teacher!? :smiley: ]

Thank you!!! That worked perfectly! And I learned something thanks to you!

One other note: I would create pin definitions for each pin and then put them in an array. Then, iterate through the array to manipulate each LED. But, it’s late, and I must off to slumberland.

I leave you in the capable, though delightfully gruff hands of Grumpy_Mike.

ZZZZZZZZZZZ…

That pins 9 and 8 were still defaulted to inputs explains why they were both dimmer than pin 10, but not why 8 was dimmer than 9.

the first LED light is bright, the second one a bit dimmer, and the third one dimmer still

I got the following PM in response to this.

426hemi:
That pins 9 and 8 were still defaulted to inputs explains why they were both dimmer than pin 10, but not why 8 was dimmer than 9.

Paul__B:
This is the internal pull-up function which is activated when the port direction register is set as INPUT but the output register is set HIGH. (There are three registers, direction, output and input though the last is not actually a register but a read gate.) It is approximately a 40k resistor, but only approximately; there is no guarantee of its value so it can readily vary from one pin to another. So which one was dimmer than which was itself, a “toss of the coin”.