# Using multiple contrains with a single map function

Hi All,

Just want to query a programming question with you all.

I have been looking into the map and constrain function and just wondered if I would be able to take this

``````int val = map (analogRead(Pots[i]), 0, 1023, 0, 255)
``````

And add multiple contrains to it like below.

``````val = constrain(val, 0, 255, 0, 10)
val = constrain(val, 0, 255, 11, 25)
val = constrain(val, 0, 255, 26, 50)
val = constrain(val, 0, 255, 51, 75)
``````

What I am essentially trying to replicate is an amplifier pot number.
A pot on an amp is generally named 1-10 I was just wondering if it would be possible to add say 9/10 constrains to a single map.

If this sounds like a silly question sorry but I am fairly new to Arduino’s.

Thanks for looking,
Ben

Perhaps you need to look at the reference for constrain https://www.arduino.cc/reference/en/language/functions/math/constrain/

It doesn't look like your version. But I'm not really sure what you are trying to do with it. Can you explain what you're trying to achieve in a bit more detail?

Steve

slipstick: Perhaps you need to look at the reference for constrain https://www.arduino.cc/reference/en/language/functions/math/constrain/

It doesn't look like your version. But I'm not really sure what you are trying to do with it. Can you explain what you're trying to achieve in a bit more detail?

Steve

Hi Steve,

I have mapped all my pots from 0 - 255 and want to set values in-between the mapped value.

So the pot would be mapped to 0 - 255 but I would also be able to assign a variable/int to a constrain between 0-10 inside of the mapped value.

I would then replicate that 11 times to get the values up to 255. This would then allow me to react to these sections which have been set out.

I have been told I could do this by taking the max value 255 and divide it by how many sections I would need in this case it would be 11 so would be 255 divided by 11 equalling 23.18 but how would I go about doing this? In a way that I could reference the values later?

Ben

This is my first time using the Div() function so not 100% if I have done this right but here is my code so far.

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

}
``````

Please post a complete program to provide some context. Snippets are useless.

If your div snippet is related to the original query then you’re making it even less clear. Where is the array Pots defined and what does it contain? Why are you calculating a local variable called part by dividing Pots[ i ] by 11 and then never doing anything with it? Since you earlier do an analogRead() from Pots[ i ] I guessed it was a pin number so dividing it by 11 seems pointless.

If you are trying to react to ranges within 0-255 then I can’t think of any easier way than standard if statements. E.g. if (val>0 && val <11).

Steve

slipstick:
Please post a complete program to provide some context. Snippets are useless.

If your div snippet is related to the original query then you’re making it even less clear. Where is the array Pots defined and what does it contain? Why are you calculating a local variable called part by dividing Pots[ i ] by 11 and then never doing anything with it? Since you earlier do an analogRead() from Pots[ i ] I guessed it was a pin number so dividing it by 11 seems pointless.

If you are trying to react to ranges within 0-255 then I can’t think of any easier way than standard if statements. E.g. if (val>0 && val <11).

Steve

// 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);
}

void loop() {
for (byte i = 0; i < numberPots; i++) {
int val = map (analogRead(Pots*), 0, 675, 0, 255);*
div_t part = div(Pots*, 11);
_
Serial.print(val); //print value read from pot*_
* Serial.print(" "); //space between sensor value and text*
_ Serial.println(Pots*);*_

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

Hello BenjaminTzou!

I am also new to Arduino. I read your post and I think you are facing the same issue that I was facing when I started out on Arduino.

I want to advise you that instead of trying to get correct output by hit and trial read the functionality of the function you are using. It would far better and each time you read about the function you will learn new things.

SO instead of replying to the answer I would recommend you reading the map() function functionality and figure out the answer yourself.

I am just trying to help!!!

Do you simply want to turn the raw reading from the pot into 1 of 11 values ?

Please at least get your “complete program” to compile before posting it. And please use </> code tags so half of it doesn’t end up in italics. You managed that in the original post and #3 so why not this time?

If you can’t get it to compile say so and say what errors you’re getting that you can’t fix. If it does compile say what it does and what you want it to do that’s different.

Steve