• Part 1.
    From the linear scaling perspective two values have to be supplied to scale the reading into Engineering Units. For example an analog input returns a value from 0.0 to 1.0. if it’s connected to a pressure sensor, the 0.0 may represent -.1 psig and the 1.0 represents 59.8 psig.
    A field technician would secure a process and use a calibrated pressure to apply 0.0 psig, record the a number of analog input values and then apply a known calibration pressure and record a number of analog input values. Using two calibration values is the minimum required. Three point or more is sometimes used. Usually the software sets up a step by step procedure to do this and then performs a linear regression to calculate and record the slope and intercept in the logger. It would be valuable to record these values in one location in the data logger to be used to provide readings in engineering units, and be available for the next scheduled calibration.
    Typically the sensor is selected so that typical values fall below 80% of the sensor’s full range. The resolution is determined by the number of bits in the analog to digital converter. Say a pressure sensor has a full scale range of 100 psig. Then using a 12-bit ADC the resolution is 100/4096 psig. 80% of 100 psig is 80 psig and is usually used as the largest calibration pressure. This avoids placing too much pressure on the sensor and damaging it.
    Once the calibration has been performed an object method is used to read the analog input expressed in engineering units.
    Let’s set up an object to calibrate and scale the analog inputs into engineering units and write it to a file on the SD card as a stringified JSON. I hope Flash GURUs can supply us with a way to do this using FLASH memory.
    The exact entries will vary with the uP hardware and the pins you want to use.

    var AnalogReadObj1={
      calibrations:[
        {pin:'A0',slope:10.0,Intercept:5.0},
        {pin:'A1',slope:50.0,Intercept:-25.0},
        {pin:'A2',slope:1.0,Intercept:0.0},
        {pin:'A3',slope:1.0,Intercept:0.0},
        {pin:'A4',slope:1.0,Intercept:0.0}
      ]
    };//end AnalogReadObj1
    
    function saveCalibration(calibration) {
      var Q;
      var sss=JSON.stringify(calibration);
     if (Q===undefined) {
      Q = E.openFile("Mycalibrations.cal", "w");
      Q.write(sss);
      Q.close();
      Q = undefined;
     }//endif
    }//end doLog
    
    saveCalibration(AnalogReadObj1); 
    console.log(require("fs").readFileSync("­Mycalibrations.cal")); 
    
    

    The left WebIDE screen:

    >echo(0);
    {"calibrations":[{"pin":"A0","slope":10,­"Intercept":5},{"pin":"A1","slope":50,"I­ntercept":-25},{"pin":"A2","slope":1,"In­tercept":0},{"pin":"A3","slope":1,"Inter­cept":0},{"pin":"A4","slope":1,"Intercep­t":0}]}
    =undefined
    
    

    Now that the pattern has been saved how to use it?
    First read it as a string into a variable for example sss.

    var sss=(require("fs").readFileSync("Mycalib­rations.cal")); 
    console.log(sss);
    
    

    Apply the string to the constructor and use the calibrations

    var sss=(require("fs").readFileSync("Mycalib­rations.cal")); 
    //console.log(sss);
    
    //The constructor for AnalogReadObj
    function AnalogReadObj(ARO){
      this.A=JSON.parse(ARO);
    }//end AnalogReadObj
    //Methods for AnalogReadObj
    AnalogReadObj.prototype.SanalogRead=func­tion(Arg){
        for(var i=0;i in this.A.calibrations;i++){
    //      console.log(i,Arg,this.A.calibrations[i]­.pin);
         if(Arg===this.A.calibrations[i].pin){
      return( analogRead(Arg)*this.A.calibrations[i].s­lope+this.A.calibrations[i].Intercept);
         }//endif
        }//next i
        return -99;
    };//end SanalogRead
    
    AnalogReadObj.prototype.SetAnalog=functi­on(Arg){
        for(var i=0;i in this.A.calibrations;i++)
         pinMode(this.A.calibrations[i].pin, 'analog');
    };//end SetAnalog
    
    //create an instance of AnalogReadObject using the string data contained in 
    //the variable sss
    var ScaledAR=new AnalogReadObj(sss);
    //console.log(ScaledAR.calibrations);
    // setup the pins for analog
    ScaledAR.SetAnalog();
    // Now do a scaled read of pin A0
    console.log("A0= ",ScaledAR.SanalogRead('A0'));
    // Now do a scaled read of pin A1
    console.log("A1= ",ScaledAR.SanalogRead('A1'));
    // Now do a scaled read of pin A2
    console.log("A2= ",ScaledAR.SanalogRead('A2'));
    
    

    The left side of the WebIDE:

    >echo(0);
    A0=  12.72961013199
    A1=  12.70809491111
    A2=  0.72095826657
    =undefined
    >
    
    

    2 Attachments

About