0

Mega 2560

I am reading in values from 3 pots then mapping them to values between 0 - 255

#define REDPIN   7
#define GREENPIN 8
#define BLUEPIN  9

int red_pot   = A0;  // analog pin used to connect the potentiometer
int green_pot = A1;
int blue_pot  = A3;


void setup() {

  pinMode(REDPIN,   OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN,  OUTPUT);

  Serial.begin(38400); //Debug port
  Serial.setTimeout(50);
}


void loop() {

  int red, green, blue; 

  red   = analogRead(red_pot);
  green = analogRead(green_pot);
  blue  = analogRead(blue_pot);

  Serial.print(" r = ");
  Serial.print(red);

  Serial.print(" b = ");
  Serial.print(blue);

  Serial.print(" g = ");
  Serial.print(green);
  Serial.print("\n\n\n");


  // map values
  red   = map(red,   20, 650, 0, 255);     
  green = map(green, 20, 570, 0, 255);
  blue  = map(blue,  20, 570, 0, 255);


  Serial.print("red map : ");
  Serial.print(red);

  Serial.print(" green map : ");
  Serial.print(green);

  Serial.print(" blue map : ");
  Serial.print(blue);
  Serial.print('\n\n\n');

  analogWrite(REDPIN,   red); 
  analogWrite(GREENPIN, green);
  analogWrite(BLUEPIN,  blue);    
}

red & green behave as expected but blue is not, from the serial monitor...

When blue is at its lowest value,

enter image description here

When blue is at its highest value,

enter image description here

I have changed blue's analog pin assignment but the same issue.

Any clues what's happening?

EDIT #1

I thought it might be some int sign error so I changed the map operation to,

blue  = map(abs(blue), 20, 670, 0, 255);

It made no difference.

  • In your print statement first r, b and g are printed but in your output it shows first the mapping. Are you sure the boxed values belong to each other? – Michel Keijzers Oct 9 at 15:31
  • yes if I leave the pots at a setting the values are stablish +/-5 on the input but pretty much the same values – DrBwts Oct 9 at 16:13
1

Note that the Arduino function "map" is really this:

long map(long x, long in_min, long in_max, long out_min, long out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

...so, if you pass a "minimum from" value which is less than the actual value you are mapping from you will get a negative number given your "minimum out" is zero.

In several of your examples you have set the minimum to 20 but pass a value of 19. If your program is intolerant of negative numbers (for instance, how would a negative number effect the PWM hardware), consider testing for lower than minimum and higher than maximum values before using the map function.

  • 1
    I realize this does not directly address your example code combined with your output. However, it is obvious the output is not directly from the code as it is printed in reversed order. Try running your code again and verifying the output. If it indicates something different, please modify your question. If it confirms your original post - then I'll have to think this over a bit more and revise my answer. – st2000 Oct 9 at 16:58
  • its definitely the output from the code, I have no idea why the serial monitor is outputting in the format as it is. Also can confirm that min(blue) == 22 but to make sure I lowered the range from 20 to 10 but still the same ouput. – DrBwts Oct 9 at 17:20
  • @DrBwts - The constrain() function can be used to ensure the proper range of "numbers" is passed to the map() function: arduino.cc/reference/en/language/functions/math/constrain – VE7JRO Oct 9 at 17:40
  • But in the code you print r, b, g before you print red map, green map then blue map. In addtion there should be 3 lines in-between. So the order you print values out in your question makes the reader think the r, b & g values relate to the next set of red map, green map & blue map values. Not previous red map, green map & blue map values as you are indicating with your red and yellow boxes. I would try to lower the "minimum map from" value to zero just to make sure. If the problem persists, I then would simplify the code and only process the "blue" POT's input. – st2000 Oct 9 at 18:03
  • weirdly I amended the code to get a sensible serial output literally changing "\n\n\n" to '\n' & now its working fine – DrBwts Oct 11 at 13:44

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.