NeoPixel Pattern - Add All white pattern

Working on building a Rack Lighting system. Currently have the following code working, but would like to add a pattern of all white light, no blink, until I press the button. Any suggestions?

// StrandTest from AdaFruit implemented as a state machine
// pattern change by push button
// By Mike Cook Jan 2016
// modified by Swin Breaux Aug 2016 to include LCD & White Lights

#define PINforControl   5 // pin connected to the small NeoPixels strip
#define NUMPIXELS1      10 // number of LEDs on strip

#include <LiquidCrystal.h>
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS1, PINforControl, NEO_GRB + NEO_KHZ800);

unsigned long patternInterval = 20 ; // time between steps in the pattern
unsigned long lastUpdate = 0 ; // for millis() when last update occoured
unsigned long intervals [] = { 20, 20, 50, 100, 100} ; // speed for each pattern
const byte button = 3; // pin to connect button switch to between pin and ground

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

void setup() {
  strip.begin(); // This initializes the NeoPixel library.
  wipe(); // wipes the LED buffers
  pinMode(button, INPUT_PULLUP); // change pattern button
  // initialize the BUTTON pin as an input
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Light Show");
}

void loop() {
  static int pattern = 0, lastReading;
  int reading = digitalRead(button);
  if(lastReading == HIGH && reading == LOW){
    pattern++ ; // change pattern number
    if(pattern > 4) pattern = 0; // wrap round if too big
    patternInterval = intervals[pattern]; // set speed for this pattern
    wipe(); // clear out the buffer 
    delay(50); // debounce delay
  }
  lastReading = reading; // save for next time

if(millis() - lastUpdate > patternInterval) updatePattern(pattern);
}

void  updatePattern(int pat){ // call the pattern currently being created
  switch(pat) {
    case 0:
        rainbow(); 
        lcd.setCursor(0, 1);
        lcd.print("Rainbow               ");
        break;
    case 1: 
        rainbowCycle();
        lcd.setCursor(0, 1);
        lcd.print("RainbowCycle        ");        break;
    case 2:
        theaterChaseRainbow(); 
        lcd.setCursor(0, 1);
        lcd.print("Theater Chase       ");        break;
    case 3: 
        colorWipe(strip.Color(255, 0, 0)); // red
        lcd.setCursor(0, 1);
        lcd.print("Red Color Wipe      ");         break;     
    case 4:
        colorWipe(strip.Color(128, 128, 128)); // white
        lcd.setCursor(0, 1);
        lcd.print("White Color Stop    ");         break;             
  }  
}

void rainbow() { // modified from Adafruit example to make it a state machine
  static uint16_t j=0;
    for(int i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
     j++;
  if(j >= 256) j=0;
  lastUpdate = millis(); // time for next change to the display
  
}
void rainbowCycle() { // modified from Adafruit example to make it a state machine
  static uint16_t j=0;
    for(int i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
  j++;
  if(j >= 256*5) j=0;
  lastUpdate = millis(); // time for next change to the display
}

void theaterChaseRainbow() { // modified from Adafruit example to make it a state machine
  static int j=0, q = 0;
  static boolean on = true;
     if(on){
            for (int i=0; i < strip.numPixels(); i=i+3) {
                strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
             }
     }
      else {
           for (int i=0; i < strip.numPixels(); i=i+3) {
               strip.setPixelColor(i+q, 0);        //turn every third pixel off
                 }
      }
     on = !on; // toggel pixelse on or off for next time
      strip.show(); // display
      q++; // update the q variable
      if(q >=3 ){ // if it overflows reset it and update the J variable
        q=0;
        j++;
        if(j >= 256) j = 0;
      }
  lastUpdate = millis(); // time for next change to the display    
}

void colorWipe(uint32_t c) { // modified from Adafruit example to make it a state machine
  static int i =0;
    strip.setPixelColor(i, c);
    strip.show();
  i++;
  if(i >= strip.numPixels()){
    i = 0;
    wipe(); // blank out strip
  }
  lastUpdate = millis(); // time for next change to the display
}

void wipe(){ // clear all LEDs
     for(int i=0;i<strip.numPixels();i++){
       strip.setPixelColor(i, strip.Color(0,0,0)); 
       }
}

uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

INTP: Use showcolor

Can you give me an example? I get error 'showcolor' was not declared in this scope.

Also found this, but if I have a 300 light strip, it would be huge!

// Pattern 1 - White light, all LEDs in the strip are white
    void pattern1() {
        strip.setPixelColor(0, strip.Color(255, 255, 255)); // White
        strip.setPixelColor(1, strip.Color(255, 255, 255));
        strip.setPixelColor(2, strip.Color(255, 255, 255));
        strip.setPixelColor(3, strip.Color(255, 255, 255));
        strip.setPixelColor(4, strip.Color(255, 255, 255));
    strip.show();
    }

Nevermind, noticed you're using the adafruit library instead of fastled. I think strip.color might be what you need.

I don't have that library, but if you navigate to its library folder you might find a doc for keywords so you know what commands the library recognizes.

Use a ‘for’ loop 0-299
Step trough the loop setting the color.

for( index=0; index<300; index++)
{
strip.setPixelColor(index, strip.Color(255, 255, 255)); // White
}
strip.show();

.

While it is great to see you have used my code, it is sad to see that you have not understood what it is doing and why.

The updatePattern function is called every time the pattern needs to be changed by a single increment. This means it is being called every 20mS. Now what you have done is write to the LCD display every 20mS. This will take far longer than 20mS to do and so will screw up some of the patterns. You clearly have not got the hang of a state machine. If you want to display the pattern being shown then you have to do this when the pattern changes, that is on the push button that moves the pattern along.

if(lastReading == HIGH && reading == LOW){
    pattern++ ; // change pattern number
    if(pattern > 4) pattern = 0; // wrap round if too big
// ----> put a call to to a function that prints out the pattern name on the LCD here <-----
    patternInterval = intervals[pattern]; // set speed for this pattern
    wipe(); // clear out the buffer
    delay(50); // debounce delay
  }

If you are adding an extra pattern you will also have to make the intervals array bigger, and change the pattern number wrap round test.

Grumpy_Mike: While it is great to see you have used my code, it is sad to see that you have not understood what it is doing and why.

You are right. I am new at this, and trying to jump in with the water way over my head. It is not the best way to swim. Reading a lot of post may be hurting me more than helping. But I am learning some.

Thanks for pointing me in the right direction, and keeping me on the right path!

How is this?

void loop() {
  static int pattern = 0, lastReading;
  int reading = digitalRead(button);
    if (pattern == 0) 
        { lcd.setCursor(0, 1);
          lcd.print("Rainbow      ");
        }
    if (pattern == 1) 
        { lcd.setCursor(0, 1);
          lcd.print("RainbowCycle ");
        }
    if (pattern == 2) 
        { lcd.setCursor(0,1);
          lcd.print("TheaterChase ");
        }
     if (pattern == 3) 
        { lcd.setCursor(0,1);
          lcd.print("ColorWipe    ");
        }
     if (pattern == 4) 
        { lcd.setCursor(0,1);
          lcd.print("All White    ");
        }
        
  if(lastReading == HIGH && reading == LOW){
    pattern++ ; // change pattern number
    if(pattern > 4) pattern = 0; // wrap round if too big
    patternInterval = intervals[pattern]; // set speed for this pattern
    wipe(); // clear out the buffer 
    delay(50); // debounce delay
void  updatePattern(int pat){ // call the pattern currently being created
  switch(pat) {
    case 0:
        rainbow(); 
        break;
    case 1: 
        rainbowCycle();
        break;
    case 2:
        theaterChaseRainbow(); 
        break;
    case 3:
        colorWipe(strip.Color(255, 0, 0)); // red
        break;     
    case 4:
        whiteLight ();
        break;             
  }  
}

How is this?

What happened when you tried it?

.

Where is your white() function defined?

I see you have an updatePattern() but I don't see it called anywhere in your loop

A quick glance, do you intentionally have two separate variables, one called "pattern" and another called "pat"?

LarryD:
What happened when you tried it?

It worked fine. There is probably a shorter way to program the IF statement, but it is still a work in progress.

INTP:
Where is your white() function defined?

I see you have an updatePattern() but I don’t see it called anywhere in your loop

A quick glance, do you intentionally have two separate variables, one called “pattern” and another called “pat”?

On my initial post, I stated I need an “all white pattern”. My second reply, is showing my current pattern, with a strip of 10 pixels for testing.

For the “Pattern Variables” i will have to defer to “Grumpy_Mike”, since he said it was his code.

    if (pattern == 0) 
        { lcd.setCursor(0, 1);
          lcd.print("Rainbow      ");
        }
    else if (pattern == 1) 

etc.

Use 'else if' after the first 'if'

https://www.arduino.cc/en/Reference/Else

Or just use switch/case

.

LarryD: Or just use switch/case

Thanks! Little cleaner, and it works!

void loop() {
  static int pattern = 0, lastReading;
  int reading = digitalRead(button);
  lcd.setCursor(0, 1);
   switch (pattern) {
        case 0:
          lcd.print("Rainbow      "); break;
        case 1:
          lcd.print("RainbowCycle "); break; 
        case 2:
          lcd.print("TheaterChase "); break;
        case 3:
          lcd.print("ColorWipe    "); break;
        case 4:
          lcd.print("All White    "); break;
        }
        
  if(lastReading == HIGH && reading == LOW){
    pattern++ ; // change pattern number
    if(pattern > 4) pattern = 0; // wrap round if too big
    patternInterval = intervals[pattern]; // set speed for this pattern
    wipe(); // clear out the buffer 
    delay(50); // debounce delay

  }
  lastReading = reading; // save for next time

if(millis() - lastUpdate > patternInterval) updatePattern(pattern);
}

Thanks! Little cleaner, and it works!

No. I bet the LCD display is flickering. That is because you are updating the display every time through the loop.

Read reply #4 again. Write that code as a function [u]and call that function where I said in that reply[/u].

Display code as a function:-

void displayPattern(){
lcd.setCursor(0, 1);
   switch (pattern) {
        case 0:
          lcd.print("Rainbow      "); break;
        case 1:
          lcd.print("RainbowCycle "); break;
        case 2:
          lcd.print("TheaterChase "); break;
        case 3:
          lcd.print("ColorWipe    "); break;
        case 4:
          lcd.print("All White    "); break;
        }
    }

Grumpy_Mike:
No. I bet the LCD display is flickering.

I didn’t see any flickering, but will take your word for it.

Read reply #4 again.

Tried that but get errors.
" switch (pattern) {

^

exit status 1
‘pattern’ was not declared in this scope"

void loop() {
  static int pattern = 0, lastReading;
  int reading = digitalRead(button);

  if(lastReading == HIGH && reading == LOW){
    pattern++ ; // change pattern number
    if(pattern > 4) pattern = 0; // wrap round if too big
    displayPattern(); //Display Pattern name on LCD.
    patternInterval = intervals[pattern]; // set speed for this pattern
    wipe(); // clear out the buffer 
    delay(50); // debounce delay
  }
  lastReading = reading; // save for next time

if(millis() - lastUpdate > patternInterval) updatePattern(pattern);
}
void displayPattern(){
lcd.setCursor(0, 1);
   switch (pattern) {
        case 0:
          lcd.print("Rainbow      "); break;
        case 1:
          lcd.print("RainbowCycle "); break;
        case 2:
          lcd.print("TheaterChase "); break;
        case 3:
          lcd.print("ColorWipe    "); break;
        case 4:
          lcd.print("All White    "); break;
        }
    }

You have 'pattern' define in loop(), make it global.

scope http://forum.arduino.cc/index.php?topic=383064.0

.

Tried that but get errors.

OK important lesson here. When you get errors it is not because you are trying to do something that is incorrect but you are getting the syntax of what you are trying to do wrong. If you get an error then look what the error says and try and understand it. The error message is just where the compiler got confused sometimes the actual error is elsewhere like in the line before, or in this case way back.

This error is telling you that the compiler doesn't know what the variable "pattern" is, in other words it has not been defined. Variables can be global - they are defined everywhere, or local :- they are valid only in the function they are defined.

LarryD: You have 'pattern' define in loop(), make it global.

scope http://forum.arduino.cc/index.php?topic=383064.0

Looking into this, thanks for the link with details!

Grumpy_Mike:
OK important lesson here.

I figured this. Having issues trying to get it working correctly. Each change I make, causes different issues.

I will continue working on this, and hopefully figure it out my self.

Kudos & Karma points for both “Grumpy_Mike” & “LarryD”

I will continue working on this, and hopefully figure it out my self.

That is good it is the best way to learn, but we are always here if you want to ask something.