For a bit of my code, I need a function that could display 2 decimal places in the serial monitor and uses a potentiometer.
I have thought to do it with the map function, but that wouldn't work.
That would only increase with 1.00 and not like 0.05 what I want.
What I have done is this:
Use the map function to constrain it between 0 and 300 and then divide it by a 100, so I could get the value between 0.00 and 3.00.
here is the code that I used.
I'm working with a esp-32.
#define potentiometer 34
#define TOLERANCE 5
int oldVal;
float devVal;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(potentiometer, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int val = analogRead(potentiometer);
val = map(val, 0, 4095, 0, 300);
delay(15);
int diff = abs(val - oldVal);
if (diff > TOLERANCE)
{
// lcd.backlight();
oldVal = val;// only save if the val has changed enough to avoid slowly drifting
devVal = val / 100;
}
//Serial.print(val);
// Serial.print(" ");
// Serial.print(oldVal);
// Serial.print(" ");
// Serial.println(devVal);
}