analogWrite blocking button press

Hello all,
I'm working on a led project for my baby's stroller,
I have six led (3left, 3 right) that are controlled by a button press.
the loops works perfectly when I use digitalWrite but as soon as it hit the third step "lightMode == 3" with analogWrite the button stops responding,
I'm a noob with the arduino language and spent the last 48 hours googling my way through pages of forum ( I learned a lot :slight_smile: )
but I still can't make the button register,
I also thought of using a digitalWrite fade, but the leds don't go down to "0".

What am I doing wrong?
thanks

/*
 * http://www.ladyada.net/learn/arduino/lesson5.html
 *  Bike light, final version
 */

int switchPin = 12;              // switch is connected to pin 2
int led1Pin = 2;
int led2Pin = 3;
int led3Pin = 4;
int led4Pin = 5;
int led5Pin = 6;
int led6Pin = 7;
int fadeValue = 0;



int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state
int lightMode = 0;              // What mode is the light in?

void setup() {
  
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);
  pinMode(led4Pin, OUTPUT);
  pinMode(led5Pin, OUTPUT);
  pinMode(led6Pin, OUTPUT);
  
  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
}

void loop(){
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // if its off
          lightMode = 1;               // turn lights on!
        } else {
          if (lightMode == 1) {        // if its all-on
            lightMode = 2;             // make it blink!
          } else {
            if (lightMode == 2) {      // if its blinking
              lightMode = 3;           // make it wave!
            } else {
			  if (lightMode == 3) { //  if its fading, 
                lightMode = 4;           
              }else {
              	  if (lightMode == 4) { // turn light off! 
                lightMode = 0;    
              }
			}
          }
        }
      }
    }
    buttonState = val;                 // save the new state in our variable
  }

  // Now do whatever the lightMode indicates
  if (lightMode == 0) { // all-off
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, LOW);
    Serial.println ("lightMode");
    Serial.println ("----------");
    Serial.println ("sleep"); 
 

  }

  if (lightMode == 1) { // all-on
    digitalWrite(led1Pin, HIGH);
    digitalWrite(led2Pin, HIGH);
    digitalWrite(led3Pin, HIGH);
    digitalWrite(led4Pin, HIGH);
    digitalWrite(led5Pin, HIGH);
    digitalWrite(led6Pin, HIGH);
    Serial.println ("lightMode1");
    Serial.println ("----------");
    Serial.println ("on");  
  }

  if (lightMode == 2) { // blinking
    digitalWrite(led1Pin, HIGH);
    digitalWrite(led2Pin, HIGH);
    digitalWrite(led3Pin, HIGH);
    digitalWrite(led4Pin, HIGH);
    digitalWrite(led5Pin, HIGH);
    digitalWrite(led6Pin, HIGH);
    delay(100);
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, LOW);
    delay(100);
    Serial.println ("lightMode2");
    Serial.println ("----------");
    Serial.println ("blinky");  

  }
  if (lightMode == 3)  { // "fade"
    for (fadeValue = 0; fadeValue <= 255; fadeValue += 5){
    analogWrite (led1Pin, fadeValue);
    analogWrite (led2Pin, fadeValue);
    analogWrite (led3Pin, fadeValue);
    analogWrite (led4Pin, fadeValue);
    analogWrite (led5Pin, fadeValue);
    analogWrite (led6Pin, fadeValue);
    delay (10);
    }
    
    for (fadeValue = 255; fadeValue >= 0; fadeValue -= 5){
    analogWrite (led1Pin, fadeValue);
    analogWrite (led2Pin, fadeValue);
    analogWrite (led3Pin, fadeValue);
    analogWrite (led4Pin, fadeValue);
    analogWrite (led5Pin, fadeValue);
    analogWrite (led6Pin, fadeValue);
    delay (10);
    Serial.println ("lightMode3"); 
    Serial.println ("----------");
    Serial.println ("ffs just work"); 
    }
  } 

   if (lightMode == 4)  { // "wave"
    digitalWrite(led6Pin, LOW);
    digitalWrite(led1Pin, HIGH);
    delay(50);
    digitalWrite(led1Pin, LOW);
    digitalWrite(led2Pin, HIGH);
    delay(50);
    digitalWrite(led2Pin, LOW);
    digitalWrite(led3Pin, HIGH);
    delay(50);
    digitalWrite(led3Pin, LOW);
    digitalWrite(led4Pin, HIGH);
    delay(50);
    digitalWrite(led4Pin, LOW);
    digitalWrite(led5Pin, HIGH);
    delay(50);
    digitalWrite(led5Pin, LOW);
    digitalWrite(led6Pin, HIGH);
    delay(50);
    Serial.println ("lightMode4"); 
    Serial.println ("----------");
    Serial.println ("strobe "); 

  }   
}

}

You are using a simple for loop with a delay in it to make a fade. The 255 * 10 microseconds gives you a three second dead time when you can't do anything else. You need to look at the blink without delay technique and use it to make a fade without delay.

Thanks a lot Grumpy_Mike,
I did a quick google for blink without delay,
I tried to modify my code but still get the erratic behaviour but button press seems less random to respond

I will give myself another 24hours to assimilate the new code and hopefully find the solution by myself,
if I do I'll post the code, if I don't I'll come back and cry for help :),

Thanks guys,

        if (lightMode == 0) {          // if its off
          lightMode = 1;               // turn lights on!
        } else {
          if (lightMode == 1) {        // if its all-on
            lightMode = 2;             // make it blink!
          } else {
            if (lightMode == 2) {      // if its blinking
              lightMode = 3;           // make it wave!
            } else {
			  if (lightMode == 3) { //  if its fading, 
                lightMode = 4;           
              }else {
              	  if (lightMode == 4) { // turn light off! 
                lightMode = 0;    
              }

Pretty complex way of doing this:

  lightMode++;
  if(lightMode == 4)
    lightMode = 0;

Thanks a lot for your replies,
I tried both methods but Gammon's tip did the job,
the button responds perfectly and the fading is really smooth.
here is my code + fritzing,
hope I'm doing everything alright, it's my first project :slight_smile:

By the way, I've watched lots of soldering tutorials, but if anyone got a hidden gem,
please send it my way :stuck_out_tongue_winking_eye:

thanks

Fritzing:
http://www.neofuturism.net/work/baby_lights_D.fzz

Sketch:

/*
 * http://www.ladyada.net/learn/arduino/lesson5.html
 *  Bike light, final version
 *using the fading sketch from http://www.gammon.com.au/forum/?id=11411
 */
#include <LedFader.h>

int switchPin = 12;              // switch is connected to pin 2

// super fade without delay
LedFader led1Pin (2, 0, 200, 1000);
LedFader led2Pin (3, 0, 200,  1000);
LedFader led3Pin (4, 0, 200,  1000);
LedFader led4Pin (5, 0, 200,   1000);
LedFader led5Pin (6, 0, 200,   1000);
LedFader led6Pin (7, 0, 200,   1000);

//all the pin called again
int led11Pin = 2;
int led22Pin = 3;
int led33Pin = 4;
int led44Pin = 5;
int led55Pin = 6;
int led66Pin = 7;


int fadeValue = 0;
int val;                        // variable for reading the pin status
int val2;                       // variable for reading the delayed status
int buttonState;                // variable to hold the button state
int lightMode = 0;              // What mode is the light in?


void setup() {
  pinMode(switchPin, INPUT);    // Set the switch pin as input
  pinMode(led11Pin, OUTPUT);
  pinMode(led22Pin, OUTPUT);
  pinMode(led33Pin, OUTPUT);
  pinMode(led44Pin, OUTPUT);
  pinMode(led55Pin, OUTPUT);
  pinMode(led66Pin, OUTPUT);
  
  Serial.begin(9600);           // Set up serial communication at 9600bps
  buttonState = digitalRead(switchPin);   // read the initial state
 /*
* Strangely, this is of no use... 
  led1Pin.begin ();
  led2Pin.begin ();
  led3Pin.begin ();
  led4Pin.begin ();
  led5Pin.begin ();
  led6Pin.begin ();
  */
}

void loop(){
  
  
  val = digitalRead(switchPin);      // read input value and store it in val
  delay(10);                         // 10 milliseconds is a good amount of time
  val2 = digitalRead(switchPin);     // read the input again to check for bounces
  if (val == val2) {                 // make sure we got 2 consistant readings!
    if (val != buttonState) {          // the button state has changed!
      if (val == LOW) {                // check if the button is pressed
        if (lightMode == 0) {          // if its off
          lightMode = 1;               // turn lights on!
        } else {
          if (lightMode == 1) {        // if its all-on
            lightMode = 2;             // make it blink!
          } else {
            if (lightMode == 2) {      // if its blinking
              lightMode = 3;           // make it wave!
            } else {
			  if (lightMode == 3) { //  if its fading, 
                lightMode = 4;           
              }else {
              	  if (lightMode == 4) { // turn light off! 
                lightMode = 0;    
              }
			}
          }
        }
      }
    }
    buttonState = val;                 // save the new state in our variable
  }

  // Now do whatever the lightMode indicates
  if (lightMode == 0) { // all-off
    digitalWrite(led11Pin, LOW);
    digitalWrite(led22Pin, LOW);
    digitalWrite(led33Pin, LOW);
    digitalWrite(led44Pin, LOW);
    digitalWrite(led55Pin, LOW);
    digitalWrite(led66Pin, LOW);
    Serial.println ("lightMode");
    Serial.println ("----------");
    Serial.println ("buttonState"); 
 

  }

  if (lightMode == 1) { // all-on
    digitalWrite(led11Pin, HIGH);
    digitalWrite(led22Pin, HIGH);
    digitalWrite(led33Pin, HIGH);
    digitalWrite(led44Pin, HIGH);
    digitalWrite(led55Pin, HIGH);
    digitalWrite(led66Pin, HIGH);
    Serial.println ("lightMode1");
    Serial.println ("----------");
    Serial.println ("buttonState");  
  }

    if (lightMode == 2) { // blinking
    digitalWrite(led11Pin, HIGH);
    digitalWrite(led22Pin, HIGH);
    digitalWrite(led33Pin, HIGH);
    digitalWrite(led44Pin, HIGH);
    digitalWrite(led55Pin, HIGH);
    digitalWrite(led66Pin, HIGH);
    delay(100);
    digitalWrite(led11Pin, LOW);
    digitalWrite(led22Pin, LOW);
    digitalWrite(led33Pin, LOW);
    digitalWrite(led44Pin, LOW);
    digitalWrite(led55Pin, LOW);
    digitalWrite(led66Pin, LOW);
    delay(100);
    Serial.println ("lightMode2");
    Serial.println ("----------");
    Serial.println ("buttonState");  

  }
    if (lightMode == 3)  { // "fade"
  led1Pin.update ();
  led2Pin.update ();
  led3Pin.update ();
  led4Pin.update ();
  led5Pin.update ();
  led6Pin.update ();
  } 

   if (lightMode == 4)  { // "wave"
    digitalWrite(led66Pin, LOW);
    digitalWrite(led11Pin, HIGH);
    delay(50);
    digitalWrite(led11Pin, LOW);
    digitalWrite(led22Pin, HIGH);
    delay(50);
    digitalWrite(led22Pin, LOW);
    digitalWrite(led33Pin, HIGH);
    delay(50);
    digitalWrite(led33Pin, LOW);
    digitalWrite(led44Pin, HIGH);
    delay(50);
    digitalWrite(led44Pin, LOW);
    digitalWrite(led55Pin, HIGH);
    delay(50);
    digitalWrite(led55Pin, LOW);
    digitalWrite(led66Pin, HIGH);
    delay(50);
    Serial.println ("lightMode4"); 
    Serial.println ("----------");
    Serial.println ("buttonState"); 
    }   
  }

}

PaulS:
Pretty complex way of doing this:

  lightMode++;

if(lightMode == 4)
    lightMode = 0;

or more simply:

 lightMode = lightMode == 4 ? 0 : lightMode+1 ;

[ never be shy of the conditional operator, it powerful and succinct :slight_smile: ]

Or using the modulo operator to

  lightMode = (lightMode + 1) % 5 ;

The original code could have used an enum to name the modes and a switch clause to reduce
over-deep nesting - actually that's the best style really - more readable and less chance
of making an error when changing it in the future (names for states are human-friendly)

MarkT:
or more simply:

 lightMode = lightMode == 4 ? 0 : lightMode+1 ;

Or using the modulo operator to

  lightMode = (lightMode + 1) % 5 ;

Some find this error prone...

edit: fixed quoting

Ciao MarkT,
thanks for your reply,
when I tried your method, the sketch would go through all the loops by itself without stoping,
I've probably set it up wrong though, I'm do not understand the arduino language really well,
I may try it again tomorrow in another way to see if I can come up with the same results

Buona notte a tutti,