Light Flicker effecting other LED's in Code

A second set of eyes to review my code may be helpful. I have rearranged code, commented out and determined that the last section "// Begin Light Flicker " (line 123) is effecting the other LED's from turning on. I have this code working without the flicker part, and can comment it out and the other lights work. I am wondering if it has something to do with my use of millis and the random brightness settings of code that are not allowing the other LED's to turn on?

My setup is a Neopixel 12 LED ring that on setup ramps color up and then during the loop flickers randomly. The other three LED are IR controlled and should turn on with button presses from the IR and then I am using the millis command to turn them off after a certain period.

Should I break that last section out into another Switch / Case? Or something else?

#include <IRremote.h>
#include <Adafruit_NeoPixel.h>

int RECV_PIN = 2; // the pin where you connect the output pin of sensor
int led1 = 8; // headlights
int led2 = 9; // red warning lights
int led3 = 10;  //interior solid
int led4 = 11;   //interior blinking
unsigned long currentTime0;
unsigned long currentTime1;
unsigned long currentTime2;
unsigned long currentTime3;
int onTime0 = 35;
int onTime1 = 10000;
int onTime2 = 10000;
int onTime3 = 10000;
unsigned long previousMillis1 = 0;


#define PIN  6 // defines NEOPIXEL pin
#define engineLED 3  // engine warm-up sound fx activation pin
#define LED_COUNT 12  // defines NEOPIXEL light count
#define code1 0xFF7A85// code received from button no. 3
#define code2 0xFF5AA5 // code received from button no. 6
#define code3 0xFF52AD // code received from button no. 9

Adafruit_NeoPixel strip = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  strip.begin();
  strip.Color(0, 255, 255, 0); // pixel color set to light blue
  strip.show();
  currentTime0 = millis();
  // Begin Engine LED Warmup

  int i = 0;
  int c = 255;

  do {
    i = i + 1;
    c = c - 1;
    strip.setPixelColor(0, 0, c, 255); // set pixel color
    strip.setPixelColor(1, 0, c, 255); // set pixel color
    strip.setPixelColor(2, 0, c, 255); // set pixel color
    strip.setPixelColor(3, 0, c, 255); // set pixel color
    strip.setPixelColor(4, 0, c, 255); // set pixel color
    strip.setPixelColor(5, 0, c, 255); // set pixel color
    strip.setPixelColor(6, 0, c, 255); // set pixel color
    strip.setPixelColor(7, 0, c, 255); // set pixel color
    strip.setPixelColor(8, 0, c, 255); // set pixel color
    strip.setPixelColor(9, 0, c, 255); // set pixel color
    strip.setPixelColor(10, 0, c, 255); // set pixel color
    strip.setPixelColor(11, 0, c, 255); // set pixel color
    strip.show();
    delay(35); // speed of color change
    strip.setBrightness(0 + i); // brings up brightness from 0 to full
    strip.show();
    delay (35); // speed of brightening
  } while (i < 255, c > 0);
}
void loop()
{
  {
    if (irrecv.decode(&results)) {
      unsigned int value = results.value;
      switch (value) {
        case code1:
          digitalWrite(led1, HIGH); // turn it on when the button is pressed
          currentTime1 = millis();
          break;
      }
      irrecv.resume(); // Receive the next value
    }
    if ((millis() - currentTime1) >= onTime1)
    {
      digitalWrite(led1, LOW); // turn it off based on time
    }
    {
      if (irrecv.decode(&results)) {
        unsigned int value = results.value;
        switch (value) {
          case code2:
            digitalWrite(led2, HIGH); // turn it on when the button is pressed
            currentTime2 = millis();
            break;
        }
        irrecv.resume(); // Receive the next value
      }
      if ((millis() - currentTime2) >= onTime2)
      {
        digitalWrite(led2, LOW); // turn it off based on time
      }
      {
        if (irrecv.decode(&results)) {
          unsigned int value = results.value;
          switch (value) {
            case code3:
              digitalWrite(led3, HIGH); // turn it on when the button is pressed
              currentTime3 = millis();
              break;
          }
          irrecv.resume(); // Receive the next value
        }
        if ((millis() - currentTime3) >= onTime3)
        {
          digitalWrite(led3, LOW); // turn it off based on time
        }
      }
      {
        // Begin Light Flicker

        strip.setBrightness(random(25, 255)); // random brightness adjust for flicker effect
        strip.show();
        unsigned long currentMillis1 = millis(); // random time adjust for brightness
        if (currentMillis1 - previousMillis1 > (random(500, 1200))) {
          previousMillis1 = currentMillis1;
        }
      }
    }
  }
}

What the Hell is that?

Random flickering of various colors along the LED strip? Power supply problems.

Things like wiring diagram, a pic of the project would be helpful.

And what the heck is that, see post # 2 and 4.

Why the second brace?

What is the type of "results.value"?

That is part of the original code that I got to ramp the LED brightness up - the Do statement starts out with :

do {
i = i + 1;
c = c - 1;

Now removed..

So another strange issue.
I put this code in to ensure I was getting correct input from the IR remote

Serial.println(results.value, HEX);

and I get random strings of output on button presses. Not the HEX codes I was expecting (so that is my problem)
Example:
34264213
F88B0435
65763615
8B8F608
4F256576
31B5A95D
A7BCBAB
DAD0543D
498D81BC

I have a good working code for the LED's with out the NeoPixel removing

unsigned int value = results.value;

and the other suggestions made.

#include <IRremote.h>

int RECV_PIN = 2; // the pin where you connect the output pin of sensor
int led1 = 8;     // the pin where you connect the LED
int led2 = 9;
int led3 = 10;
unsigned long currentTime1;
unsigned long currentTime2;
unsigned long currentTime3;
int onTime1 = 10000;
int onTime2 = 10000;
int onTime3 = 10000;
#define code1 0xFF7A85// code received from button no. 3
#define code2 0xFF5AA5 // code received from button no. 6
#define code3 0xFF52AD // code received from button no. 9
IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  Serial.begin(9600); // you can ommit this line
  irrecv.enableIRIn(); // Start the receiver
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
}

void loop()
{
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX);

    switch (results.value) {
      case code1:
        digitalWrite(led1, HIGH); // turn it on when the button is pressed
        currentTime1 = millis();
        break;
    }
    irrecv.resume(); // Receive the next value
  }
  if ((millis() - currentTime1) >= onTime1)
  {
    digitalWrite(led1, LOW); // turn it off based on time
  }
  if (irrecv.decode(&results)){
    switch (results.value) {
      case code2:
        digitalWrite(led2, HIGH); // turn it on when the button is pressed
        currentTime2 = millis();
        break;
    }
    irrecv.resume(); // Receive the next value
  }
  if ((millis() - currentTime2) >= onTime2)
  {
    digitalWrite(led2, LOW); // turn it off based on time
  }
  if (irrecv.decode(&results)){
    switch (results.value) {
      case code3:
        digitalWrite(led3, HIGH); // turn it on when the button is pressed
        currentTime3 = millis();
        break;
    }
    irrecv.resume(); // Receive the next value
  }
  if ((millis() - currentTime3) >= onTime3)
  {
    digitalWrite(led3, LOW); // turn it off based on time
  }
}

I'll work on providing a wiring diagram soon. Just need to determine why the original code doesn't send the proper HEX values.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.