Fading leds in an existing code.

Hi, first time user here. I'm learning all about arduino.
I'm building an iron man suit and i'm now busy with putting electronics in my helmet.
The code I got from theRPF works like a charm! I made some small allterations to suit my needs.
The code fades the leds on and turns the servo's and shuts the faceplate. Now I would like the leds to fade out when the
button is pressed and the faceplate opens. Now it just turns off. I don't now how and where to add the code.
Thanks for your help.
Here's my scetch:

 #include <Servo.h>
//servo 1
Servo myservo;
Servo myservo1;
int val; // variable for reading the pin status
int val2; // variable for reading the delayed/debounced status
int buttonState;
int pos = 40;
int pos1 = 140;
int servostatus = 0;
int switchPin =2; // Switch connected to digital pin 2
int ledPin = 5;
int ledPin2 = 18;
void setup() // run once, when the sketch starts
{
  //servo 1

  myservo.attach(9);
  myservo1.attach(10);
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
  buttonState = digitalRead(switchPin);
  myservo.write(40);
  myservo1.write(140);
  pinMode(ledPin2, OUTPUT);
}

void loop() // run over and over again

//servo 1
{
  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 (servostatus == 0) { // is the light off?
          servostatus = 1; // turn light on!

          myservo.write(40);
          myservo1.write(140);
          delay(50);
          digitalWrite(ledPin, HIGH);
          delay(50);
          digitalWrite(ledPin, LOW);
          delay(200);
          digitalWrite(ledPin, HIGH);
          delay(100);
          digitalWrite(ledPin, LOW);
          delay(100);
          digitalWrite(ledPin, HIGH);
          delay(250);
          digitalWrite(ledPin, LOW);
          delay(50);


          // fading
          for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
            // sets the value (range from 0 to 255):
            analogWrite(ledPin, fadeValue);
            delay(30);


          }

        } 
        else {
          servostatus = 0; // turn light off!


          digitalWrite(ledPin, LOW);
          delay(15);
          digitalWrite(ledPin2, LOW);
          myservo.write(140);
          myservo1.write(40);






        }
      }
    }
    buttonState = val; // save the new state in our variable
  }
}

Please format your code in the arduino software with CTRL+T and put your code in code tags. Look for the # above the smiley faces.

Delays block the code from doing anything else until they are done, so if you have a large sketch, those delays are going to bog it down to a crawl. Look into the Blink Without Delay example sketch to fix those delays.

OK, about your question.
This fades the LEDs to full brightness, by incrementing the fadeValue by 5, so what would you need to change to make the LEDs fade to fully off?

for(int fadeValue = 0 ; fadeValue <= 255; fadeValue +=5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin, fadeValue);

Two other things, this is a poor way to debounce a button, yes it does somewhat work, but there is a better way. You have an example sketch that does just that.

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

And this can be changed to use a latch, so if the button is pressed once, face plate opens, press button again, face plate closes.

if (servostatus == 0) { // is the light off?
servostatus = 1; // turn light on!

I will edit the first post tomorow when I am behind my computer. :wink:
Thanks for your help. Not sure if I understand it all, but going to do some reading.
Thanks.

Servo myservo;
Servo myservo1;

Don't these servos do something that would lead you to come up with better names? If you have kids, do you (or did you) name them mykid and mykid2?

After trying to alter the code in the first post,I switched to the code I used first.
I think this code might be better. As HazardsMind pointed out to me the code from the first post has a lot of delays.
The code i have now has 1 or 2 delays,it uses the internal pullup resistor and it has the fade off I want.
Credit goes to xl97 from the RPF Forum
Here's the sketch:

// IronMan Helmet: eye blink sequence_v1.0
// created by: xl97


//import servo lib
#include <Servo.h>

//servo object names
Servo myservo; // create servo object to control a servo
Servo myservo1;

const int buttonPin = 2; // the pin that the pushbutton is attached to
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button


// led control pins (need to be PWM enabled pins for fading)
const int leftEye =  6;  // the number of the left eye/pcb LEDs
const int rightEye =  3;  // the number of the right eye/pcb LEDs

unsigned long fadeDelay = .5; //speed of the eye 'fade'
unsigned long callDelay = 700; //length to wait to start eye flicker after face plate comes down
unsigned long blinkSpeed = 100; //delay between init blink on/off
unsigned long currentPWM = 0;
boolean isOpen = true;

#define S_IDLE 1
#define S_LEDON 2
#define S_WAITON 3
#define S_LEDOFF 4
#define S_WAITOFF 5
#define S_INITON 6
#define S_INITWAIT 7
#define S_BLINKON 8
#define S_SERVOUP 9
#define S_SERVODOWN 0
#define S_SERVOWAIT 10



//FSM init vars
static int state = S_IDLE; // initial state is 1, the "idle" state.
static unsigned long lastTime;  // To store the "current" time in for delays.


void setup() {
  // Set up serial port
  Serial.begin(9600);  
  //start it off
  //state = S_BLINKON; 
  Serial.print("INTIT STATE: ");
  Serial.println(state);

  myservo.attach(9); // attaches the servo on pin 9 to the servo object
  myservo1.attach(10); // attaches the servo on pin 10 to the servo object

  pinMode(buttonPin, INPUT); // initialize the button pin as a input
  digitalWrite(buttonPin, HIGH); //use interal pull up resistors
}

void loop() { 
  switch(state)
  {
  case S_IDLE:
    // We don't need to do anything here, waiting for a forced state change...like button press.
    //check mian button state
    buttonState = digitalRead(buttonPin);

    // compare buttonState to previous state  
    if (buttonState != lastButtonState) {
      //if button pressed/down
      if (buttonState == LOW){
        //ie: pressed   
        if(isOpen == true){
          Serial.print("CLOSING FACE PLATE: ");
          Serial.println(isOpen, DEC); 
          state = S_SERVODOWN;
        }
        else{
          Serial.print("OPENING FACE PLATE: ");
          Serial.println(isOpen, DEC); 
          //state = S_SERVOUP;
          state = S_LEDOFF;
        }
        isOpen = !isOpen;
      } 
      else{
        //went from ON/HIGH to LOW/OFF..ie: released
        //Serial.print("RELEASE: ");
        //Serial.println(isOpen, DEC); 
      } 
    }
    // save the current state for next loop
    lastButtonState = buttonState;
    break;

  case S_BLINKON:
    Serial.println("init blink.........");   
    //do blink routine here
    //one blink
    analogWrite(leftEye, 155);
    analogWrite(rightEye, 155);
    delay(blinkSpeed);
    analogWrite(leftEye, 0);
    analogWrite(rightEye, 0);
    delay(10);
    //two blinks
    /*
    analogWrite(leftEye, 155);
     analogWrite(rightEye, 155);
     delay(blinkSpeed);
     analogWrite(leftEye, 0);
     analogWrite(rightEye, 0);
     delay(10);
     */
    state = S_LEDON;    
    break;

  case S_LEDON:
    Serial.println("increase........");    
    lastTime = millis();  // Remember the current time
    analogWrite(leftEye, currentPWM);
    analogWrite(rightEye, currentPWM);
    state = S_WAITON;  // Move to the next state
    break;

  case S_WAITON:
    // If one second has passed, then move on to the next state.
    if(millis() > (lastTime + fadeDelay)){
      if(currentPWM < 255){
        currentPWM += 5;
        state = S_LEDON;        
      }
      else{
        Serial.println("@ 255 done........");
        state = S_IDLE;
        //state = S_LEDOFF; //no auto turn off.. set to idle state
      }
    }
    break;

  case S_LEDOFF:
    Serial.println("........decrease");     
    lastTime = millis();  // Remember the current time
    analogWrite(leftEye, currentPWM);
    analogWrite(rightEye, currentPWM);
    state = S_WAITOFF;
    break;

  case S_WAITOFF:
    // If one second has passed, then move on to the next state.
    if(millis() > (lastTime + fadeDelay)){
      if(currentPWM > 0){  //change 0 to higher number to init face 'up' function sooner.
        currentPWM -= 5;
        state = S_LEDOFF;        
      }
      else{
        Serial.println("@ 0 done........");
        state = S_SERVOUP; //leds off..raise faceplate
      }
    }
    break; 

  case S_SERVOUP:
    Serial.println("servo up........."); 
    myservo.write(20);
    myservo1.write(20);
    state = S_IDLE;    
    break;

  case S_SERVODOWN:
    lastTime = millis();  // Remember the current time
    Serial.println("servo down.........");   
    myservo.write(100);
    myservo1.write(100);
    state = S_SERVOWAIT;    
    break;

  case S_SERVOWAIT:
    // If enough time has passed, call the eye flicker routine
    if(millis() > (lastTime + callDelay)){   
      Serial.println("start eye flicker routine");
      state = S_BLINKON;        
    }
    else{
      Serial.println("waiting........");
    }
    break;

  default:
    state = S_IDLE;
    break;
  } 
}

This code works great except it does not run the servo's in mirror.
So i changed:

  case S_SERVOUP:
    Serial.println("servo up........."); 
    myservo.write(20);
    myservo1.write(20);
    state = S_IDLE;    
    break;

  case S_SERVODOWN:
    lastTime = millis();  // Remember the current time
    Serial.println("servo down.........");   
    myservo.write(100);
    myservo1.write(100);
    state = S_SERVOWAIT;    
    break;

to

  case S_SERVOUP:
    Serial.println("servo up........."); 
    myservo.write(20);
    myservo1.write(100);
    state = S_IDLE;    
    break;

  case S_SERVODOWN:
    lastTime = millis();  // Remember the current time
    Serial.println("servo down.........");   
    myservo.write(100);
    myservo1.write(20);
    state = S_SERVOWAIT;    
    break;

Now when I disconnect the battery pack and reconnect it ,1 of the servo's turns to a starting point.
I guess the code does tell it exactly what it supposed to do.
My question is where or how can I alter the code the have the servo's start at the same point.
Thank you.