LEDs wont cycle through modes (ws2801)

Ive been learning arduino over the past year and am sincerly stumped by this little issue. I have a momentary switch hooked up to two WS2801 breakouts and i cant get a switch to cycle through. I really thought this was it but alas i am STUMPED! Any help would rock!

#include "SPI.h"
#include "WS2801.h"
    
    int buttonPin = 4;    // momentary push button on pin 4
    int oldButtonVal = 0;
    int dataPin = 2;
    int clockPin = 3;
    
   
     
    
    
   
   WS2801 strip = WS2801(2, dataPin, clockPin);
    
    int nPatterns = 3;
    int lightPattern = 1;

// the setup routine runs once when you press reset:
    void setup() {
    strip.begin();
    strip.show();                // initialize all pixels to 'off'    
// initialize the BUTTON pin as an input
    pinMode(buttonPin, INPUT);
    digitalWrite(buttonPin, HIGH);  // button pin is HIGH, so it drops to 0 if pressed
}


// Pattern 1 - White light, all LEDs in the strip are white
    void pattern1() {
        strip.setPixelColor(0,(190,0,60));
        strip.setPixelColor(1,(100,190,00));
    
    strip.show();
    }
    
// Pattern 2 - Red light, all LEDs in the strip are red
    void pattern2() {
        strip.setPixelColor(0,(255, 100, 0)); // Red
        strip.setPixelColor(1,(0, 111, 0));

    strip.show();
    }
    // Pattern 2 - Red light, all LEDs in the strip are red
    void pattern3() {
        strip.setPixelColor(0,(255, 255, 255)); // Red
        strip.setPixelColor(1,(199, 111, 9));

    strip.show();
    }
    

    
// the loop routine runs over and over again forever;
void loop() {
  // read that state of the pushbutton value;
  int buttonVal = digitalRead(buttonPin);
  if (buttonVal == LOW && oldButtonVal == HIGH) {// button has just been pressed
    lightPattern = lightPattern + 1;
  }
  if (lightPattern > nPatterns) lightPattern = 1;
  oldButtonVal = buttonVal++;
  
  switch(lightPattern) {
    case 1:
      pattern1();
      break;
    case 2:
      pattern2();
      break;
      case 3:
      pattern3();
      break;
  }  
}
[code/]

What is the purpose of this?

  oldButtonVal = buttonVal++;

The 'previous' button value is not the current value plus one... the value you want to store for the next time around is the current value! So, I think it should be...

  oldButtonVal = buttonVal;

Hmm, def true. It didnt fix the code though :frowning:

Please be more specific then. I think I know what you expect to happen, but please say what actually happens.

Also, you don't need to check the value of "lightPattern" for overflow when you haven't changed it, so I would restructure the block this way...

  if (buttonVal == LOW && oldButtonVal == HIGH) {// button has just been pressed
    lightPattern++;
    if (lightPattern > nPatterns) lightPattern = 1;
  }

Thank you. the first LED of the Strip i made stays blue and the second does nothing. The button also yields no response. Once again to explain in the best way possible.

I want to press the button and each time have it cycle through a different predetermined set of 2 colors before it eventually is back to off.

Sounds like you might have multiple hardware issues. Can you post a diagram or something?

I totaly fixed it by mixing it with another code i found. The only thing it wont do is start on the off position…why is this?

#include "SPI.h"
#include "WS2801.h"



const int buttonPin = 4;     // the number of the pushbutton pin
int dataPin = 2;
int clockPin = 3;

int oldButtonVal = 0;

int nPatterns = 4;
int lightPattern = 1;

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

// Don't forget to connect the ground wire to Arduino ground,
// and the +5V wire to a +5V supply


// Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row
WS2801 strip = WS2801(2, dataPin, clockPin);

// Optional: leave off pin numbers to use hardware SPI
// (pinout is then specific to each board and can't be changed)
//WS2801 strip = WS2801(25);

void setup() {
    
    strip.begin();
    strip.show();                // initialize all pixels to 'off'    
// initialize the BUTTON pin as an input
    pinMode(buttonPin, INPUT);
    digitalWrite(buttonPin, HIGH);  // button pin is HIGH, so it drops to 0 if pressed
}

  
  // initialize the pushbutton pin as an input:

  
 


  // Update LED contents, to start they are all 'off'



void loop() {
  // Some example procedures showing how to display to the pixels
  
   // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
   int buttonVal = digitalRead(buttonPin);
  if (buttonVal == LOW && oldButtonVal == HIGH) {// button has just been pressed
    lightPattern = lightPattern + 1;
  }
  if (lightPattern > nPatterns) lightPattern = 1;
  oldButtonVal = buttonVal;
  
  switch(lightPattern) {
    case 1:
      pattern1();
      break;
    case 2:
      pattern2();
      break;
      case 3:
      pattern3();
      break;
    case 4:
      pattern4();
      break;
  }
}void pattern1() {
     strip.setPixelColor(0, Color(255,77,255));
     strip.setPixelColor(1, Color(144,0,6));
     strip.show();   // write all the pixels out
  } void pattern2() {
     strip.setPixelColor(0, Color(255,0,255));
     strip.setPixelColor(1, Color(0,0,255));
     strip.show();   // write all the pixels out
  }  void pattern3() {
     strip.setPixelColor(0, Color(0,255,255));
     strip.setPixelColor(1, Color(100,0,255));
     strip.show();   // write all the pixels out
  }  void pattern4() {
     strip.setPixelColor(0, Color(0,255,0));
     strip.setPixelColor(1, Color(100,255,255));
     strip.show();   // write all the pixels out
  
  
    
    
  }

  
  


void rainbow(uint8_t wait) {
  int i, j;

  for (j=0; j < 256; j++) {     // 3 cycles of all 256 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel( (i + j) % 255));
    }  
    strip.show();   // write all the pixels out
    delay(wait);
  }
}

// Slightly different, this one makes the rainbow wheel equally distributed 
// along the chain
void rainbowCycle(uint8_t wait) {
  int i, j;

  for (j=0; j < 256 * 5; j++) {     // 5 cycles of all 25 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      // tricky math! we use each pixel as a fraction of the full 96-color wheel
      // (thats the i / strip.numPixels() part)
      // Then add in j which makes the colors go around per pixel
      // the % 96 is to make the wheel cycle around
      strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
    }  
    strip.show();   // write all the pixels out
    delay(wait);
  }
}

// fill the dots one after the other with said color
// good for testing purposes
void colorWipe(uint32_t c, uint8_t wait) {
  int i;

  for (i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

/* Helper functions */

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
  uint32_t c;
  c = r;
  c <<= 8;
  c |= g;
  c <<= 8;
  c |= b;
  return c;
}

//Input a value 0 to 255 to get a color value.
//The colours are a transition r - g -b - back to r
uint32_t Wheel(byte WheelPos)
{
  if (WheelPos < 85) {
    return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } 
  else if (WheelPos < 170) {
    WheelPos -= 85;
    return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } 
  else {
    WheelPos -= 170; 
    return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}
[code/]

Well, you have programmed it to start with light pattern 1. You don’t have an “off” pattern. You should create that as pattern 1.

PERFECT!!!! Done Thank you!!!