How to make this work ?

Hello,

I want to make it work that the leds are going round.
But on some way led7 is set to on but there is no led7.

Can anyone help me figure where my thinking mistake is.

Project so far : Wokwi - Online ESP32, STM32, Arduino Simulator

You misunderstand array index and array content.

Here you compare currentLed with the last item in the array (5)

if (currentLed == ledPins[sizeof(ledPins) - 1])

And here you use currentLed as an array index :

ledPins[currentLed + 1]

Maybe you want to do this instead :

if (currentLed == sizeof(ledPins) - 1)

why not explain it?

Still not quit what I want

See the output :

Aan: 6

Uit: 7

Aan: 5

Uit: 5

Aan: 4

I still see led 7 used and on one place led 5 is put on and just after thet off.
That is not what I had in mind

What do you mean with "explain it" ??

what are you trying to do?

i changed the formating so that there is one complete line per loop iteration and get the following.

shouldn't current LED be in the range of 0-5?

Start
Aan: 0 Uit: 1 Overflow
Aan: 6 Uit: 7
Aan: 5 Uit: 9
Aan: 4 Uit: 5
Aan: 3 Uit: 4
Aan: 2 Uit: 3
Aan: 1 Uit: 2
Aan: 0 Uit: 1 Overflow
Aan: 6 Uit: 7
Aan: 5 Uit: 9
Aan: 4 Uit: 5
Aan: 3 Uit: 4
Aan: 2 Uit: 3
Aan: 1 Uit: 2
Aan: 0 Uit: 1 Overflow
Start
Aan: 1 Uit: 11
Aan: 2 Uit: 10
Aan: 3 Uit: 6
Aan: 4 Uit: 3
Aan: 5 Uit: 5
Aan: 0 Uit: 9
Aan: 1 Uit: 11
Aan: 2 Uit: 10
Aan: 3 Uit: 6
Aan: 4 Uit: 3
Aan: 5 Uit: 5
Aan: 0 Uit: 9
Aan: 1 Uit: 11
Aan: 2 Uit: 10
Aan: 3 Uit: 6
Aan: 4 Uit: 3
Aan: 5 Uit: 5
Aan: 0 Uit: 9
Aan: 1 Uit: 11
Aan: 2 Uit: 10
Aan: 3 Uit: 6
const byte Potpin    = A3;
const byte PinLeds[] = { 9, 11, 10, 6, 3, 5 };
const int  Nled      = sizeof(PinLeds);

enum { Off = HIGH, On = LOW };

byte currentLed = 0;

// -----------------------------------------------------------------------------
void setup()
{
    Serial.begin(115200);
    Serial.println(F("Start"));
    for (uint8_t cnt = 0; cnt < Nled; cnt++) {
        digitalWrite (PinLeds[cnt], Off);
        pinMode      (PinLeds[cnt], OUTPUT);
    }
}


void loop()
{
    if (512 <  analogRead(Potpin)) {
        digitalWrite (PinLeds[currentLed], Off);

        if (Nled <= ++currentLed)
            currentLed = 0;
        digitalWrite (PinLeds[currentLed], On);

        Serial.print   (F("Aan: "));
        Serial.print   (currentLed);
        Serial.print   (F(" Uit: "));
        Serial.println (PinLeds[currentLed]);
    }
    delay(500);
}

yep, the range is good.
There are 6 leds so the range should be between 0 and 5

This code does exactly what I want.
And is a lot shorter.

but im confused why it also works well when the loop should work again so some study to do.

but it gives a wierd output:

$

Start

Aan: 1 Uit: 11

Aan: 2 Uit: 10

Aan: 3 Uit: 6

Aan: 4 Uit: 3

Aan: 5 Uit: 5

Aan: 0 Uit: 9

Aan: 1 Uit: 11

Aan: 2 Uit: 10

Aan: 3 Uit: 6

Aan: 4 Uit: 3

Aan: 5 Uit: 5

Aan: 0 Uit: 9

Aan: 1 Uit: 11

Aan: 2 Uit: 10

A lot of numbers above the 5 so what I said im confused why it still works.

i don't know wat Aan or Uit mean. i got the impression from your code that they were currentLed and the corresponding led Pin #

Aan is Dutch for On so which led is on
UIt is Dutch for Off so which led schould be turned off.

Can my code for turning it the other way around be also so short :frowning:

if (Potvalue < 512)
  {
    ////////////////////////
    // aansturen LEDs
    ////////////////////////
    Serial.print(F("Aan: "));
    Serial.println(currentLed);
    digitalWrite(ledPins[currentLed], LOW);
    Serial.print(F("Uit: "));
    if (currentLed == 0)
    {
      Serial.println(sizeof(ledPins) - 1);
      digitalWrite(ledPins[sizeof(ledPins) - 1], HIGH);
    }
    else
    {
      Serial.println(currentLed - 1);
        digitalWrite(ledPins[currentLed - 1], HIGH);
    }

    ////////////////////////
    // aanpassen currentLed
    ////////////////////////
    currentLed++;
    if (currentLed == sizeof(ledPins))
    {
      Serial.println(F("Overflow"));
      currentLed = 0;
    }
  }
  delay(500);
}

yes. understand this

As far as I Can see it means

if the numbers of leds is smaller or equal to currentLed updated with 1 then currentLed = 0

So for the other way around I can do

if (0 <= --currentLed)
            currentLed = Nled;

?

  • do you want to reset it to Nled?
  • do you want to reset when currentLed is positive?

Moment , I have to think now.
Your code works the oppossite of what i want.

The new code must work like this :

ON OFF
1 2
6 1
5 6
4 5
3 4
2 3

Nope, if I have it right It needs to switch when its bigger then nled
and it must then reset to the first pin

is this correct?
just worry about current Led , not the pin

I think so
There are 6 led's in the array.

what is the value of currentLed for the first element in the array?

Hello roelofw

Post the current sketch in code tags to see how we can help.