Problem with Button and Adafuit neopixels.

Hi, I am new to coding and I have a code designed for the adafruit neopixel sticks where when I push a button once ( not hold down ) It would make the LED's go from one animation to then next out of many, but the problem i face now is that the button doesn't seem to do anything when i push it and the led's are stuck on the first mode when you give it power. Is there anyway I can solve this? I can be pretty dense at times so it would be great if you could explain it in simple terms thanks.

Thanks in advance.

Here is my code. (currently compiles but doesn't work)

I am using 2 adafruit neopixel sticks

#include <Adafruit_NeoPixel.h>
#include <math.h>
#define PIN 5
#define button_pin 3


#define FADESPEED 1
#define PERIOD 25
#define PERIOD2 100
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN);


int counter = 0;
int Formula(int t) {
  int button_Pin = 0;
  int oldbuttonVal = 0;
  int result;
  result = round(127.5 * (cos(2 * M_PI / 255 * t) + 1));
  return result;
}








void setup() {
  int button_Pin = 0;
  int oldbutton_Pin = 0;
  pinMode (button_pin, INPUT);
  digitalWrite(button_Pin, HIGH);
  strip.begin();
  strip.show();
  pinMode(PIN, OUTPUT);


}


void loop() {
  {
    {
      int r, g, b, t, x, counter;
      int button_Pin = 0;
      int oldbuttonVal = 0;

      int buttonVal = digitalRead(button_pin);

      if (buttonVal == LOW && oldbuttonVal == HIGH)

        counter ++;
      if (counter == 4) {
        counter = 1;
        oldbuttonVal = buttonVal;
      }






      {




        if (counter == 1) {

          for (t = 0; t < 255; t = t + 2) {
            for (x = 0; x < 16; x++) {
              r = Formula (t - x * PERIOD);
              g = Formula (t - x * PERIOD - PERIOD2);
              b = Formula (t - x * PERIOD - 2 * PERIOD2);
              strip.setPixelColor(x, r, g, b);
              strip.show();
              delay(FADESPEED);
            }
          }
        }



        else if (counter == 2) {

          for (t = 0; t < 255; t = t + 2) {
            for (x = 0; x < 16; x++) {
              r = Formula (t);
              g = Formula (t - PERIOD2);
              b = Formula (t - 2 * PERIOD2);
              strip.setPixelColor(x, r, g, b);
              strip.show();
              delay(FADESPEED);
            }
          }

        }



        else if (counter == 3) {
          digitalRead (counter == 3);
          //while (counter ==3 is false) {
          for (x = 0; x < 16; x++) {
            strip.setPixelColor(x, 255, 255, 255);
            strip.show();
          }
        }


        else if (counter == 4) {
          digitalRead (counter == 4);
          //while (counter == 4 is false){
          for (x = 0; x < 16; x++) {
            strip.setPixelColor(x, 255, 227, 105);
            strip.show();
          }
        }
      }

    }
  }
}


}





//mode colour swirl
//
//for (t=0; t<255; t=t+2){

//for (x=0; x < 16; x++){

//r = Formula (t - x * PERIOD);
//g = Formula (t - x * PERIOD - PERIOD2);
//b = Formula (t - x * PERIOD - 2*PERIOD2);
//strip.setPixelColor(x, r, g, b);
// strip.show();
//delay(FADESPEED);
//}

Well done on posting the code in code tags on your first post.

Way too much white space though, a single blank line between functions will do. Also too many braces in the loop function.

Your problem is that all your variables in the loop function get remade and loose any value they had each time the loop function is entered. Therefore your count variable can never be anything other than one. Also you have two different variables with the same name, while this is OK if you know what you are doing, you don't and are being confused by it.

Solution either delclaire them to be static variables or make them global variables by defining them outside the loop.

The problem you will not have seen yet is that the code only looks at the button once a complete anamation sequence has been performed.

What Mike said, plus

      if (counter == 4) {
        counter = 1;
        oldbuttonVal = buttonVal;
      }

you need to set oldbuttonVal to buttonVal every time in loop(), not just when counter hits 4.

I made my Counter a static Variable, and changed some of the variable names but now the led's are just black and still wont respond to the button. I guess Mike was right about not knowing what i was doing. Whats still not right?

code:

#include <Adafruit_NeoPixel.h>
#include <math.h>
#define PIN 5
int button_pin = 3;

#define FADESPEED 1
#define PERIOD 10
#define PERIOD2 75
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN);
   static int counter = 0;
  int buttonstate = 0;
  int oldbuttonstate = 0;
int Formula(int t) {

  int result;
  result = round(127.5 * (cos(2 * M_PI / 255 * t) + 1));
  return result;
}

void setup() {
  int buttonstate = 0;
  int oldbuttonstate = 0;
  pinMode (button_pin, INPUT);
  digitalWrite(buttonstate, HIGH);
  strip.begin();
  strip.show();
  pinMode(PIN, OUTPUT);

}

void loop() {
  {
 
    {
  static int counter;
      int r, g, b, t, x;
      int buttonstate = 0;
       int oldbuttonstate = 0;

       int button_pin = digitalRead(buttonstate);
      if (buttonstate == LOW && oldbuttonstate == HIGH)
   oldbuttonstate = buttonstate;
 counter ++;
  
     if (counter == 4) {
        counter = 1;   
      }
      {
        if (counter == 1) {
 counter = 1;
          for (t = 0; t < 255; t = t + 2) {
            for (x = 0; x < 16; x++) {
              r = Formula (t - x * PERIOD);
              g = Formula (t - x * PERIOD - PERIOD2);
              b = Formula (t - x * PERIOD - 2 * PERIOD2);
              strip.setPixelColor(x, r, g, b);
              strip.show();
              delay(FADESPEED);
            }
          }
        }
        else if (counter == 2) {
counter = 2;
          for (t = 0; t < 255; t = t + 2) {
            for (x = 0; x < 16; x++) {
              r = Formula (t);
              g = Formula (t - PERIOD2);
              b = Formula (t - 2 * PERIOD2);
              strip.setPixelColor(x, r, g, b);
              strip.show();
              delay(FADESPEED);
            }
          }

        }
        else if (counter == 3) {
 counter = 3;

          for (x = 0; x < 16; x++) {
            strip.setPixelColor(x, 255, 255, 255);
            strip.show();
          }
        }
        else if (counter == 4) {
 counter = 4;

          for (x = 0; x < 16; x++) {
            strip.setPixelColor(x, 255, 227, 105);
            strip.show();
          }
        }
      }
    }
  }
}

Yeah... You've got curly braces where you don't need them and not where you do need them, all over the place. You can't just cut & paste lines of code and hope for the best. You've got to understand what each individual line does, line by line. Also get used to using the Auto Format feature (Tools menu). Correct indentation will really help you get those braces in the right places!

oldbuttonstate also needs to be static. Think about it. Each time loop() runs, it checks the button. To know if the button has changed, it needs to know what the button was the previous time loop() ran. But if you make oldbuttonstate local to loop(), it's value will be lost when loop() ends. Or you could just declare them at the top of the sketch, before setup(), which has a similar effect to making them static.

Ok, sorry its been a while, I've modified my code a bit, but I still doesn't work, I don't need oldbuttonstate to be static because its a global variable right? the LED's are still stuck on mode with the initial counter variable despite it being a static variable, so I think something's wrong with the button code, any pointers would be great!

#include <Adafruit_NeoPixel.h>
#include <math.h>
#define PIN 5
int button_pin = 3;
#define FADESPEED 1
#define PERIOD 10
#define PERIOD2 75
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN);

int oldbuttonstate = 0;
int buttonstate = 0;
int Formula(int t) {

  int result;
  result = round(127.5 * (cos(2 * M_PI / 255 * t) + 1));
  return result;
}

void setup() {

  pinMode(PIN, OUTPUT);
  pinMode (button_pin, INPUT);
  strip.begin();
  strip.show();
Serial.begin(9600);

}

void loop() {
  int r, g, b, t, x;
  static int counter = 1;
  buttonstate = digitalRead(button_pin);
  if (buttonstate != oldbuttonstate) {
    if (buttonstate == LOW) {
Serial.print("PRESSED");
      counter ++;
    }
  }
  oldbuttonstate = buttonstate;
  if (counter == 5) {
    counter = 1;
  }

  if (counter == 1) {

    for (t = 0; t < 255; t = t + 2) {
      for (x = 0; x < 16; x++) {
        r = Formula (t - x * PERIOD);
        g = Formula (t - x * PERIOD - PERIOD2);
        b = Formula (t - x * PERIOD - 2 * PERIOD2);
        strip.setPixelColor(x, r, g, b);
        strip.show();
        delay(FADESPEED);
      }
    }
  }

  else if (counter == 2) {
    for (t = 0; t < 255; t = t + 2) {
      for (x = 0; x < 16; x++) {
        r = Formula (t);
        g = Formula (t - PERIOD2);
        b = Formula (t - 2 * PERIOD2);
        strip.setPixelColor(x, r, g, b);
        strip.show();
        delay(FADESPEED);

      }
    }
  }

  else if (counter == 3) {
    for (x = 0; x < 16; x++) {
      strip.setPixelColor(x, 255, 255, 255);
      strip.show();

    }
  }
  else if (counter == 4) {
    for (x = 0; x < 16; x++) {
      strip.setPixelColor(x, 255, 251, 133);
      strip.show();

    }
  }
}

(PS if i change initial counter to another number, LED's would be stuck on corresponding counter mode, so I know that it's the incrementing or button thats the problem.)

I've modified my code a bit, but I still doesn't work,

That is because you have ignored what I said in reply #8.

The problem you will not have seen yet is that the code only looks at the button once a complete animation sequence has been performed.

So unless you hold down the button until the end of the sequence your code will not be looking at the button when you push it.

The solution is NOT to use the delay function at all. You should either rewrite the animation sequence as a state machine. ( too complex for you at this stage but if you want to see what you need to do see
http://www.thebox.myzen.co.uk/Tutorial/State_Machine.html
Or Robin2's several things at once
http://forum.arduino.cc/index.php?topic=223286.0
)

Maybe the simplest is to replace the delay function with one that looks at the button and ends the delay when you see your button pressed. How to do this is described in this thread
http://forum.arduino.cc/index.php?topic=496863.0