Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7874275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:45:53+00:00 2026-06-03T02:45:53+00:00

Background: We have an embedded system that converts linear positions (0 mm – 40

  • 0

Background:
We have an embedded system that converts linear positions (0 mm – 40 mm) from a potentiometer voltage to its digital value using a 10-bit analog to digital converter.

    ------------------------
0mm |                      | 40 mm
    ------------------------

We show the user the linear position at 1 mm increments. Ex. 1mm, 2mm, 3mm, etc.

The problem:
Our system can be used in electromagnetically noisy environments which can cause the linear position to “flicker” due to noise entering the ADC. For example, we will see values like: 39,40,39,40,39,38,40 etc. when the potentiometer is at 39 mm.

Since we are rounding to every 1 mm, we will see flicker between 1 and 2 if the value toggles between 1.4 and 1.6 mm for example.

Proposed software solution:
Assuming we can not change the hardware, I would like to add some hysteresis to the rounding of values to avoid this flicker. Such that:

If the value is currently at 1mm, it can only go to 2mm iff the raw value is 1.8 or higher.
Likewise, if the current value is 1mm it can only go to 0mm iff the raw value is 0.2 or lower.

I wrote the following simple app to test my solution. Please let me know if I am on the right track, or if you have any advice.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PDFSHysteresis
{
    class Program
    {
        static void Main(string[] args)
        {
            double test = 0;
            int curr = 0;
            Random random = new Random();

            for (double i = 0; i < 100; i++)
            {
                test = test + random.Next(-1, 2) + Math.Round((random.NextDouble()), 3);
                curr = HystRound(test, curr, 0.2);

                Console.WriteLine("{0:00.000} - {1}", test, curr);
            }

            Console.ReadLine();
        }

        static int HystRound(double test, int curr, double margin)
        {

            if (test > curr + 1 - margin && test < curr + 2 - margin)
            {
                return curr + 1;
            }
            else if (test < curr - 1 + margin && test > curr - 2 + margin)
            {
                return curr - 1;
            }
            else if (test >= curr - 1 + margin && test <= curr + 1 - margin)
            {
                return curr;
            }
            else
            {
                return HystRound(test, (int)Math.Floor(test), margin);
            }
        }
    }
}

Sample output:

Raw      HystRound
======   =========
00.847   1
00.406   1
01.865   2
01.521   2
02.802   3
02.909   3
02.720   3
04.505   4
06.373   6
06.672   6
08.444   8
09.129   9
10.870   11
10.539   11
12.125   12
13.622   13
13.598   13
14.141   14
16.023   16
16.613   16
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:45:56+00:00Added an answer on June 3, 2026 at 2:45 am

    How about using the average of readings for the last N seconds, where N could be fairly small / sub-second depending on your sample rate?

    You can use a simple linear average, or something more complex, depending on your needs. Several moving average algorithms are detailed on Wikipedia:

    http://en.wikipedia.org/wiki/Moving_average

    Depending on your sensitivity / responsiveness needs, you could reset the average if a new reading exceeds the running average by X%.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background I have a dimension table that has a single record for each day.
Background I have a custom collection that is binded to the datagridview this.datagridview.DataSource =
Background: I have a WCF service deployed on my local machine that in turns
Background : I have a asp.net webapplication project that should contain a public and
I have an image (embedded resource) that i can get access to and form
I have a Movieclip with about five FLV movies embedded on its timeline, one
I have several images that i want to be Embedded into the exe. When
I have a CSS file that is embedded in my assembly. I need to
I have a reconciliation process where by a background thread that periodically retrieves a
I have flash object embedded to my page and it should act as background

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.