Raw analog value?

Can you please tell me which method to use to get the Raw analog value from FezUtility and FezHat? (not the scaled 0 to 1 value)

Is that even possible on Windows? Why do you need the raw value?

I will like to modify this method to use it on a heating element whose temperature ranges from 0.0 to 212.6 F°

private static Int32 _scaleLow, _scaleHigh;         // Lower/Upper bound of scale
        private const Int32 MinValue = 0, MaxValue = 4095;  // Min/Max values returned by ADC

        public static void Main()
        {
            SetScale(-127, 127);
            Debug.Print("0 -> " + Scale((Int32) 0.0));
            Debug.Print("4095 -> " + Scale((Int32) 4095.0));
            Debug.Print("2048 -> " + Scale((Int32) 2048.0));

            Thread.Sleep(Timeout.Infinite);
        }

        public static void SetScale(Int32 low = MinValue, Int32 high = MaxValue)
        {
            if (((high - low) > MaxValue) || (low >= high)) { throw new ArgumentOutOfRangeException("SetScale"); }
            _scaleLow = low;
            _scaleHigh = high;
        }

        private static Int32 Scale(Int32 x)
        {
            return (_scaleHigh - _scaleLow) * (x - MinValue) / MaxValue + _scaleLow;
        }

you do realise that you can use the range 0.0 to 1.0 in your example and be done with it, right? They need to be floating point numbers not integers, but the same scaling capability is possible (and trivial). If you use minvalue=0 and maxvalue=1 as the extremities of the ADC reading, and you set your scale to the expected range of the temperature probe, then you get the adjusted temperature…

I get an Exception: System.ArgumentOutOfRangeException

        private static float _scaleLow, _scaleHigh;         // Lower/Upper bound of scale
        private const float MinValue = 0.0f, MaxValue = 1.0f;  // Min/Max values returned by ADC

        public static void Main()
        {
            SetScale(0.0f, 212.6f);
            Console.WriteLine("0 -> " + Scale(0.0f));
            Console.WriteLine("1.0 -> " + Scale(1.0f));
            Console.WriteLine("0.50 -> " + Scale(0.50f));
        }


        public static void SetScale(float low = MinValue, float high = MaxValue)
        {
            if (((high - low) > MaxValue) || (low >= high)) { throw new ArgumentOutOfRangeException("SetScale"); }
            _scaleLow = low;
            _scaleHigh = high;
        }

        private static float Scale(float x)
        {
            return (_scaleHigh - _scaleLow) * (x - MinValue) / MaxValue + _scaleLow;
        }

Did I got anything wrong in the code. I pretty much follow your instructions Brett.

at what point is the exception thrown? Is it the exception from SetScale?

Whose code is that, that you posted originally? It assumes that the range of the scaled values has to be greater than the MaxValue, which in this case isn’t a valid assumption. You could get rid of the test: (high - low) > MaxValue

Of course you could just calculate this on the fly every time without worrying about these methods…

I found the code on GHI site:
[url]https://www.ghielectronics.com/community/forum/topic?id=23698[/url]

I would really like to have some kind of Analog Scaling code I can use to scale all my analog signals on these two Fez boards.

You need to modify the values in the code to match your case. Otherwise you could have a look at a relevant driver here: [url]https://bitbucket.org/ghi_elect/gadgeteer/src/86e246d051bf5e78ec1923b173410c5e47591bf8/Modules/GHIElectronics/TempHumidity/TempHumidity_43/TempHumidity_43.cs?at=master&fileviewer=file-view-default[/url]

Also worth a look: [url]https://www.ghielectronics.com/docs/8/analog-inputs[/url]

The GHI code is specific for the use case they have - it’s just a calculation. It’s wrapped with methods and exception handling in case people do stupid things

You know everything you need. The calculation just looks like:
(_scaleHigh - _scaleLow) * (value - MinValue) / MaxValue + _scaleLow
(1.0 - 0.0) * (value - 0.0) / 212.6 + 0.0

Of course there’s one other consideration. Your temperature may actually NOT be linear, and this is a linear representation.