Weird analogRead

HI all,

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      
       }
}

Any clue what's going on ?

Not without seeing your complete program

I understand, but sending the complete program would be a problem, it has 1300 lines...

This problem is driving me crazy !

Thanks,

tk5ep: I understand, but sending the complete program would be a problem, it has 1300 lines...

This problem is driving me crazy !

Thanks,

So write a small example that show the problem.

(deleted)

Hi,

Attached my software.

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...

Apaguard version 1.030618 started ! Date : 3/6/2018 08:22 Temp. DS3231 : 25.00 Firstloop Dernier poids 1: 0.00 Poids 1: -0.10 ALERTE PERTE POIDS Temperature : 0.00 Pression : 0.00 Humidite : 0.00 1009 2018 3026 4034 5043 6052 7061 8070 9079 10088 1008 Ubatt = 13.86 CSQ = 2 SMS sent Interruption !! UTC : 8:25 Dernier poids 1: -0.10 Poids 1: -0.10 1009 2018 3027 4036 5045 6054 7063 8072 9081 10090 1009 Ubatt = 13.88 Interruption !! UTC : 8:30 Dernier poids 1: -0.10 Poids 1: -0.05 1009 2018 3027 4036 5045 6054 7063 8072 9081 10090 1009 Ubatt = 13.88

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.

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 ?

Thanks,

tk5ep: I understand that the code can be hard to read... Do you have an example of how it should be ?

Use the autoformat tool in the IDE. Have you never looked at other code before and seen how the blocks are all lines up and indented together?

My code is indented. Maybe not as you would like to see it... Never mind,

Thanks

tk5ep:
My code is indented. Maybe not as you would like to see it…
Never mind,

Thanks

Yes but your blocks keep going back over to the left margin which makes it really hard to tell where one function ends. If you don’t want help bad enough to present readable code then I guess you just don’t want help that bad. Best of luck to you.

(deleted)

Hi,

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 ?

Patrick

tk5ep: Apart my "bad" coding habits, is there something wrong in my function i overlooked ?

I'd have to be able to read the code to look at the function in order to tell you that. Now do you see the problem?

If you're not willing to put in the small amount of effort needed to format that code then why should any one else put any effort in at all. It's not my project. What should I care if you don't?

You really cannot expect any good help, if you do not provide the information needed to give it. What is wrong with the following code?

int iVal;

int clampReading(int reading)
{
  if (reading < 200) iVal = 200;
  else if (reading < 400) iVal = 400;
  else if (reading < 800) iVal = 800;
  else iVal = 1000;
  return iVal;
}

It will not allow my code to calculate an average reading… Well, hard to answer - lets look at the entire code:

int iVal, iAvg;

int clampReading(int reading)
{
  if (reading < 200) iVal = 200;
  else if (reading < 400) iVal = 400;
  else if (reading < 800) iVal = 800;
  else iVal = 1000;
  return iVal;
}

void setup()
{
  Serial.begin(9600);
  pinMode(A0, INPUT);
}

void loop()
{
  iAvg = 0;
  for (iVal = 0; iVal < 5; iVal++) iAvg += clampReading(analogRead(A0));
  iAvg /= 5;
  Serial.print("Average = ");
  Serial.println(iAvg);
}

See the problem with posting partial code? :wink: