0

I want to create a LED animation using Adafruit_NeoPixel library. Unfortunately I'm struggling with a probably dump thing. For some reason the strip does not anymore work when using a certain array access if (led_states[pixel] > 0) {. Meaning even strip.clean() + strip.show() in setup doesn't work anymore. May you can tell me what I did wrong because I really don't get it. PS: I'm using ATTiny85 with arduino framework if that helps.

#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>

#define LED_AMOUNT 30
#define LED_DATA_PIN PB1

SoftwareSerial SWSERIAL(0, PB3);  // RX, TX
Adafruit_NeoPixel strip(LED_AMOUNT, LED_DATA_PIN, NEO_GRB + NEO_KHZ800);

int step = 2;
int max = 255 - step;
int led_states[LED_AMOUNT] = { 0 };
int led_values[LED_AMOUNT] = { 0 };
unsigned long last_frame = 0;
unsigned long last_change = 0;

void led_test_setup() {
    delay(5000);
    SWSERIAL.begin(9600);
    SWSERIAL.println("Setup");
    randomSeed(analogRead(0));

    strip.begin();
    strip.clear();
    strip.show();
}

void led_test_loop() {
    unsigned long now = millis();
    if (last_change + 200 < now) {
        int pixel = random(0, LED_AMOUNT);
        led_states[pixel] = 1;
        led_values[pixel] = 0;
        last_change = now;
    }
    if (last_frame + 10 < now) {
        for (int pixel = 0; pixel < LED_AMOUNT; pixel++) {
            if (led_states[pixel] > 0) { // <---- strip works when commenting this block
                SWSERIAL.printf("V: %i\n", led_values[pixel]);
            }
            strip.setPixelColor(pixel, led_values[pixel], led_values[pixel], led_values[pixel]);
        }
        last_frame = now;
        strip.show();
    }
}
fragsalat
  • 500
  • 4
  • 14
  • Try changing if (led_states[pixel] > 0) { // <---- strip works when commenting this block SWSERIAL.printf("V: %i\n", led_values[pixel]); } ..to just... SWSERIAL.printf("V: %i\n", led_values[pixel]); ...and see if that works. (My guess is that it will not) – bigjosh Dec 07 '21 at 04:49
  • Commenting out the `if (led_state[pixel] > 0) {` but keeping the `printf` works. The strip can still be controlled. Once I uncomment the if again, the strip can not be controlled anymore. No clue how this could relate o.O Why would you have expected to not work then? Weird is also there is no exception and code still runs through and logs as expected but just the strip functions take no effect. – fragsalat Dec 07 '21 at 12:53
  • 1
    Your values in `led_values` are always 0 so the LEDs are always off. I am not sure what you even consider the LED strip working. – gre_gor Dec 07 '21 at 15:51
  • I know it's working or not as the strip gets enlightned during flashing process. So when it is turned off again it works. If not it didn't – fragsalat Dec 07 '21 at 19:41

1 Answers1

0

I may found the issue. Seems like SoftwareSerial and Neopixel library are using both interrupts which then corrupts neo pixel communication.

Found here: https://forum.arduino.cc/t/arduino-nano-softwareserial-adafruit_neopixel-problem/540057/2

fragsalat
  • 500
  • 4
  • 14