Potentiometer Map

Hi,

I am trying to map potentiometers serial values from 0-1023 to 0-255 however it seems to be broken as it reads 0-675 both before I added the map function and after. I have also tried changing 1023 to 675 and still doesn't work.

I know this will be and easy fix but got no idea what I've done wrong here.

// Code for Potentiometers

byte Pots[] = {A0, A1, A2, A3, A4}; //define analog inputs connected to pots
byte numberPots = 5; //number of inputs to read

void setup() {
  Serial.begin(9600);
  
  for (byte i = 0; i < numberPots; i++){
    map (Pots[i], 0, 1023, 0, 255);
  }
}

void loop() {
  for (byte i = 0; i < numberPots; i++) {
    Serial.print(analogRead(Pots[i])); //print value read from pot
    Serial.print(" "); //space between sensor value and text

  }
  Serial.println(); //print blank line between groups of output
  delay(1000); //slows loop to once per second
}

Link to Code Login | Tinkercad

Also tried this on my Arduino as TinkerCAD user C98++ has issues sometimes but still doesn't map.

Any help would be greatly appreciated :smiley:

map() works on the value of the analogRead() function. It does not change the range of the values returned from an analogRead().

// Code for Potentiometers

byte Pots[] = {A0, A1, A2, A3, A4}; //define analog inputs connected to pots
byte numberPots = 5; //number of inputs to read

void setup() {
  Serial.begin(9600);
 
  for (byte i = 0; i < numberPots; i++){
    map (Pots[i], 0, 1023, 0, 255);
  }
}

void loop() {
  for (byte i = 0; i < numberPots; i++) {
    int val = map(analogRead(Pots[i]), 0, 1023, 0, 255 );
    Serial.print(val); //print value read from pot
    Serial.print(" "); //space between sensor value and text

  }
  Serial.println(); //print blank line between groups of output
  delay(1000); //slows loop to once per second
}

for you case, you can just as easily divide by 4.

And remove this from setup(). It doesn't do anything.

  for (byte i = 0; i < numberPots; i++){
    map (Pots[i], 0, 1023, 0, 255);
  }

blh64:
map() works on the value of the analogRead() function. It does not change the range of the values returned from an analogRead().

// Code for Potentiometers

byte Pots[] = {A0, A1, A2, A3, A4}; //define analog inputs connected to pots
byte numberPots = 5; //number of inputs to read

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

for (byte i = 0; i < numberPots; i++){
   map (Pots[i], 0, 1023, 0, 255);
 }
}

void loop() {
 for (byte i = 0; i < numberPots; i++) {
   int val = map(analogRead(Pots[i]), 0, 1023, 0, 255 );
   Serial.print(val); //print value read from pot
   Serial.print(" "); //space between sensor value and text

}
 Serial.println(); //print blank line between groups of output
 delay(1000); //slows loop to once per second
}

Slightly better:

// Code for Potentiometers
const byte NumberPots = 5; //number of inputs to read
const byte PotPins[NumberPots] = {A0, A1, A2, A3, A4}; //define analog inputs connected to pots


void setup() 
{
  Serial.begin(115200);
}


void loop() 
{
  for (byte i = 0; i < NumberPots; i++) 
  {
    int val = map(analogRead(PotPins[i]), 0, 1023, 0, 255);
    Serial.print(val); //print value read from pot
    Serial.print(" "); //space between sensor value and text
  }
  Serial.println(); //print blank line between groups of output
  delay(1000); //slows loop to once per second
}

johnwasser:
Slightly better:

// Code for Potentiometers

const byte NumberPots = 5; //number of inputs to read
const byte PotPins[NumberPots] = {A0, A1, A2, A3, A4}; //define analog inputs connected to pots
...

Even better

const byte PotPins[] = {A0, A1, A2, A3, A4}; //define analog inputs connected to pots
const byte NumberPots = sizeof(PotPins) / sizeof(PotPins[0]); //number of inputs to read