Hello
I have a maths problem were I'm trying to work out the percentage of a battery
the min value is 1300
the max value is 2100
the variable value will be between 2100 and 1300, lets say its 2000.
how do I work out the percentage of the variable value in arduino.
Use the mapfunction.
percent = map(readValue, 0, 100, 1300, 2100)
Reference: map() - Arduino Reference
nathanhanson:
Hello
I have a maths problem were I'm trying to work out the percentage of a battery
the min value is 1300
the max value is 2100
Evaluating the % of charge of a battery is not that simple.
You need at least to perform two mappings: one while charging, the other discharging.
better add a third one while being quasi idle.
Notebooks use a 3d landscape integrating the battery current: easy to programm, hard to parameter for halfway credible results.
Sorry I may have missed something.
when I run
int readValue = 2100;
int percent = map(readValue, 0, 100, 1300, 2100);
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(percent);
delay(100);
}
I get a result of 18621 i was expecting 100
Hi,
Try this, map function was wrong way round.
int readVal = 2100;
int percent = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
percent = map(readVal, 1300, 2100, 0, 100);
Serial.println(percent);
delay(100);
}
Tom.... 
nathanhanson:
Sorry I may have missed something.
I get a result of 18621 i was expecting 100
it was in the link Railroader posted to the function
Syntax
map(value, fromLow, fromHigh, toLow, toHigh)
TomGeorge:
Hi,
Try this, map function was wrong way round.
int readVal = 2100;
int percent = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
percent = map(readVal, 1300, 2100, 0, 100);
Serial.println(percent);
delay(100);
}
Tom.... :)
Thank you so much that worked. Thank you all for your help