diff --git a/src/Oversampling.cpp b/src/Oversampling.cpp index bf36306..9598a68 100644 --- a/src/Oversampling.cpp +++ b/src/Oversampling.cpp @@ -19,42 +19,9 @@ Oversampling::Oversampling(int adcbytes, int samplebytes, int Averaging) unsigned long Oversampling::read(int pin) { - int SampleCount = _samplebytes - _adcbytes; - SampleCount = constrain(SampleCount, 1, 8); - int OversampleCount;// = pow(SampleCount, 4); - int DecimationCount;// = pow(SampleCount, 2); - if (SampleCount==1){ - OversampleCount = 4; - DecimationCount = 2; - } - if (SampleCount==2){ - OversampleCount = 16; - DecimationCount = 4; - } - if (SampleCount==3){ - OversampleCount = 64; - DecimationCount = 8; - } - if (SampleCount==4){ - OversampleCount = 256; - DecimationCount = 16; - } - if (SampleCount==5){ - OversampleCount = 1024; - DecimationCount = 32; - } - if (SampleCount==6){ - OversampleCount = 4096; - DecimationCount = 64; - } - if (SampleCount==7){ - OversampleCount = 16384; - DecimationCount = 128; - } - if (SampleCount==8){ - OversampleCount = 65536; - DecimationCount = 256; - } + int DecimationCount = _samplebytes - _adcbytes; + DecimationCount = constrain(DecimationCount, 1, 8); + int OversampleCount = 1 << DecimationCount*2;// = pow(2, DecimationCount*2); // Can Lead To Overflow unsigned long TotalADC = 0; @@ -63,8 +30,8 @@ unsigned long Oversampling::read(int pin) TotalADC = TotalADC + analogRead(pin); } } - TotalADC = TotalADC / DecimationCount / _Averaging; + TotalADC = (TotalADC >> DecimationCount) / _Averaging; //_Averaging return TotalADC; -} \ No newline at end of file +}