[SOLVED] Button operated continuous up/down counter glitch

Hej,

a NeoPixel ring with 24 LEDs is illuminated with a fading trail clockwise and a button press makes it reverse direction any time it is pressed.

This works, but there’s a glitch - when I serial print ledPos, I get 1-24 going forward, but 23-0 going backward, so the 1st LED never lights up when going backward. What am I doing wrong?

Thanks!

#include "FastLED.h"
#define NUM_LEDS 24
#define DATA_PIN 4
const byte pinSensor = A0;
const byte pinPotentiometer = A1;
const byte pinSwitch = A2; // Pin to which the momentary switch is connected

byte lastSwitchState = HIGH; // Switch is open at start

int ambientLightLevel, lightLevelMax = 1023, lightLevelMin = 0;

byte ledPos = 0; // Position in LED array at start
int counter = 1; // Increment at start

struct CRGB leds[NUM_LEDS];

void setup() {

  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
  pinMode (pinSwitch, INPUT_PULLUP);

}

void loop() {

  int changeHue = map(analogRead(pinPotentiometer), 0, 1024, 0, 256);
  ambientLightLevel = analogRead(pinSensor);
  autoDim();

  byte switchState = digitalRead (pinSwitch); // Switch is hardware debounced

  if (switchState != lastSwitchState)
  {
    lastSwitchState =  switchState;
    if (switchState == LOW)
    {
      counter = -counter;
    }
  }

  EVERY_N_MILLISECONDS(20)
  {
    fadeToBlackBy(leds, NUM_LEDS, 128);
    leds[ledPos] = CHSV(changeHue, 255, 255);

    ledPos = ledPos + counter;
    Serial.println(ledPos);

    if (ledPos == NUM_LEDS)
    {
      ledPos = 0;
    }
    else if (ledPos == 0)
    {
      ledPos = NUM_LEDS;
    }

    FastLED.show();

  }

}

void autoDim()
{
  FastLED.setBrightness(128); // Later ambient light level calculated here
}
    if (ledPos == NUM_LEDS)
    {
      ledPos = 0;
    }
    else if (ledPos == 0)
    {
      ledPos = NUM_LEDS;
    }

Right there

Hej AWOL,

thanks! Changing

    else if (ledPos == 255)
    {
      ledPos = NUM_LEDS;
    }

works (with ledPos being a byte variable).

It would seem to me that if you have 24 LEDs, they are probably referenced as 0 to 23, so ledPos would need to be assigned a value of NUM_LEDS-1.

Thanks PaulS,

yes, FastLED references them in the array with 0-23. NUM_LEDS - 1 indeed also works. Maybe my eyes are too slow to spot the difference between not subtracting 1 and subtracting 1.

Serial printing ledPos now goes from 1, 2, ... 23, 24 (forward) and 255, 22, ... 1, 0 (backward).

Serial printing ledPos now goes from 1, 2, ... 23, 24 (forward) and 255, 22, ... 1, 0 (backward).

So, it is right going forward, and wrong going backward, and you can't see why but are happy with the inconsistency. Well, OK.

Well

    else if (ledPos == 255)
    {
      ledPos = NUM_LEDS;
    }

and

    else if (ledPos == 255)
    {
      ledPos = NUM_LEDS - 1;
    }

look the same slowed down, but serial printing ledPos goes from 1, 2, ... 23, 24 (forward) and 255, 22, ... 1, 0 (backward) with subtracting 1. Why this is so, I cannot visually discern or understand.

Why this is so, I cannot visually discern or understand.

From those snippets, neither can we. You wasted your time, and ours, posting them.

Hej PaulS,

no time wasted - forum contributor AWOL's hint found me a solution!

In any case, I added the missing code in the initial post.