Ohms law basic calculations for calculating R2 in voltage divider when R1 is known and to calculate voltage drop ratio when both R1 and R2 are known.

ohms.cpp

Committer:
joeata2wh
Date:
2016-08-04
Revision:
2:1c29960a42c6
Parent:
0:fe642275688d

File content as of revision 2:1c29960a42c6:

#include "mbed.h"
//#include "ohms.h"
 

float volDivideCalcRatio(long r1, long r2) {
  return (float) ((double) r2 / (double) (r1 + r2));
}
float voltDivideAdjVolt(float vin, long r1, long r2) {
  float ratio = volDivideCalcRatio(r1, r2);
  return vin / ratio;
}




long calcResistV(long refResistR1, float maxV, float measuredV) {
  float VDrop = maxV - measuredV;
  float dropRatio =  measuredV / VDrop;  
  return  (long) (refResistR1 * dropRatio);
}


/*

adjVoltDivide::adjVoltDivide(long r1, long r2, float refVolt) {
    _r1 = r1;
    _r2 = r2;
    _refVolt = refVolt;
    _ratio = volDivideRatio(r1, r2);
}
    
float adjVoltDivide::read(AnalogIn apin) {
    return apin.read() / _ratio;    
}

uint16_t adjVoltDivide::read_u16(AnalogIn apin) {
    return (int) ((float) apin.read_u16() / _ratio);
}

*/