File tree Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Expand file tree Collapse file tree 1 file changed +7
-4
lines changed Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments