Potentiometer controlling RGB LED

I just bought the starter kit and am working through the projects. I’ve just completed Project 5 Mood Cue (using a Servo Motor). I wanted to try something different and build off what I learned in Project 4 with the RGB LED.

The idea was to install the RGB LED and depending on the output of the potentiometer(potMtr) to fire the RGB.

Using 0-1023 from the potMtr, I was going for
0-500 Red
341-682 Blue
682-1023 Green

If I am in the range then I am sending 200 to the RGB

My issues:

  1. I am not seeing any Blue. I have opened the Serial Monitor and dialed in 500. The color was the last color seen. No Change. I have double checked the physical wiring and it appears correct.
  2. The RGB is not changing colors as the servo is moving. I assumed I could have the RGB change in real time with the servo.

Here is my code…and it is attached

#include <Servo.h>
Servo myServo;

//declaring the variables
int const potPin = A0; //output from the potentiometer is on pin A0
int potVal;
int angle;

const int redLEDpin = 11;
const int blueLEDpin = 10;
const int greenLEDpin = 6;

int redValue = 0;
int blueValue = 0;
int greenValue = 0;

void setup() {
myServo.attach(9); //this is telling the board the servo is on pin 9.

Serial.begin(9600);

pinMode(redLEDpin, OUTPUT);
pinMode(blueLEDpin, OUTPUT);
pinMode(greenLEDpin, OUTPUT);
}

void loop () {
potVal = analogRead(potPin); //my variable potVal is equal the board reading the potPin which is on AO
Serial.print("potVal: ");
Serial.print(potVal);

angle = map(potVal, 0, 1023, 0, 179); //the servo motor opearted on 0-179 but the pot. spits out values 0-1023
Serial.print(", angle: ");
Serial.println(angle);

myServo.write(angle);

if(potVal < 500) {
analogWrite(redLEDpin,200);//analogWrite(pin to write to, value between 0-255
}
else if(potVal > 342 && potVal <682){
analogWrite(blueLEDpin,200);
}

else if(potVal > 682) {
analogWrite(greenLEDpin,200);
}

delay(15);
}

Thanks,
Matt

RGB_AND_SERVO.ino (1.24 KB)

Move the blue LED to another PWM pin.
https://www.arduino.cc/en/Reference/Servo

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.

  if (potVal < 500) {
    analogWrite(redLEDpin, 200); //analogWrite(pin to write to, value between 0-255
  }
  else if (potVal > 342 && potVal < 682) {
    analogWrite(blueLEDpin, 200);
  }

  else if (potVal > 682) {
    analogWrite(greenLEDpin, 200);
  }

You turn the LEDs on, but never turn them off.

I realize you’re doing a tutorial, so for when you get further along the same goal with different implementation -

 // <http://forum.arduino.cc/index.php?topic=446557.0>

#include <Arduino.h>

#define NUM_ENTRIES(ARRAY)      (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t   pinPOT          = A0;

const uint8_t   pinLED_RED      = 11;
const uint8_t   pinLED_BLUE     = 10;
const uint8_t   pinLED_GREEN    =  6;

const uint8_t   pinsLEDS[]      = { pinLED_RED, pinLED_BLUE, pinLED_GREEN };

void setup()
{
    for ( size_t i = NUM_ENTRIES(pinsLEDS); i--; )
    {
        pinMode(pinsLEDS[i], OUTPUT);
    }
}

void loop()
{
    int index = map(analogRead(pinPOT), 0, 1023, 0, 2);

    for ( size_t i = NUM_ENTRIES(pinsLEDS); i--; )
    {
        pinMode(pinsLEDS[i], ((i == index) ? 200 : 0));
    }

    delay(15);
}

Hi,

 if(potVal < 500) {
                  analogWrite(redLEDpin,200);//analogWrite(pin to write to, value between 0-255
                  }
else if(potVal > 342 && potVal <682){
                  analogWrite(blueLEDpin,200);
                                    }
else if(potVal > 682) {
                  analogWrite(greenLEDpin,200);
                      }
delay(15);
              }

You turn ON RED LED below 500, but you don’t turn it OFF anywhere.
You turn ON BLUE LED between 342 and 682, but you don’t turn it OFF anywhere.
You turn ON GREEN LED above 682, but you don’t turn it OFF anywhere.

I understand you only
-want RED between 0 and 500
-want BLUE between 342 and 682
-want GREEN between 682 and 1023

Tom… :slight_smile:

And a little tip, if you place your opening brackets like this

if(test){

the indentation of the closing bracket should match that of "if", NOT the opening bracket.

So

if(test)
  //do stuff
  //more stuff
  /etc
}

That way it's wayyyyyyyy more readable :)

This:

int index = map(analogRead(pinPOT), 0, 1023, 0, 2);

will only return 2 if the pinPOT reads 1023! You have been advised.

If the potentiometer has trouble reaching the reference voltage, you might never see 1023 at the analog value, you would never get 2 out of the map function.

For your convenience, two short demonstrations:

Flawed:

int val;

void setup() {
  Serial.begin(9600);

  for (val = 0; val < 1150; val += 35) {

    Serial.print(val);
    Serial.print("  ");
    Serial.println(map(val, 0, 1023, 0, 2));
  }

  Serial.println();
  Serial.println();

  val = 1023;

  Serial.print(val);
  Serial.print("  ");
  Serial.println(map(val, 0, 1023, 0, 2));

}

void loop() {
  // put your main code here, to run repeatedly:

}

And fixed:

int val;

void setup() {
  Serial.begin(9600);

  for (val = 0; val < 1150; val += 35) {

    Serial.print(val);
    Serial.print("  ");
    Serial.println(map(val, 0, 1024, 0, 3));
  }

  Serial.println();
  Serial.println();

  val = 1023;

  Serial.print(val);
  Serial.print("  ");
  Serial.println(map(val, 0, 1024, 0, 3));

}

void loop() {
  // put your main code here, to run repeatedly:

}

– a7

alto777:
This:

int index = map(analogRead(pinPOT), 0, 1023, 0, 2);

will only return 2 if the pinPOT reads 1023! You have been advised.

Well of course, as expected, and tested!

pinMode(pinsLEDS[i], ((i == index) ? 200 : 0));

What is pinMode 200? IS that INPUT_PULLUP? or OUTPUT?

Do you intentionally do this sort of stuff to show off that you know something or do you really in your heart of hearts think that makes for more readable code? I mean sure we're impressed that you figured out how to ditch the last term in your for loop. You're so awesome. I wish we could all be just like you.

But now who is going to explain that shit to the poor noobie who is going to try to learn from this code now? Maybe when you do things so unorthodox and outside what is being taught to these guys you should add some explanation with it. Maybe a paragraph or two about how that works in a for loop. Cause I can see someone without a full set of programming tools get lost in there. Maybe they can copy and paste your code and it works for what they want now, but damn they'll never be able to modify it or work with it until they learn all these esoteric constructs that you like to use.

And while you're at it, go ahead and give him a little primer on the ternary operator. I bet the noob hasn't seen that one before and it certainly isn't in the reference. So be nice. If you're going to spring it on him at least tell him what it does.

All in all I think it is disingenuous and that you aren't trying to help but are instead just trying to show off. m2c anyway.

lloyddean: Well of course, as expected, and tested!

I don't know what you tested nor what you expected.

As long as you are happy that the green LED only lights up when the potPIN is 1023 so am I. You prolly meant analog.write? Anyway

The OP and others seemed to want to break the span 0-1023 into three somewhat equal regions. Your call to map does not do that.

-- a7