I have a weird behavior of the following function.
It basicaly reads a 12V battery voltage on pin A7 thru a resistor divider with an Arduino promini.
It works perfectly when i call it the first time, even if i call it 3 times in a row. The displayed voltage varies with the supply voltage i'm varying.
But the next times i call it inside the main loop, it displays the same 10 analogread values and calculated voltage as the last time it has been called....
Any clue what's going on ?
Thanks
float tensionBatterie;
const byte PIN_VOLTMETER = A7;
const float REFVOLTAGE = 3.281; // exact voltage of Arduino supply measured on pin VCC
const float PONTDIVISEUR = 0.23295; // measured voltage divider ratio 27000/8200 Ohm
const float SEUILBATTERIE = 11.0; // low voltage alarm level
void readBatterie() {
int analogBatt=0;
tensionBatterie=0;
analogRead(PIN_VOLTMETER);
delay(750);
for (byte i=0; i < 10; i++) {
analogBatt += analogRead(PIN_VOLTMETER);
Serial.println(analogBatt);
}
analogBatt /= 10;
// TEST
Serial.println(analogBatt);
tensionBatterie = analogBatt * (REFVOLTAGE / 1024);
tensionBatterie /= PONTDIVISEUR ;
#ifdef DEBUG
Serial.print(F("Ubatt = "));
Serial.println(tensionBatterie);
#endif
if ( tensionBatterie < SEUILBATTERIE ) {
alerteBatterie = 1;
#ifdef DEBUG
Serial.println(F("ALARME BATTERIE"));
#endif
}
}
I call the readBatterie() function in the first part of the loop, and then at each interrupt (actualy every 5 min for test purpose).
The first time, the voltage measure is correct, i can call several the function times in a row and vary the voltage to check, it works.
But when called by the interrupt, it gives EXACTLY the same value as last time it has been executed.
I display the analog value in the function to check.
I tried to clear the tensionBatterie variable , but it doesn't change anyhting.
Not sure if you will find patience to look at my script...
Delta_G:
That code is all over the place. Please be nice enough to format your code if it is that long and you want folks to be able to read it.
If readBatterie() is really being called from an interrupt then it shouldn't have all those Serial print lines in it because Serial is driven by interrupts that are disabled inside your ISR. You should also probably make the tensionBatterie variable volatile since it is changing inside an ISR.
The function is called after an interrupt, but i disable it the time needed to do all the things... So volatile and other printing should not be a source of problem.
I understand that the code can be hard to read...
Do you have an example of how it should be ?
I understand. But i don't want anyone to get into my code and solve my problem.
Apart my "bad" coding habits, is there something wrong in my function i overlooked ?
I can't understand, why there is no change in the 10 analogreadings when i call this function after a first execution. What could be the reasons for that ?