I need to read a value off of a potentiometer which will be 0 - 5V. I then need to assign a variable so it is 1 of 23 based on the voltage at the pot. Basically you turn the handle on the pot and it counts from 1 to 23 as you turn it.
I am then going to have that value trigger a nixie display that counts from 1 to 23. is there a function like a table I could fill out assigning the ranges for each position to keep from having 24 if statements?
Look into the Arduino reference and search for the map function!
int val = analogRead(potPin) / 44;
int analogValue = analogRead(potPin)
int count = map(analogValue, 0, 1023, 0, 23);
void setup()
{
Serial.begin(9600);
}
void loop()
{
int val = analogRead(A0) / 44;
Serial.println(val);
delay(500);
}
Sketch uses 2452, bytes (7%) of program storage space.
void setup()
{
Serial.begin(9600);
}
void loop()
{
int analogValue = analogRead(A0);
int count = map(analogValue, 0, 1023, 0, 23);
Serial.println(count);
delay(500);
}
Sketch uses 2,744 bytes (8%) of program storage space.
Difference, 292 bytes, map can be expensive.
My worry about the map function is that how it works is not transparent
However it does simplify the actual math. (Thanks Erik!)
jwags818:
I am then going to have that value trigger a nixie display that counts from 1 to 23. is there a function like a table I could fill out assigning the ranges for each position to keep from having 24 if statements?
IoT_hobbyist:
int analogValue = analogRead(potPin)
int count = map(analogValue, 0, 1023, 0, 23);
See [Arduino - map() function](https://arduinogetstarted.com/reference/arduino-map)
Make that:
int count = map(analogValue, 0, 1023, 1, 23);
johnerrington:
My worry about the map function is that how it works is not transparent
You have the source, and it's described here
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.