float floatMap(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin GPIO36:
int analogValue = analogRead(15);
// Rescale to potentiometer's voltage (from 0 to 12v):
float Voltage = floatMap(analogValue, 0, 4095, 0, 12);
// print out the value you read:
Serial.print("Analog: ");
Serial.print(analogValue);
Serial.print("Voltage: ");
Serial.println(Voltage);
// Contoh Remap Pada Nilai desimal yang dikeluarkan sensor tegangan
float battery = floatMap(12.54, 8.30, 12.80, 0, 100);
Serial.print("Battery Percentage: ");
Serial.println(battery);
delay(1000);
}