Why does LED3 not fire up?


i have a problem with my code. yeah, i know, i could have used an array instead and what not. but i'm new to coding and my language skills are not strong :slight_smile:
trying to make a button, with which i can cicly through different outputs. at the end there should be a relay on each output, to switch different lights (230V / europe) in the house.

so, my problem is this: i have a counter which works fine. it counts from 1 to 3. the leds (relays) fire up when the mode is in the right state - all but the LED3!
i just cannot see the error...
the other two are working and when the counter is back on 1 again, it works again. its just the third LED, which is not lighing up (the counter goes on 3 dough)

what could be the problem?

int mode = 0;

int buttonPin = 2;

int ledPin_1 = 5;
int ledPin_2 = 6;
int ledPin_3 = 9;

void setup() {
  // put your setup code here, to run once:

  pinMode (buttonPin, INPUT);

  pinMode (ledPin_1, OUTPUT);
  pinMode (ledPin_2, OUTPUT);
  pinMode (ledPin_3, OUTPUT);


void loop() {
  // put your main code here, to run repeatedly:
  while (digitalRead(buttonPin) == LOW) {

    if (mode >= 3){
      (mode = 0);

if (mode == 1){
  digitalWrite(ledPin_2, LOW);
  digitalWrite(ledPin_3, LOW);
  digitalWrite(ledPin_1, HIGH);

if (mode == 2){
  digitalWrite(ledPin_1, LOW);
  digitalWrite(ledPin_3, LOW);
  digitalWrite(ledPin_2, HIGH);

if (mode == 3){
  digitalWrite(ledPin_1, LOW);
  digitalWrite(ledPin_2, LOW);
  digitalWrite(ledPin_3, HIGH);

Have you swapped LEDs around to check if you have a dud LED or one the wrong way around.
Checked the arduino output with a DMM?

Tom... :slight_smile:

Your mode variable cycles through 0, 1, 2 then back to 0.

Your if statements are checking for modes 1, 2 or 3.


if (mode >= 3)

This says if mode is greater than or EQUAL to 3, then mode = 0.
So you make mode =3 then immediately make it 0.

if (mode > 3)

Will let mode =3 for the cycle of the loop.

Tom... :slight_smile:

@tom, yeah i have swapped the leds and assigned to new pins. but i didn't check if them with a DMM.

@gypsum, thank you for this input. didn't thought about that. i assigned the statements no to 0,1,2. it works now

however, it has a funny behave to it.

  • The Mode is 0 if i start the arduino.
  • then when i click the button its: 1,2,3 - and back to one (because "if (mode >= 3){
    (mode = 0);")

when i start up the arduino, LED1 is ON, because mode = 0.
after that first cycle, LED1 is on at Mode 3!

i can't wrap that around my head. can you explain me the logic behind it?
is the arduino like: "ah, it is not mode2 or 1, so everything else is equal to 0!"

or how does this work?

alright tom, thank you! i think i get it now... slowely:)
its just hard to understand because the serial monitor prints 1,2,3 all the time, although the mode is 0 sometimes..!

next step, implement a RTC, phuu...:slight_smile:

i have a following question.
when i press the button, it cycles thru all modes (with the delay of 500ms)
how can i make it just an impulse?
so that it only steps 1 mode up, even if the button stays pressed.


how can i make it just an impulse?

Look at the StateChangeDetection example in the IDE

Look at the StateChangeDetection example in the IDE

thank you for the fast answer!