Main Site Documentation

Overriding the AnalogIn class


#1

I’m starting work on connecting my EMX to a TI ADS1158 ADC. This is a 16-bit 16-channel ADC which I need for a several analog inputs that require 16-bit resolution.

Since the AnalogIn has a constructor that takes one parameter (the analog pin) I can’t simply derive from this class. It would be nice if AnalogIn was an interface…

So in my own implementation of this class for an external ADC I assume the math for SetLinearScale is simply the ADC 10 bit value divided by the difference between the minValue and maxValue parameters. In other words…((maxValue-minValue)/4096) * adcValue?

Thanks
Bob


#2

It probably something like:


        private static int Scale(int oldValue, int oldMin, int oldMax, int newMin, int newMax)
        {
            if (oldValue < oldMin || oldValue > oldMax)
                throw new ArgumentOutOfRangeException("oldValue");
            return ((oldValue - oldMin) * (newMax - newMin) / (oldMax - oldMin)) + newMin;
        }