Skip to content

Commit 67ad322

Browse files
committed
Improve accuracy of ADC.
1 parent 8187173 commit 67ad322

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

MbitMoreService.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -522,16 +522,19 @@ void MbitMoreService::updateAnalogValues()
522522
{
523523
for (size_t i = 0; i < sizeof(analogIn) / sizeof(analogIn[0]); i++)
524524
{
525+
int samplingCount;
526+
int prevValue;
525527
int value;
526528
if (uBit.io.pin[analogIn[i]].isInput())
527529
{
528530
uBit.io.pin[analogIn[i]].setPull(PinMode::PullNone);
529-
value = (uint16_t)uBit.io.pin[analogIn[i]].getAnalogValue();
530-
if (value == 255)
531+
// for accuracy, read more than 2 times to get same values continuously
532+
do
531533
{
532-
// Read again cause it may fail to read correct value.
534+
prevValue = value;
533535
value = (uint16_t)uBit.io.pin[analogIn[i]].getAnalogValue();
534-
}
536+
samplingCount++;
537+
} while (prevValue != value || samplingCount < 4);
535538
analogValues[i] = value;
536539
setPullMode(analogIn[i], pullMode[analogIn[i]]);
537540
}

0 commit comments

Comments
 (0)