AntiGuide: Attiny85DoublePrecision



PagePrincipale :: DerniersChangements :: ParametresUtilisateur :: Vous êtes 216.73.216.54 :: Signaler un abus :: le: 20250718 01:30:57

double est compilé comme float

bac à sable: compteur de mWh en décharge d'un accumulateur/
Si on somme les débits à la seconde, ça devient très vite imprécis.
solution, double précision autour de 1.0

soluce proposée:
on dispose de deux compteurs, un pour la partie fractionnaire et un pour la partie entière
quand le "petit" compteur dépasse 1 on incrément le compteur entier

Gaffe: pas une panacée
selon la plage numérique souhaitées, il faut changer le critère "1" è le mettre au mileir de la plageà une autre valeur

int sensorValue = 0;


#include "DigiKeyboardFr.h"

#define PERIODE 1  // un seconds
#define RESISTANCE 1 // Ohms
#define NBDEC 10
//#define SEP "\t" // excel but . intead de , dor fren ch
#define SEP ";"
const float VSTEP=5.0/1024;
const float H=PERIODE/3600.;

void setup() {
    //You need not set pin mode for analogRead - though if you have set the pin to
    //output and later want to read from it then you need to set pinMode(0,INPUT);
    //where 0 is the physical pin number not the analog input number.
    //
    //See below for the proper pinMode statement to go with each analog read.
    // header line for Excel
 for (int i=0;i<100;i++)DigiKeyboardFr.print(F("_"));
    DigiKeyboardFr.println();
    DigiKeyboardFr.println(F("stamp;raw;V;mAh;mWh;delta"));
   }

long big=0;
const long mil=1000;
float mAh=0;
float mWh=0;
float V;
float A;
float delta;
float w;
  float error=0;
  float th; 
  float mWhfortà0;
float mWhfaible=0;
void sep(){
  DigiKeyboardFr.print(SEP);
}
void loop() {
   big=big+PERIODE;

    
    // The analog pins are referenced by their analog port number, not their pin
    //number and are as follows:

    //THIS IS P2, P2 is analog input 1, so when you are using analog read, you refer to it as 1.
 
    sensorValue = analogRead(1); //Read P2
    V=sensorValue*VSTEP;
    V=5.0; // force value for test
    A=V/RESISTANCE;
    mAh=mAh+H*A;
    delta=mWh;
    w=H*A*V;
    mWh=mWh+w;
    mWhfaible=mWhfaible+w;
    if (mWhfaible>1.0) {
      mWhfort=mWhfort+1;
      mWhfaible=mWhfaible-1;
    }
    delta=mWh-delta-w;
    error=error+delta;
    th=(big/3600)*V*A;
    if (big%36000==0){
    DigiKeyboardFr.print(big);
    sep();
    DigiKeyboardFr.print(sensorValue);
    sep();
    DigiKeyboardFr.print(V);
    sep();
    DigiKeyboardFr.print(mAh,NBDEC);
    sep();
    DigiKeyboardFr.print(mWh,NBDEC);
    sep();
    DigiKeyboardFr.print(w,NBDEC);
    sep();
    DigiKeyboardFr.print(delta,NBDEC);
    sep();
  //  DigiKeyboardFr.print(error,NBDEC);
  //  sep();
    DigiKeyboardFr.print(th,NBDEC);
    sep();
    DigiKeyboardFr.println(mWhfort+mWhfaible,NBDEC);
    }
    //THIS IS P4, P4 is analog input 2, so when you are using analog read, you refer to it as 2.
   //THIS IS P3, P3 is analog input 3, so when you are using analog read, you refer to it as 3.
   //THIS IS P5, P5 is analog input 0, so when you are using analog read, you refer to it as 0.
 // delay(mil*PERIODE);
 
}

/*
**___________________
*
*
 */


exemple de sortie (capteur forcé à 5V):
___________________________________
stamp;raw;V;mAh;mWh;delta
36000;654;5.00;49.9900169372;249.9476165771;0.0069444446;-0.0000016955;250.0000000000;249.9995880126
72000;532;5.00;99.9778137207;500.4226074218;0.0069444446;0.0000135633;500.0000000000;499.9991760253
108000;440;5.00;149.9656066894;750.9108886718;0.0069444446;0.0000135633;750.0000000000;749.9987182617
144000;318;5.00;199.9533996582;1001.3991699218;0.0069444446;0.0000135633;1000.0000000000;999.9983520507
180000;319;5.00;249.9411926269;1251.8874511718;0.0069444446;0.0000135633;1250.0000000000;1249.9979248046
216000;318;5.00;300.4117126464;1502.3757324218;0.0069444446;0.0000135633;1500.0000000000;1499.9974365234
252000;319;5.00;350.9488220214;1752.8640136718;0.0069444446;0.0000135633;1750.0000000000;1749.9970703125
288000;318;5.00;401.4859313964;2003.3522949218;0.0069444446;0.0000135633;2000.0000000000;1999.9967041015
324000;319;5.00;452.0230407714;2250.2292480468;0.0069444446;-0.0001085070;2250.0000000000;2249.9963378906
360000;319;5.00;502.5601501464;2496.3229980468;0.0069444446;-0.0001085070;2500.0000000000;2499.9958496093
396000;319;5.00;553.0972290039;2742.4167480468;0.0069444446;-0.0001085070;2750.0000000000;2749.9953613281
.......
5976000;318;5.00;8472.9072265625;44387.3671875000;0.0069444446;0.0008680555;41500.0000000000;41499.9296875000
6012000;319;5.00;8508.0634765625;44668.6171875000;0.0069444446;0.0008680555;41750.0000000000;41749.9296875000
6048000;319;5.00;8543.2197265625;44949.8671875000;0.0069444446;0.0008680555;42000.0000000000;41999.9296875000
6084000;318;5.00;8578.3759765625;45231.1171875000;0.0069444446;0.0008680555;42250.0000000000;42249.9296875000