[SOLVED] RGB LED and Servo Synchronization Issues

Howdy,

My project involves using a single switch state change to automate a 1/4" = 1' model between different "scenes." Using the servos to move walls, I want to have control over the lighting using RGB LEDS. Lastly, I want them to fade to the next state, instead of simply snapping on and off. The issue I have run into, is that in state change, the servo and LED function perfectly, but then the LED snaps back to it's prior state, only to repeat the fade. What confuses me even more, is that it only repeats once. Could anyone shed some light on this? Thank you!

Here is the original code for RGB LED fading by AdaFruit: http://ardx.org/RGBANA

I also used Korman's VarSpeedServo Library

// ***LED Color Blend Project***
//       Version 2.0

// Libraries
  #include <VarSpeedServo.h>
  
// Definitions
  VarSpeedServo Test; //attach servo
  
// Constants  
  const int buttonPin  = 2;
  const int ledPin     = 13;
  const int ledFadeTime = 10;
  const byte RED[] = {255, 0, 0}; 
  const byte ORANGE[] = {83, 4, 0}; 
  const byte YELLOW[] = {255, 255, 0}; 
  const byte GREEN[] = {0, 255, 0}; 
  const byte BLUE[] = {0, 0, 255}; 
  const byte INDIGO[] = {4, 0, 19}; 
  const byte VIOLET[] = {23, 0, 22}; 
  const byte CYAN[] = {0, 255, 255}; 
  const byte MAGENTA[] = {255, 0, 255}; 
  const byte WHITE[] = {255, 255, 255}; 
  const byte BLACK[] = {0, 0, 0}; 
  const byte PINK[] = {158, 4, 79};

// Variables
  int stateNum  =  0;
  int buttonState      = 0;
  int lastButtonState  = 0; 
  int ledState         = 0;
  int ledAnalogOne[] = {3, 5, 6};
 



void setup()
{ 
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
    for(int i = 0; i < 3; i++){
   pinMode(ledAnalogOne[i], OUTPUT);   //Set the three LED pins as outputs
  }
  setColor(ledAnalogOne, BLACK);
  Test.attach (10, 544, 2400);
  Test.slowmove (544, 120);
}



void loop(){
 
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
   
    if (buttonState == 1) {
      stateNum++;
      if(stateNum>4) stateNum=0;}

  if (stateNum==0) {
    ledState=0;
    fadeToColor(ledAnalogOne, ORANGE, BLACK, ledFadeTime);
    Test.slowmove (544, 10);
    delay(5000);
    ledState=1;}  
  
  if (stateNum==1) {
    ledState=0;
    fadeToColor(ledAnalogOne, BLACK, BLACK, ledFadeTime);
    Test.slowmove (800, 3);
    fadeToColor(ledAnalogOne, BLACK, BLUE, ledFadeTime);
    delay(5000);
    ledState=1;}
    
  if (stateNum==2) {
    ledState=0;
    fadeToColor(ledAnalogOne, BLUE, BLACK, ledFadeTime);
    Test.slowmove (1200, 4);
    fadeToColor(ledAnalogOne, BLACK, GREEN, ledFadeTime);
    delay(5000);
    ledState=1;}
    
  if (stateNum==3) {
    ledState=0;
    fadeToColor(ledAnalogOne, GREEN, BLACK, ledFadeTime);
    Test.slowmove (1800, 4);
    fadeToColor(ledAnalogOne, BLACK, RED, ledFadeTime);
    delay(5000);
    ledState=1;}
    
  if (stateNum==4) {
    ledState=0;
    fadeToColor(ledAnalogOne, RED, BLACK, ledFadeTime);
    Test.slowmove (2400, 4);
    fadeToColor(ledAnalogOne, BLACK, INDIGO, ledFadeTime);
    delay(5000);
    ledState=1;}

    lastButtonState = buttonState;}

  digitalWrite(ledPin, ledState);
  delay(20);
}



void setColor(int* led, byte* color){
   for(int i = 0; i < 3; i++){            
   analogWrite(led[i], 255 - color[i]);}
 }



void setColor(int* led, const byte* color){
  byte tempByte[] = {color[0], color[1], color[2]};
 }
 
 
//fades the RGB LED when given the name of a Color
void fadeToColor(int* led, const byte* startColor, const byte* endColor, int fadeSpeed){
  byte tempByte1[] = {startColor[0], startColor[1], startColor[2]};
  byte tempByte2[] = {endColor[0], endColor[1], endColor[2]};
  fadeToColor(led, tempByte1, tempByte2, fadeSpeed);
 }


//fades the RGB LED when given numerical parameters
void fadeToColor(int* led,  byte* startColor, byte* endColor, int fadeSpeed){
  int changeRed = endColor[0] - startColor[0];             
  int changeGreen = endColor[1] - startColor[1];                 
  int changeBlue = endColor[2] - startColor[2];                           
  int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue))); 
  
  for(int i = 0 ; i < steps; i++){                                        
    byte newRed = startColor[0] + (i * changeRed / steps);                 
    byte newGreen = startColor[1] + (i * changeGreen / steps);             
    byte newBlue = startColor[2] + (i * changeBlue / steps);               
    byte newColor[] = {newRed, newGreen, newBlue};                         
    delay(fadeSpeed);
    setColor(led, newColor);}
    
  setColor(led, endColor);                                               
}

VarSpeedServo.h (6.29 KB)

  int ledAnalogOne[] = {3, 5, 6};

Are these analog or digital pins. If there are digital pins, why is analog in the name?

In setup(), you define buttonPin as an input pin, but you don't activate the pull-up resistor. Since you are testing that the pressed value is HIGH, the assumption is that you have wired an external pull-down resistor. Is that a valid assumption?

  if (stateNum==0) {
  if (stateNum==1) {
  if (stateNum==2) {

Crappy placement of the { and }. Is there some way that more than one if block can be executed on any one pass through loop? If not, then you should be using "else if" for all but the first if test.

The issue I have run into, is that in state change, the servo and LED function perfectly, but then the LED snaps back to it's prior state, only to repeat the fade.

I don't understand what is happening. What does "in state change" mean?

Are these analog or digital pins. If there are digital pins, why is analog in the name?

These are analog pins. I'm using the PWM pins to color blend an RGB LED.

In setup(), you define buttonPin as an input pin, but you don't activate the pull-up resistor. Since you are testing that the pressed value is HIGH, the assumption is that you have wired an external pull-down resistor. Is that a valid assumption?

I have hard wired a resistor to my switch.

Crappy placement of the { and }. Is there some way that more than one if block can be executed on any one pass through loop? If not, then you should be using "else if" for all but the first if test.

I'll try switching out the code to else if, and see if that helps

The issue I have run into, is that in state change, the servo and LED function perfectly, but then the LED snaps back to it's prior state, only to repeat the fade.

I don't understand what is happening. What does "in state change" mean?

I meant every time the program enters an "if" statement with a button push. Each "if" statement is intended to move the servo, and fade the RGB LED to the next color selected. What actually happens is the servo moves, and the RGB LED fades to the desired color, but then the RGB LED repeats the fade script, causing it to temporarily snap back to it's previous color setting and fade to the desired color. It repeats the fade script once.

These are analog pins. I'm using the PWM pins to color blend an RGB LED.

PWM isn't done on analog pins. The analog pins are input only. PWM pins are digital pins that are turned on and off rapidly, at precisely defined intervals, to produce a less than 100% duty cycle, making the pins appear, to an LED at least, to be outputting less than 5V.

I meant every time the program enters an "if" statement with a button push.

The switch press does not cause the servo to move or the LED to change color. What it does do is cause the value in stateNum to change.

The value in stateNum is what defines the action that occurs on every pass through loop. It appears that you want the action to occur only when the switch is pressed and was not previously pressed. In which case, all the code needs to be in the inner block of this nested pair of if statements.

  if (buttonState != lastButtonState)
  {
    if (buttonState == 1)
    {
    }
  }

I've played with code and tried nesting it inside my if statements, but the response varies. Sometimes the switch works immediately, sometimes it takes up to a minute to move.

I've posted a video to give a better idea of what i'm trying to do.

I'm using 2 Futaba high torque 6v servos and 2 RGB LED connected in Parallel. Also using

The Latest code:

// ***LED Color Blend Project***
//       Version 2.0

// Libraries
  #include <VarSpeedServo.h>
  
// Definitions
  VarSpeedServo Turntable;      // 1/4" = 2400  1/2" = 544
  VarSpeedServo Stairs;         // OUT = 875    IN = 2245
  
// Constants  
  const int buttonPin  = 2;
  const int ledPin     = 13;
  const int ledFadeTime = 10;
  const byte RED[] = {255, 0, 0}; 
  const byte ORANGE[] = {83, 4, 0}; 
  const byte YELLOW[] = {255, 255, 0}; 
  const byte GREEN[] = {0, 255, 0}; 
  const byte BLUE[] = {0, 0, 255}; 
  const byte INDIGO[] = {4, 0, 19}; 
  const byte VIOLET[] = {23, 0, 22}; 
  const byte CYAN[] = {0, 255, 255}; 
  const byte MAGENTA[] = {255, 0, 255}; 
  const byte WHITE[] = {255, 255, 255}; 
  const byte BLACK[] = {0, 0, 0}; 
  const byte PINK[] = {158, 4, 79};

// Variables
  int stateNum  =  0;
  int buttonState      = 0;
  int lastButtonState  = 0; 
  int ledState         = 0;
  int ledAnalogOne[] = {3, 5, 6};
 



void setup()
{ 
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
    for(int i = 0; i < 3; i++){
   pinMode(ledAnalogOne[i], OUTPUT);   //Set the three LED pins as outputs
  }
  Turntable.attach (10, 544, 2400);
  Stairs.attach (9, 544, 2400);
  Stairs.slowmove (875, 255);
  delay (750);
  Turntable.slowmove (544, 255);
  delay (750);
  Stairs.slowmove (2245, 255);
  setColor(ledAnalogOne, BLACK);
}



void loop(){
 
  buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
   
    if (buttonState == 1) {
      
       if (stateNum==0) {
        ledState=0;
        fadeToColor(ledAnalogOne, BLACK, BLACK, ledFadeTime);
        Turntable.slowmove (544, 10);
        delay(17800);
        Stairs.slowmove (2245, 50);}
        ledState=1;}  
  
        else if (stateNum==1) {
          ledState=0;
          Turntable.slowmove (800, 3);
          Stairs.slowmove (875, 100);
          fadeToColor(ledAnalogOne, BLACK, BLACK, ledFadeTime);
          fadeToColor(ledAnalogOne, BLACK, BLUE, ledFadeTime);
          ledState=1;}
    
        else if (stateNum==2) {
          ledState=0;
          Turntable.slowmove (1200, 4);
          fadeToColor(ledAnalogOne, BLUE, BLACK, ledFadeTime);
          fadeToColor(ledAnalogOne, BLACK, GREEN, ledFadeTime);
          ledState=1;}
    
        else if (stateNum==3) {
          ledState=0;
          Turntable.slowmove (1800, 4);
          fadeToColor(ledAnalogOne, GREEN, BLACK, ledFadeTime);
          fadeToColor(ledAnalogOne, BLACK, RED, ledFadeTime);
          ledState=1;}
    
        else if (stateNum==4) {
          ledState=0;
          Turntable.slowmove (2400, 4);

          ledState=1;}
      
        
      stateNum++;
      if(stateNum>5) stateNum=0;}

    lastButtonState = buttonState;

  digitalWrite(ledPin, ledState);
  delay(20);
}



void setColor(int* led, byte* color){
   for(int i = 0; i < 3; i++){            
   analogWrite(led[i], 255 - color[i]);}
 }



void setColor(int* led, const byte* color){
  byte tempByte[] = {color[0], color[1], color[2]};
 }
 
 
 
void fadeToColor(int* led, const byte* startColor, const byte* endColor, int fadeSpeed){
  byte tempByte1[] = {startColor[0], startColor[1], startColor[2]};
  byte tempByte2[] = {endColor[0], endColor[1], endColor[2]};
  fadeToColor(led, tempByte1, tempByte2, fadeSpeed);
 }



void fadeToColor(int* led,  byte* startColor, byte* endColor, int fadeSpeed){
  int changeRed = endColor[0] - startColor[0];             
  int changeGreen = endColor[1] - startColor[1];                 
  int changeBlue = endColor[2] - startColor[2];                           
  int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue))); 
  
  for(int i = 0 ; i < steps; i++){                                        
    byte newRed = startColor[0] + (i * changeRed / steps);                 
    byte newGreen = startColor[1] + (i * changeGreen / steps);             
    byte newBlue = startColor[2] + (i * changeBlue / steps);               
    byte newColor[] = {newRed, newGreen, newBlue};                         
    delay(fadeSpeed);
    setColor(led, newColor);}
    
  setColor(led, endColor);                                               
}
delay(17800);

I can't imagine that sort of thing doing much to improve responsiveness.

Well, I'm at a loss at what to do now. I've been working the code and can't seem to get what I want out of it.

Essentially, I would like every time I press and release one button, a different action to happen. Ideally, 5-10 different actions before it loops back to the first action.

The action should consist of 2 independent servos moving, and the RGB LED array to fade to black from it's previous color, then fade from Black to it's next Color.

My current code seems to get tangled in the if statements and the fade of the RGB Led void Script. I think I need a complete restructuring of the code to function the way I want.

Does anyone have any ideas?

Latest Version

/* 

***LED Color Blend Project***
          Version 3.0
          4/20/2011
          
Developed by Jeff D.
          
*/
// Libraries
  #include <VarSpeedServo.h>
  
// Definitions
  VarSpeedServo Turntable;      // 1/4" = 2400  1/2" = 544
  VarSpeedServo Stairs;         // OUT = 875    IN = 2245
  
// Constants  
  const int buttonPin  = 2;
  const int ledPin     = 13;
  const int ledFadeTime = 50;
  
  // Colors
    const byte RED[] = {255, 0, 0}; 
    const byte ORANGE[] = {83, 4, 0}; 
    const byte YELLOW[] = {255, 255, 0}; 
    const byte GREEN[] = {0, 255, 0}; 
    const byte BLUE[] = {0, 0, 255}; 
    const byte INDIGO[] = {4, 0, 19}; 
    const byte VIOLET[] = {23, 0, 22}; 
    const byte CYAN[] = {0, 255, 255}; 
    const byte MAGENTA[] = {255, 0, 255}; 
    const byte WHITE[] = {255, 255, 255}; 
    const byte BLACK[] = {0, 0, 0}; 
    const byte PINK[] = {158, 4, 79};

// Variables
  int stateNum  =  0;
  int buttonState      = 0;
  int lastButtonState  = 0; 
  int ledState         = 0;
  int RGBLed[] = {3, 5, 6};
 


// Single Fire Program
void setup()
{ 
   for(int i = 0; i < 3; i++){
  pinMode(RGBLed[i], OUTPUT);   //Set the three LED pins as outputs
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
 
  }
  Turntable.attach (10, 544, 2400);
  Stairs.attach (9, 544, 2400);
  
  Stairs.slowmove (875, 255);
  delay (750);
  Turntable.slowmove (2400, 255);
  setColor(RGBLed, BLACK);
}


// Looped Program
void loop(){
 
  buttonState = digitalRead(buttonPin);
  
  if (buttonState != lastButtonState) {
   
    if (buttonState == 1) {
      
       if (stateNum==0) {
        ledState=0;
        digitalWrite(ledPin, ledState);
        setColor(RGBLed, BLACK);
        Turntable.slowmove (544, 10);
        delay (17800);
        Stairs.slowmove (2245, 50);}
        ledState=1;
        digitalWrite(ledPin, ledState);}  
  
        else if (stateNum==1) {
          ledState=0;
          setColor (RGBLed, BLACK);
          Stairs.slowmove (875, 100);
          Turntable.slowmove (800, 3);
          fadeToColor(RGBLed, BLACK, BLUE, ledFadeTime);
          ledState=1;}
    
        else if (stateNum==2) {
          ledState=0;
          setColor (RGBLed, BLUE);
          fadeToColor(RGBLed, BLUE, BLACK, ledFadeTime);
          Turntable.slowmove (1300, 4);
          fadeToColor(RGBLed, BLACK, GREEN, ledFadeTime);
          ledState=1;}
    
        else if (stateNum==3) {
          ledState=0;
          setColor (RGBLed, GREEN);
          fadeToColor(RGBLed, GREEN, BLACK, ledFadeTime);
          Turntable.slowmove (1800, 4);
          fadeToColor(RGBLed, BLACK, RED, ledFadeTime);
          ledState=1;}
    
        else if (stateNum==4) {
          ledState=0;
          setColor (RGBLed, RED);
          fadeToColor (RGBLed, RED, BLACK, ledFadeTime);
          Turntable.slowmove (2400, 4);
          ledState=1;}
         
      stateNum++;
      if(stateNum>5) stateNum=0;}

    lastButtonState = buttonState;

  digitalWrite(ledPin, ledState);
  delay(20);
}



// Void Commands
void setColor(int* led, byte* color){
   for(int i = 0; i < 3; i++){            
   analogWrite(led[i], 255 - color[i]);}
 }



void setColor(int* led, const byte* color){
  byte tempByte[] = {color[0], color[1], color[2]};
 }
 
 
 
void fadeToColor(int* led, const byte* startColor, const byte* endColor, int fadeSpeed){
  byte tempByte1[] = {startColor[0], startColor[1], startColor[2]};
  byte tempByte2[] = {endColor[0], endColor[1], endColor[2]};
  fadeToColor(led, tempByte1, tempByte2, fadeSpeed);
 }



void fadeToColor(int* led,  byte* startColor, byte* endColor, int fadeSpeed){
  int changeRed = endColor[0] - startColor[0];             
  int changeGreen = endColor[1] - startColor[1];                 
  int changeBlue = endColor[2] - startColor[2];                           
  int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue))); 
  
  for(int i = 0 ; i < steps; i++){                                        
    byte newRed = startColor[0] + (i * changeRed / steps);                 
    byte newGreen = startColor[1] + (i * changeGreen / steps);             
    byte newBlue = startColor[2] + (i * changeBlue / steps);               
    byte newColor[] = {newRed, newGreen, newBlue};                         
    delay(fadeSpeed);
    setColor(led, newColor);}
    
  setColor(led, endColor);                                               
}

My current code seems to get tangled in the if statements and the fade of the RGB Led void Script.

I love a good technical description of a problem. :slight_smile:

Too bad this one isn't.

Putting each { and } on a separate line, and properly indenting the code between them will help a lot with seeing the structure of the program.

When you have more than about 3 cases (stateNum == 0, stateNum == 1, stateNum == 2, etc.), and switch statement is easier to follow, and produces smaller code.

You can use Serial.begin() in setup(), and Serial.print() or Serial.println() in loop(), to see what values variables have, and where in the code control is getting to. Add some debug stuff to figure out what is happening.

Thank you for your help and patience.

I'll continue to plug away at it and let you know.

I've seen worse code on this forum, :roll_eyes: your's isnt so bad, crashoverride61088. And I do love you giving us a whole video of everything. :slight_smile: You clearly are good at getting a good finish on things (my problem is the opposite - my code works, but my hardware isn't pretty)

Still, a few general pointers on the software is what you're asking for.

One possible flaw is you do not debounce the switch. As your events (state actions) usually involve lots of delay, this masks that fact, but I wonder (as I have not studied the code in detail) if you might skip between states? (which is why a Serial.print at each state beginning might help)

Your slow colourfade is stuck doing that (and nothing else). Once started no other changes will happen. This may be intentional or not, but your button will be 100% unresponsive while a fade is progressing. I have not used Korman's VarSpeedServo library so I do not know if that has the same problem. (unrelated note: I have made my own "slow" servo move for a robot arm, works, but didn't realise a library might have saved me the hassle, will go and try that as soon as this post is done :blush: )

Hope that gave some inspiration

I've tried to change coding tactics.

On PaulS suggestion, I've switched over to switch/case, while using a if statement to track the button pushes.

The 'if' statement to track the button functions perfectly, but I do not know how to keep the switch/case from repeating itself. Any thoughts?

I have Serial.println statements placed when the programs begins a Case, when a RGBLed fadeToColor is complete, and inside the 'if' statement to count the button pushes (taken from the learning section on Arudino site).

Here's what I get in the serial monitor:

on
Number of Button Pushes:  1
case 1
color change complete
off
case 1
color change complete
on
Number of Button Pushes:  2
case 2
color change complete
color change complete
off
case 2
color change complete
color change complete
on
Number of Button Pushes:  3
case 3
color change complete
color change complete
off
case 3
color change complete
color change complete
on
Number of Button Pushes:  4
case 4
color change complete
color change complete
off
case 4
color change complete
color change complete
on
Number of Button Pushes:  5
case 5
color change complete
off
case 5
color change complete
on
Number of Button Pushes:  6
case 0
off
case 0
on
Number of Button Pushes:  1
case 1
color change complete
off
case 1
color change complete

Here is the code

/* 

***Model House with RGB Led and Servo Control***
              Version 1.0
              4/20/2011
          
Developed by Jeff D.
          
*/
// Libraries
  #include <VarSpeedServo.h>
  
// Definitions
  VarSpeedServo Turntable;      // 1/4" = 2400  1/2" = 544
  VarSpeedServo Stairs;         // OUT = 875    IN = 2245
  
// Constants  
  const int buttonPin  = 2;
  const int ledPin     = 13;
  const int ledFadeTime = 5;
  
// Colors
  const byte RED[] = {255, 0, 0}; 
  const byte ORANGE[] = {83, 4, 0}; 
  const byte YELLOW[] = {255, 255, 0}; 
  const byte GREEN[] = {0, 255, 0}; 
  const byte BLUE[] = {0, 0, 255}; 
  const byte INDIGO[] = {4, 0, 19}; 
  const byte VIOLET[] = {23, 0, 22}; 
  const byte CYAN[] = {0, 255, 255}; 
  const byte MAGENTA[] = {255, 0, 255}; 
  const byte WHITE[] = {255, 255, 255}; 
  const byte BLACK[] = {0, 0, 0}; 
  const byte PINK[] = {158, 4, 79};

// Variables
  int lastButtonState = 0;
  int buttonState      = 0;
  int buttonPushCounter = 0;
  int ledState         = 0;
  int RGBLed[] = {3, 5, 6};
 
void setup() { 
  
  for(int i = 0; i < 3; i++) 
  pinMode(RGBLed[i], OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Turntable.attach (10, 544, 2400);
  Stairs.attach (9, 544, 2400);
  
  
  //Reset Servo Positions and RGB Leds
  Stairs.slowmove (875, 255);
  delay (750);
  Turntable.slowmove (2400, 255);
  setColor (RGBLed, BLACK);
  
  //Debug
  Serial.begin(9600);
}


// Program
void loop() {
 
  int buttonState = digitalRead(buttonPin); 
  if (buttonState != lastButtonState) {

    if (buttonState == HIGH) {

      buttonPushCounter++;
      Serial.println("on");
      Serial.print("Number of Button Pushes:  ");
      Serial.println(buttonPushCounter, DEC);   
      if(buttonPushCounter > 5) {
        buttonPushCounter = 0;
      }
    }
    
    else {
      Serial.println("off");
    }
    
  lastButtonState = buttonState;
  
  //cases
  switch (buttonPushCounter) {
   
    case 0:
      Serial.println("case 0");
      setColor(RGBLed, BLACK);
      Stairs.slowmove (875, 30);
      delay (1000);
      Turntable.slowmove(2400, 10);
      break;
      
    case 1:
      Serial.println("case 1");
      setColor(RGBLed, BLACK);
      Turntable.slowmove(2000, 10);
      fadeToColor(RGBLed, BLACK, BLUE, ledFadeTime);
      break;
      
    case 2:
      Serial.println("case 2");
      Turntable.slowmove(1500, 10);
      fadeToColor(RGBLed, BLUE, BLACK, ledFadeTime);
      fadeToColor(RGBLed, BLACK, RED, ledFadeTime); 
      break;
      
    case 3:
      Serial.println("case 3");
      Turntable.slowmove(1000, 10);
      fadeToColor(RGBLed, RED, BLACK, ledFadeTime);
      fadeToColor(RGBLed, BLACK, GREEN, ledFadeTime);  
      break;
      
    case 4:
      Serial.println("case 4");
      Turntable.slowmove(700, 10);
      fadeToColor(RGBLed, GREEN, BLACK, ledFadeTime);
      fadeToColor(RGBLed, BLACK, INDIGO, ledFadeTime); 
      break;
      
    case 5:
      Serial.println("case 5");
      Turntable.slowmove(544, 10);
      fadeToColor(RGBLed, INDIGO, BLACK, ledFadeTime);
      delay(3000); //wait for Turntable to clear
      Stairs.slowmove (2245, 10);
      break;
}

  digitalWrite(ledPin, ledState);
  delay(20);
}
}


// Void Commands
void setColor(int* led, byte* color) {
  for(int i = 0; i < 3; i++) {            
    analogWrite(led[i], 255 - color[i]);
}
}


void setColor(int* led, const byte* color) {
  byte tempByte[] = {color[0], color[1], color[2]};
   setColor(led, tempByte);
 }
 
 
 
void fadeToColor(int* led, const byte* startColor, const byte* endColor, int fadeSpeed) {
  byte tempByte1[] = {startColor[0], startColor[1], startColor[2]};
  byte tempByte2[] = {endColor[0], endColor[1], endColor[2]};
  fadeToColor(led, tempByte1, tempByte2, fadeSpeed);
 }



void fadeToColor(int* led,  byte* startColor, byte* endColor, int fadeSpeed) {
  int changeRed = endColor[0] - startColor[0];             
  int changeGreen = endColor[1] - startColor[1];                 
  int changeBlue = endColor[2] - startColor[2];                           
  int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue))); 
  
  for(int i = 0 ; i < steps; i++) {    
    byte newRed = startColor[0] + (i * changeRed / steps);                 
    byte newGreen = startColor[1] + (i * changeGreen / steps);             
    byte newBlue = startColor[2] + (i * changeBlue / steps);               
    byte newColor[] = {newRed, newGreen, newBlue};                         
    delay(fadeSpeed);
    setColor(led, newColor);}
    
  setColor(led, endColor);
Serial.println ("color change complete");  
}

Msquare,

Thanks for the feedback.

One possible flaw is you do not debounce the switch. As your events (state actions) usually involve lots of delay, this masks that fact, but I wonder (as I have not studied the code in detail) if you might skip between states? (which is why a Serial.print at each state beginning might help)

This might have been occurring, but I think I've eliminated it by switching to switch/case, allowing me to simplify my 'if' statements. While on the subject though, what is debouncing the switch?

Your slow colourfade is stuck doing that (and nothing else). Once started no other changes will happen. This may be intentional or not, but your button will be 100% unresponsive while a fade is progressing

This effect is not an issue at this time, but may be in the foreseeable future. How would I solve this?

The 'if' statement to track the button functions perfectly, but I do not know how to keep the switch/case from repeating itself. Any thoughts?

Do you want the action that is performed in each state to be performed only once, when the state changes?

If that is the case, more the whole switch statement inside the if(buttonState == HIGH) block.

Nice job on the reformatting, by the way. The structure of the code is much easier to see. NOT.

The 'if' statement to track the button functions perfectly, but I do not know how to keep the switch/case from repeating itself. Any thoughts?

Do you want the action that is performed in each state to be performed only once, when the state changes?

If that is the case, more the whole switch statement inside the if(buttonState == HIGH) block

This worked. It now functions perfectly. Thank you for your help.

Final Code

/* 

***Model House with RGB Led and Servo Control***
              Version 1.0
              4/20/2011
          
Developed by Jeff D.
          
*/
// Libraries
  #include <VarSpeedServo.h>
  
// Definitions
  VarSpeedServo Turntable;      // 1/4" = 2400  1/2" = 544
  VarSpeedServo Stairs;         // OUT = 875    IN = 2245
  
// Constants  
  const int buttonPin  = 2;
  const int ledPin     = 13;
  const int ledFadeTime = 5;
  
// Colors
  const byte RED[] = {255, 0, 0}; 
  const byte ORANGE[] = {83, 4, 0}; 
  const byte YELLOW[] = {255, 255, 0}; 
  const byte GREEN[] = {0, 255, 0}; 
  const byte BLUE[] = {0, 0, 255}; 
  const byte INDIGO[] = {4, 0, 19}; 
  const byte VIOLET[] = {23, 0, 22}; 
  const byte CYAN[] = {0, 255, 255}; 
  const byte MAGENTA[] = {255, 0, 255}; 
  const byte WHITE[] = {255, 255, 255}; 
  const byte BLACK[] = {0, 0, 0}; 
  const byte PINK[] = {158, 4, 79};

// Variables
  int lastButtonState = 0;
  int buttonState      = 0;
  int buttonPushCounter = 0;
  int ledState         = 0;
  int RGBLed[] = {3, 5, 6};
 
void setup() { 
  
  for(int i = 0; i < 3; i++) 
  pinMode(RGBLed[i], OUTPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  Turntable.attach (10, 544, 2400);
  Stairs.attach (9, 544, 2400);
  
  
  //Reset Servo Positions and RGB Leds
  Stairs.slowmove (875, 255);
  delay (750);
  Turntable.slowmove (2400, 255);
  setColor (RGBLed, BLACK);
  
  //Debug
  Serial.begin(9600);
}


// Program
void loop() {
 
  int buttonState = digitalRead(buttonPin); 
  if (buttonState != lastButtonState) {

    if (buttonState == HIGH) {

      buttonPushCounter++;
      Serial.println("on");
      Serial.print("Number of Button Pushes:  ");
      Serial.println(buttonPushCounter, DEC);
      switch (buttonPushCounter) {
   
    case 0:
      Serial.println("case 0");
      setColor(RGBLed, BLACK);
      Stairs.slowmove (875, 30);
      delay (1000);
      Turntable.slowmove(2400, 10);
      break;
      
    case 1:
      Serial.println("case 1");
      setColor(RGBLed, BLACK);
      Turntable.slowmove(2000, 10);
      fadeToColor(RGBLed, BLACK, BLUE, ledFadeTime);
      break;
      
    case 2:
      Serial.println("case 2");
      Turntable.slowmove(1500, 10);
      fadeToColor(RGBLed, BLUE, BLACK, ledFadeTime);
      fadeToColor(RGBLed, BLACK, RED, ledFadeTime); 
      break;
      
    case 3:
      Serial.println("case 3");
      Turntable.slowmove(1000, 10);
      fadeToColor(RGBLed, RED, BLACK, ledFadeTime);
      fadeToColor(RGBLed, BLACK, GREEN, ledFadeTime);  
      break;
      
    case 4:
      Serial.println("case 4");
      Turntable.slowmove(700, 10);
      fadeToColor(RGBLed, GREEN, BLACK, ledFadeTime);
      fadeToColor(RGBLed, BLACK, INDIGO, ledFadeTime); 
      break;
      
    case 5:
      Serial.println("case 5");
      Turntable.slowmove(544, 10);
      fadeToColor(RGBLed, INDIGO, BLACK, ledFadeTime);
      delay(2000); //wait for Turntable to clear
      Stairs.slowmove (2245, 10);
      break;
}  
      if(buttonPushCounter > 5) {
        buttonPushCounter = 0;
      }
    }
    
    else {
      Serial.println("off");
    }
    
  lastButtonState = buttonState;
  digitalWrite(ledPin, ledState);
  delay(200);
}
}


// Void Commands
void setColor(int* led, byte* color) {
  for(int i = 0; i < 3; i++) {            
    analogWrite(led[i], 255 - color[i]);
}
}


void setColor(int* led, const byte* color) {
  byte tempByte[] = {color[0], color[1], color[2]};
   setColor(led, tempByte);
 }
 
 
 
void fadeToColor(int* led, const byte* startColor, const byte* endColor, int fadeSpeed) {
  byte tempByte1[] = {startColor[0], startColor[1], startColor[2]};
  byte tempByte2[] = {endColor[0], endColor[1], endColor[2]};
  fadeToColor(led, tempByte1, tempByte2, fadeSpeed);
 }



void fadeToColor(int* led,  byte* startColor, byte* endColor, int fadeSpeed) {
  int changeRed = endColor[0] - startColor[0];             
  int changeGreen = endColor[1] - startColor[1];                 
  int changeBlue = endColor[2] - startColor[2];                           
  int steps = max(abs(changeRed),max(abs(changeGreen), abs(changeBlue))); 
  
  for(int i = 0 ; i < steps; i++) {    
    byte newRed = startColor[0] + (i * changeRed / steps);                 
    byte newGreen = startColor[1] + (i * changeGreen / steps);             
    byte newBlue = startColor[2] + (i * changeBlue / steps);               
    byte newColor[] = {newRed, newGreen, newBlue};                         
    delay(fadeSpeed);
    setColor(led, newColor);}
    
  setColor(led, endColor);
Serial.println ("color change complete");  
}