Controlling RGB LED and Servo Using Potentiometer

Hi people,
i am trying to control RGB led and Servo with two different photometers,problem is once include Servo control function servo jitters and led goes to unknown blue color state.
my question whether using two different pot for controlling two different function causing problem!!!?


#include <Servo.h>
int potPin = 1;
int servoPin = 6;
Servo servo;
int trim = A0 ;
int val = 0 ;
int pinButton = 8;
int Relay = 2;
int stateRelay = HIGH;
int stateButton;
int previous = LOW;
int pinButton1 = 7;
int Relay1 = 4;
int stateRelay1 = HIGH;
int stateButton1;
int previous1 = LOW;
int redPin = 9 ;
int greenPin = 11 ;
int bluePin = 10 ;
long time = 0;
long debounce =3000;

void setup()
{
servo.attach(servoPin);
pinMode(pinButton, INPUT);
pinMode(pinButton1, INPUT);
pinMode(Relay, OUTPUT);
pinMode(Relay1, OUTPUT);
pinMode( redPin, OUTPUT ) ;
pinMode( greenPin, OUTPUT ) ;
pinMode( bluePin, OUTPUT ) ;

}

void loop()
{

{

stateButton = digitalRead(pinButton);
if(stateButton == HIGH && previous == LOW && millis() - time > debounce)
{
if(stateRelay == HIGH)
{
stateRelay = LOW;
}
else
{
stateRelay = HIGH;
}
time = millis();

}
digitalWrite(Relay, stateRelay);
previous == stateButton;
}
{

stateButton1 = digitalRead(pinButton1);
if(stateButton1 == HIGH && previous1 == LOW && millis() - time > debounce) {
if(stateRelay1 == HIGH){
stateRelay1 = LOW;
} else
{
stateRelay1 = HIGH;
}
time = millis();

}
digitalWrite(Relay1, stateRelay1);
previous1 == stateButton1;
}

val = analogRead( trim ) ;
Serial.println(val) ;
if( val < 100 )
{
setColor( 220, 0, 0 ) ;
}
if( val >= 200 )
{
if( val < 300 )
{
setColor( 0, 255, 100 ) ;
}
}
if( val > 300 )
{
setColor( 0,255, 255 ) ;
}
{
int reading = analogRead(potPin);
int angle = reading / 6;
servo.write(angle);
}

}

void setColor(int red, int green, int blue)
{
analogWrite( redPin, 200 - red ) ;
analogWrite( greenPin, 255 - green ) ;
analogWrite( bluePin, 255 - blue ) ;
}


 previous == stateButton;

Hmmm...

Using a servo requires sending it pulses at regular intervals. This requires a timer. Using PWM means turning the pin on and off at regular intervals. This also requires a timer. There are only a limited number of timers. Using a timer for a servo means that PWM on two pins is lost, starting with pin 9 and 10.

How are you powering the servo?

Hi ,
not only continuous pulse to servo ,even for RGB led,
but UNO has got multiple timers,(timer0 ,timer1,timer3) how to use these different timers to drive different devices?
i am using external 5V supply to drive servo.

and led goes to unknown blue color state

I can never remember - are those Democrats or Republicans?

Please remember to use code tags when posting code.

not only continuous pulse to servo ,even for RGB led,

My gibberish translator failed.

but UNO has got multiple timers,(timer0 ,timer1,timer3) how to use these different timers to drive different devices?

You could re-write the analogWrite() function to use different timers. Or, you could re-write the Servo class to use a different timer.

Or, you could use the ServoTimer2 class that already uses a different timer.

Be aware though that changing to timer2 for the servo will disable PWM on different pins.

"On boards other than the Mega, use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins."

int redPin = 9 ;
int greenPin = 11 ;
int bluePin = 10 ;

Move your Red and Blue LED connections to two other analogWrite() pins.