1

I'm having this weird effect where my NeoPixel will blink/glitch after every cycle of a fading sketch. I'm not certain whether the issue is hardware or in my code. The blinking only occurs during the if statement portion of the code and not during the else statement which blinks red. I have tried multiple sources of power, 5V 2amp, 9V 1 amp, 12V 1amp and a Mac Usb and the problem persists on each power supply.

Any suggestions or ideas would be greatly appreciated.

#include <Adafruit_NeoPixel.h>
#include <DistanceGP2Y0A21YK.h>

#define PIN 6
#define PIXEL 16

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL, PIN, NEO_GRBW + NEO_KHZ800);


DistanceGP2Y0A21YK Dist;
int distance;
int R = 0;
int G = 0;
int B = 0;


void setup() {
  strip.begin();
  strip.show();
  strip.setBrightness(127);

  Serial.begin(9600);
  Dist.begin(A0);
}

void loop() {
  distance = Dist.getDistanceCentimeter();
  Serial.print("\nDistance in centimers: ");
  Serial.print(distance);
  delay(2); //make it readable

  if (distance > 10) {

    for (R && G && B; R < 150 && G < 150 && B < 150; R++ && G++ && B++) {
      for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(R, G, B));
        strip.show();
        delay(1);
      }
      delay(2);
    }

    // fade off
    for (R && G && B; R > -1 && G > -1 && B > -1; R-- && G-- && B--) {
      for (int j = 0; j < strip.numPixels(); j++) {
        strip.setPixelColor(j, strip.Color(R, G, B));
        strip.show();
        delay(1);
      }
      delay(1);
    }

  } else {

    for (int j = 0; j < 256; j++) {
      for (int i = 0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, strip.Color(255, 0, 0));
        strip.show();
      }
      delay (1);

      //blink off
      for (int j = 64; j > 0; j--) {
        for (int i = 0; i < strip.numPixels(); i++) {
          strip.setPixelColor(i, strip.Color(j, 0, 0));
          strip.show();
        }
        delay (2);
      }
    }
  }
}
1
  • Pick a brace style and stick with it. You'll find that other people can read your code that way. Jul 12, 2016 at 1:22

1 Answer 1

1
for (int j = 0; j < 256; j++) {

  ...

  //blink off
  for (int j = 64; j > 0; j--) {
    ...

Notice that you are re-declaring the variable j.

1
  • 1
    Thanks for all the responses however I diagnosed the problem and figured out the fix. The problem comes from my for loop where I use a boolean expression for (R && G && B;. Rather the values should be initialized to 0 and look like for (R = G = B = 0; Jul 12, 2016 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.