Pages: [1]   Go Down
Author Topic: RGB LED + Servo Problem  (Read 997 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: July 10, 2011, 11:03:54 pm by rcko » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is the program code, for reference:
Code:
/*

  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);
  }
  
« Last Edit: July 10, 2011, 10:24:23 pm by rcko » Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

good to hear you have it working

Have fun!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: