ok, I think I know what you want to do. I played with it for a bit. This will work, assuming the button code is still ok.
make sure you understand the code though.
const int buttonPin = 2; // the number of the pushbutton pin
int ledPin[] = {
13, 9, 10, 11}; // pwm pins only
int ledState[5]; // last state of each led
int lastReading = 0; // -----> added this as you forgot to initialize it
int lastButtonState = LOW; // -----> added this as you forgot to initialize it
///////////////////////////
// this is a variable which decides whether to blinklights or not
boolean blinkLEDs = true;
///////////////////////////
long randNumber;
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
for (int i=0; i<=4; i++){ // set each led pin as an output
pinMode(ledPin[i], OUTPUT); // -----> changed from ledPin to ledPin[i]
}
randomSeed(analogRead(0)); // seed the rnd generator with noise from unused pin
for (int i=0; i<=4; i++){ // init each led with a random value
ledState[i] = random(20, 201); // -----> changed from ledPin to ledPin[i]
}
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastReading) {
// reset the debouncing timer
lastDebounceTime = millis();
// save the reading. Next time through the loop,
// it'll be lastReading:
lastReading = reading;
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so accept the button state change:
// toggle the LED if the state of the button changes from LOW to HIGH:
if (lastButtonState == LOW && reading == HIGH) {
////////////////////////
///// if blinkLEDs is true, the following code will set it to false
////// if blinkLEDS is flase it will be set to true.
////// so everytime you push the button, it will switch state
///////////////////////
if (blinkLEDs == true){
blinkLEDS = false;
}
else {
blinkLEDs = true;
}
}
lastButtonState = reading;
}
////////////
//if blinkLEDs is true, awesomeNebulaEffect will happen
if (blinkLEDs == true) {
doAwesomeNebulaEffect(); // this calls the function "doAwseomNebulaEffect"
}
///////////////
// THis is where the loop ends
}
//////////////////////
// this is a function. its called from within the loop, but the code is outside of the loop
// here we declare the function which makes the nabula effect
void doAwesomeNebulaEffect () {
for (int i=0; i<=4; i++){ // for each led:
analogWrite(ledPin[i], ledState[i]); // set the pwm value of that pin determined previously -----> its an ARRAY tell it which index you need
randNumber = random(-40, 41); // generate new random number and add that to the current value
ledState[i] += randNumber; // that range can be tweaked to change the intensity of the flickering
if (ledState[i] > 200) { // clamp the limits of the pwm values so it remains within
ledState[i] = 200; // a pleasing range as well as the pwm range
}
if (ledState[i] < 10) {
ledState[i] = 10;
}
}
}