1

We made this code in class:

 int red = 6;
  int green = 7;
  int blue = 8;
  int pot = A0;
  int potVal = 0;
  int chosenColor = 0;


void setup() {
  // put your setup code here, to run once:
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(blue, OUTPUT);
  pinMode(pot, INPUT);
  Serial.begin (9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  potVal = analogRead(pot);
  chosenColor = map(potVal, 0, 1023, 1, 3);
  if(chosenColor = 1) {
    redCycle();
  }

  else {
    analogWrite(red,255);
  }

  if(chosenColor = 2) {
    greenCycle();
  }

  else {
    analogWrite(green,255);
  }


  if(chosenColor = 3) {
    blueCycle();
  }

  else {
    analogWrite(blue,255);
  }
  Serial.println(chosenColor);
  Serial.println(potVal);
  delay(500);
}

void redCycle() { 
    analogWrite(red,0);
  for(int i=0; i < 256; i+=5) {
    analogWrite(red, i);
  }
}

void greenCycle() {
  analogWrite(green,0);
  for(int i=0; i < 256; i+=5) {
    analogWrite(green, i);
  }
}

void blueCycle() {
  analogWrite(blue,0);
  for(int i=0; i < 256; i+=5) {
    analogWrite(blue, i);
  }
}

When we open the serial monitor we find that chosenColor is always 3 regardless of what the potVal is reading. I am sure is something simple but we can't find where is the error. Please help.

  • analogWrite() doesn't work on pins 7 and 8, only on those bearing the "~" symbol. – Edgar Bonet Apr 5 '17 at 20:12
  • Ok that is one problem we didn't think about! Thank you. Still not sure why the map function is not working. – Gabe Ruiz Apr 5 '17 at 20:14
  • chosenColor = 3 means “let chosenColor take the value 3”. – Edgar Bonet Apr 5 '17 at 20:19
3

As Edgar Bonet pointed out, “chosenColor = 3 means 'let chosenColor take the value 3'”. That is, your potVal = analogRead(pot); chosenColor = map(potVal, 0, 1023, 1, 3); sequence may very well be working, but by the time you get to Serial.println(chosenColor); you have changed chosenColor to 3.

First, the statement if(chosenColor = 1) {...} changes chosenColor to 1. Then the statement if(chosenColor = 2) {...} changes chosenColor to 2. Finally, if(chosenColor = 3) {...} changes chosenColor to 3, just before you print out the current value of chosenColor.

When you wish to compare two numeric values for equality, use == instead of =.

  • Aha! I get it. So I need to use ==. We will change that. – Gabe Ruiz Apr 5 '17 at 21:00
1

You are confusing = (assign) with == (compare).

eg.

Wrong! ...

if (chosenColor = 3)  // this assigns 3 to chosenColor

Correct:

if (chosenColor == 3)   // compare

Still not sure why the map function is not working.

You just assumed it is not working. Before making assumptions like this do a Serial print to show what the return value of the map function is. (Directly after calling map - not later on in the code).

  • I thought that <= would mean less or equal than. How would I do a greater than or equal to? – Gabe Ruiz Apr 5 '17 at 21:37
  • <= reads "less than or equal to"; >= is "greater than or equal to". – Mathieu K. Apr 6 '17 at 4:45
1

Although not related to your main problem, I would like to point out that there is an issue with your use of the map() function. Or maybe I should say, with the map() function itself. Since it works with integer arithmetic, and integer division by default rounds towards zero, you get a mapping that may be quite different from what you expect.

In the table below, I am showing the result of map(potVal, 0, 1023, 1, 3). The column labeled “not rounded” is what you would get if the mapping was done in floating point. The last column is the rounded value you actually get out of map():

potVal  not rounded  rounded
----------------------------
    0     1.00000       1
            ...
  511     1.99902       1
  512     2.00098       2
            ...
 1022     2.99804       2
 1023     3.00000       3

As you see, you have 512 possible values of potVal mapped to 1, 511 values mapped to 2 and one single value mapped to 3.

I assume you would rather have one third of the range mapped to each value. This can be achieved by writing map(potVal, 0, 1024, 1, 4):

potVal  not rounded  rounded
----------------------------
    0     1.00000       1
            ...
  341     1.99902       1
  342     2.00195       2
            ...
  682     2.99805       2
  683     3.00098       3
            ...
 1023     3.99707       3

This gives 342 values mapped to 1, 341 values mapped to 2 and 341 values mapped to 3. Now, the problem is that this way of using map() is confusing. You may prefer to write something more explicit instead, like:

if (potval < 1023/3) chosenColor = 1;
else if (potval < 1023*2/3) chosenColor = 2;
else chosenColor = 3;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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