0

Flicker Effect on Neopixel 12 Ring
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Flicker Effect on Neopixel 12 Ring

by Hsestes on Wed May 12, 2021 10:35 am

I have the below code that operates a Neopixel 12 LED ring. I had the flicker effect working in another code, but now that I am expanding on the code the flicker portion doesn't function. Can some one assist and review the code to see what I may be missing? The flicker code is at line 92. I do realize there are some other issues in the code I am still working through as well.

Code: Select all | TOGGLE FULL SIZE
#include <Adafruit_NeoPixel.h>
#include <IRremote.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
int itsONled[] = {0, 0, 0, 0};
/* the initial state of LEDs is OFF (zero)
  the first zero must remain zero but you can
  change the others to 1's if you want a certain
  led to light when the board is powered */

unsigned long currentTime;
unsigned long previousMillis1 = 0;
//unsigned long previousMillis2 = 0;
//unsigned long previousMillis3 = 0;
//unsigned long interval = 120;
//int onTime = 500;

#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 code14 0xFF7A85// code received from button no. 3
#define code17 0xFF5AA5 // code received from button no. 6
#define code20 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);
  pinMode(engineLED, OUTPUT);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}
void loop() {
  if (irrecv.decode(&results)) {
    Serial.println(results.value, HEX); // Print Code in HEX
    unsigned int value = results.value;
    switch (value) {
      case code14:
        if (itsONled[1] == 1) { // if first led is on then
          strip.show(); // turn it off when button is pressed
          itsONled[1] = 0; // and set its state as off
        } else { // else if first led is off
          strip.begin();// turn it on when the button is pressed
          itsONled[1] = 1; // and set its state as on
        }

        strip.Color(0, 255, 255, 0); // pixel color set to light blue
        strip.show();
        currentTime = millis();

        // Begin Engine 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(25); // speed of color change
          strip.setBrightness(0 + i); // brings up brightness from 0 to full
          strip.show();
          delay (25); // speed of brightening
        } while (i < 255, c > 0);


        // 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;
        } break;
      case code17:
        if (itsONled[2] == 1) {
          digitalWrite(led2, LOW);
          itsONled[2] = 0;
        } else {
          digitalWrite(led2, HIGH);
          itsONled[2] = 1;
        }
        break;
      case code20:
        if (itsONled[3] == 1) {
          digitalWrite(led3, LOW);
          itsONled[3] = 0;
        } else {
          digitalWrite(led3, HIGH);
          itsONled[3] = 1;
        }
        break;
    }
    irrecv.resume(); // Receive the next value
  }
}

Hsestes
 
Posts: 4
Joined: Tue May 11, 2021 6:40 pm

Re: Flicker Effect on Neopixel 12 Ring

by adafruit_support_bill on Wed May 12, 2021 11:17 am

Your flicker code at line 92 is part of a much larger case beginning on line 51. It will only execute one iteration of that code after the rather large do/while loop that precedes it.

adafruit_support_bill
 
Posts: 81327
Joined: Sat Feb 07, 2009 10:11 am

Re: Flicker Effect on Neopixel 12 Ring

by Hsestes on Wed May 12, 2021 2:28 pm

adafruit_support_bill wrote:Your flicker code at line 92 is part of a much larger case beginning on line 51. It will only execute one iteration of that code after the rather large do/while loop that precedes it.


So should I create some type of loop with the void loop?

Hsestes
 
Posts: 4
Joined: Tue May 11, 2021 6:40 pm

Re: Flicker Effect on Neopixel 12 Ring

by adafruit_support_bill on Wed May 12, 2021 2:46 pm

I can't answer that without a clear definition of what you want the system to do. Right now your code appears to recieve some command code via IR and executes different cases for different codes. If you want the flicker to work, it needs to be a case by itself - not lumped in with another case.

adafruit_support_bill
 
Posts: 81327
Joined: Sat Feb 07, 2009 10:11 am

Re: Flicker Effect on Neopixel 12 Ring

by Hsestes on Wed May 12, 2021 7:18 pm

The goal of that portion of code is to fade up the Neopixel rings color from light blue to bright blue and then flicker randomly. Below is line 50 to 99 of the code that was posted above.

Code: Select all | TOGGLE FULL SIZE
switch (value) {
      case code14:
        if (itsONled[1] == 1) { // if first led is on then
          strip.show(); // turn it off when button is pressed
          itsONled[1] = 0; // and set its state as off
        } else { // else if first led is off
          strip.begin();// turn it on when the button is pressed
          itsONled[1] = 1; // and set its state as on
        }

        strip.Color(0, 255, 255, 0); // pixel color set to light blue
        strip.show();
        currentTime = millis();

        // Begin Engine 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(25); // speed of color change
          strip.setBrightness(0 + i); // brings up brightness from 0 to full
          strip.show();
          delay (25); // speed of brightening
        } while (i < 255, c > 0);


        // 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;
        } break;

Hsestes
 
Posts: 4
Joined: Tue May 11, 2021 6:40 pm

Re: Flicker Effect on Neopixel 12 Ring

by adafruit_support_bill on Wed May 12, 2021 7:36 pm

Flicker for how long? Your lines 92-99 do just one random brightness change. If you want a visible flicker, you need it to execute that code repeatedly. One way is to put it into a loop. But you need to decide how long you want to continue flickering.

adafruit_support_bill
 
Posts: 81327
Joined: Sat Feb 07, 2009 10:11 am

Re: Flicker Effect on Neopixel 12 Ring

by Hsestes on Thu May 13, 2021 12:44 am

Thanks for the general pointers, I moved the fade up portion to the Void Setup and left the flicker portion in the Void Loop. It now works - but not how I intended. If the code is in the setup section, I understand it will only run that one time during start up. There is no way to start and stop it using an IR or button command in the setup section? I may have to go back and research how to incorporate it all in the Void Loop and set a time limit for the flicker to run.

Hsestes
 
Posts: 4
Joined: Tue May 11, 2021 6:40 pm

Re: Flicker Effect on Neopixel 12 Ring

by adafruit_support_bill on Thu May 13, 2021 5:35 am

Sounds like what you want is a 'State Machine'. It can run your flicker code indefinitely, while still being responsive to inputs:

https://learn.adafruit.com/multi-taskin ... ino-part-1
https://learn.adafruit.com/multi-taskin ... ino-part-2
https://learn.adafruit.com/multi-taskin ... ino-part-3

adafruit_support_bill
 
Posts: 81327
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.