Go Down

Topic: RGB LED + Servo Problem (Read 1 time) previous topic - next topic

rcko

Jul 11, 2011, 05:21 am Last Edit: Jul 11, 2011, 06:03 am by rcko Reason: 1
Hello,

I'm using the components included in the Sparkfun Inventor's Kit. I'm trying to get a demonstration working where a potentiometer knob controls both the hue of an RGB LED and the position of a small servo motor.

I've run into an issue (documented earlier here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1291275850). When:
Code: [Select]
myservo.attach(3)
is added to the setup() section, RGB LED functionality breaks. The LED will only produce blue light.

This has nothing to do with the power requirements of the servo. Even when the servo is not plugged in, or if there is no output to the servo, the RGB LED still breaks. It seems to be the pin assignment function in servo.h that breaks the RGB LED.

rcko

#1
Jul 11, 2011, 05:22 am Last Edit: Jul 11, 2011, 05:24 am by rcko Reason: 1
This is the program code, for reference:
Code: [Select]
/*

 RGB_LED_Color_Fade_Cycle.pde
 
 Cycles through the colors of a RGB LED

 Written for SparkFun Arduino Inventor's Kit CIRC-RGB

*/

#include <Servo.h>

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

// LED leads connected to PWM pins
const int RED_LED_PIN = 9;
const int GREEN_LED_PIN = 10;
const int BLUE_LED_PIN = 11;
int potval = 0;
int ledval = 0;
int potpin = 0;
int serval = 0;
 
// Used to store the current intensity level of the individual LEDs
int redIntensity = 0;
int greenIntensity = 0;
int blueIntensity = 0;



void  setup()
{
//myservo.attach(3);  // attaches the servo on pin 3 to the servo object **RGB LED BREAKS IF UNCOMMENTED**
}

void loop() {

 delay(50);
  potval = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023)
 ledval = map(potval, 0, 600, 1, 900);  
 
 if (ledval < 310) {
   redIntensity = map(ledval, 1, 300, 1, 255);
   greenIntensity = 0;
   blueIntensity = map(ledval, 1, 300, 255, 1);
 }
   else if (ledval < 610 && ledval > 300) {  
   redIntensity = map(ledval, 310, 610, 255, 1);
   greenIntensity = map(ledval, 310, 610, 1, 255);
   blueIntensity = 0;
 }
  else if (ledval > 600 && ledval < 900) {  
   redIntensity = 0;
   greenIntensity = map(ledval, 610, 910, 255, 1);
   blueIntensity = map(ledval, 610, 910, 1, 255);
 }
 else {
   redIntensity = 0;
   blueIntensity = 0;
   greenIntensity = 0;
 }
       analogWrite(RED_LED_PIN, redIntensity);
       analogWrite(BLUE_LED_PIN, blueIntensity);
       analogWrite(GREEN_LED_PIN, greenIntensity);
 }
 

mem

The problem is that your LEDs are using pins connected to the same timer used by the Servo library.

The Servo Library reference says:
Quote
use of the library disables analogWrite() (PWM) functionality on pins 9 and 10, whether or not there is a Servo on those pins


If you use pins 3, 5 and 6 for LEDs it should start working.

rcko

Thank you mem,

That was the problem. Switching the output pins in the code solved the issue.

Incidentally the power supply of the arduino seems to work fine for running both a tiny servo and a bright RGB LED.

mem

good to hear you have it working

Have fun!

avictor

Was this ever completed? I would like to see the final code that controls the RGB LED/Servo from a pot.

rcko

It was completed, yes. I have a problem with saving my code in an organized way so I'll try to dig up a version and debug it and get it uploaded.

Go Up