1

Arduino programming has been a huge learning curve for me and I am trying to create a project with an rgb that's color is determined by a potentiometer. To where the further I turn it, the further across the rainbow spectrum it goes. I can't figure out how to do all values in between because I am not educated enough but I came up with this basic code...

int redpin = 6;
int greenpin = 3;
int bluepin = 5;
int potPin = 0;


void setup() {
  // put your setup code here, to run once:
  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(potPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int wavecolor = analogRead(potPin);
  Serial.print(wavecolor);
  {
    if (wavecolor = 0) {
      setColor(255, 0, 0);  // red
    }
    if (wavecolor > 0 or wavecolor < 333) {
      setColor(0, 255, 0);  // green
    }
    if (wavecolor > 333 or wavecolor < 667) {
      setColor(0, 0, 255);  // blue
    }
    if (wavecolor > 667 or wavecolor < 1022) {
      setColor(255, 255, 0);  // yellow
    }
    if (wavecolor = 1023) {
      setColor(80, 0, 80);  // purple
    }
  }
}

void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
  red = 255 - red;
  green = 255 - green;
  blue = 255 - blue;
#endif
  analogWrite(redpin, red);
  analogWrite(greenpin, green);
  analogWrite(bluepin, blue);
}

And for some reason, where the light should change colors once the potentiometer goes past a certain number, the light just flickers and only changes colors to white when it is all the way up or down. Also, it has a weird flickery yellow color. Since I am new to this, can one of you better educated people be willing to assist me?

1
  • think about this .... what will happen if you set the potentiometer to 333?
    – jsotola
    May 10, 2019 at 20:35

2 Answers 2

6
  • Equality comparison in C and C++ is expressed by == operator. ==, not =. This

    if (wavecolor = 0) {
    

    actually assigns zero to your wavecolor variable, thus destroying the value you just read.

  • The common sense says that you actually need and in your conditions, not or

    if (wavecolor > 0 and wavecolor < 333) {
    

    The or conditions that you curently use are simply always true, which makes them nonsensical.

  • This is valid syntax

    wavecolor > 0 or wavecolor < 333
    wavecolor > 0 and wavecolor < 333
    

    but you might want to try using more "canonical" forms

    wavecolor > 0 || wavecolor < 333
    wavecolor > 0 && wavecolor < 333
    

    or and and are "alternative" forms of || and && operators, introduced a long time ago for reasons that are all but obsolete today.

  • Assuming that you meant and instead of or, in your conditions you managed to "exclude" such values as 333, 667 and 1022 - they wouldn't match any of and conditions. Changing some strict comparisons to non-strict ones might be in order.

  • As a side note, in a single-file program all file-scope variables should be either const or static. In your case

    const int redpin = 6;
    const int greenpin = 3;
    const int bluepin = 5;
    const int potPin = 0;
    
4
  • 1
    "should be either const or static", saves a few bytes of SRAM too, can come in handy in big programs with lots of variable data.
    – CrossRoads
    May 9, 2019 at 18:27
  • Stylistic point: I prefer to write my conditions to look like ranges (in languages that don't have ranges built in), so 0 < wavecolor and wavecolor < 333 rather than wavecolor > 0 and wavecolor < 333
    – Alexander
    May 10, 2019 at 3:14
  • @Alexander: It makes perfect formal sense. But in practice it doesn't always work as well as one might expect. It clashes with another natural principle: express comparisons as "variable vs. limit" in that specific order, since that is how we usually express them in natural language. Which approach will win in the end is a matter of personal preference. May 10, 2019 at 3:26
  • @AnT Yeah, it's certainly contentious, but I have 3 reasons I prefer it: 1) It "looks" like a range, 2) The operands are ordered from least on the left to greatest on the right (which is usually how things are ordered, e.g. the x axis on a graph)
    – Alexander
    May 10, 2019 at 3:40
0

Try this code below. This will give you the desired output!

int redpin = 6;
int greenpin = 3;
int bluepin = 5;
int potPin = 0;


void setup() {
  // put your setup code here, to run once:
  pinMode(redpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(potPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  int wavecolor = analogRead(potPin);
  Serial.print(wavecolor);
  {
    if (wavecolor == 0) {
      setColor(255, 0, 0);  // red
    }
    else if (wavecolor > 0 && wavecolor < 333) {
      setColor(0, 255, 0);  // green
    }
    else if (wavecolor >= 333 && wavecolor < 667) {
      setColor(0, 0, 255);  // blue
    }
    else if (wavecolor >= 667 && wavecolor < 1022) {
      setColor(255, 255, 0);  // yellow
    }
    else if (wavecolor >= 1022) {
      setColor(80, 0, 80);  // purple
    }
  }
}

void setColor(int red, int green, int blue)
{
#ifdef COMMON_ANODE
  red = 255 - red;
  green = 255 - green;
  blue = 255 - blue;
#endif
  analogWrite(redpin, red);
  analogWrite(greenpin, green);
  analogWrite(bluepin, blue);
}

Read up on logical and relational operators in Arduino (||, &&, >, <, ==, >=, <=) etc.
I will leave the logic development to you as it is what develops your coding ability and trust me logic is the most important and fun part of writing a code!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.