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)