Assigning an analog input

Hi,
Sorry for this basic question, but I couldn’t find information on how to properly assign and set scale for an input pin.

This is what I have been able to collect so far (and I wondering where is all the related information supposed to be found?)

GT.Socket socket = GT.Socket.GetSocket(9, true, null, null);
var analogIn = new GT.Interfaces.AnalogInput(socket, GT.Socket.Pin.Five, null);

Now to set the scale I found this:

AnalogIn anaIn = new AnalogIn(AnalogIn.Pin.Five);

anaIn .SetLinearScale(0, 10);

The question is how to combine the first 2 statements with the last 2?

Thank you

hiya,

so you have a couple of concepts here you probably want to look through.

GT.Socket socket = GT.Socket.GetSocket(9, true, null, null);

This is just defining a Gadgeteer socket object, using socket 9.

var analogIn = new GT.Interfaces.AnalogInput(socket, GT.Socket.Pin.Five, null);

this creates a VAR object (not a “real” type). It happens to be a Gadgeteer AnalogInput type, and is the Gadgeteer way of defining an AnalogIn pin based on an existing socket.

If you look at the Gadgeteer references to AnalogInput, http://www.netmf.com/gadgeteer/docs/gadgeteercore/html/42848af9-1785-a78d-68ce-c453e3e2c310.htm you will see that there’s no scaling method; the scaling you refer to later is related to the GHI AnalogIn implementation. So you can implement your own scaling function that uses either the voltage or proportion value that the Gadgeteer type exposes.

There’s also another method you want to look at when using GetSocket, and that is ReservePin. Here’s an example of how you could achieve what you want without using Gadgeteer’s AnalogInput:

AnalogIn anaIn = new AnalogIn((AnalogIn.Pin)socket.ReservePin(Gadgeteer.Socket.Pin.Five, null));
anaIn.SetLinearScale(0, 10);

hope that’s helped!

Hi Brett,
Thanks for your reply. So does that mean that I can either of these approaches (Interfaces.AnalogInput or AnalogIn)?
What is the difference between them? There is not enough documentation on this as far as I could find.

What I was wondering about here:
If I use (AnalogIn) do I need to define a socket? Do I need to refer to the actual pin number in the processor here and not in the socket?
If I use (Interfaces.AnalogInput) do I understand you correctly that I don’t need to set a scale in forehand? In this case do I just make scale division later after I read my Analog value?

If there is a working example that could help a lot.

Thanks again

@ TJAG - Hiya, what board are you using and what are you trying to do?
With that i could whip up an example.

The last code I added requires you to use GetSocket.

There’s lots of examples and documentation, but I just suspect that you’re still early in your understanding of how all these things work together and how you would put these pieces all together - unfortunately you might have picked on a “curly” one to start :slight_smile:

Fundamentally, either approach allows you to read an analog input in your code. I assume that’s what you want to do, right?

The difference is that one is using the Gadgeteer interface, and this means that it might be more “hardware agnostic” than the GHI specific implementation. What is the right way to do this? Well, the Gadgeteer method works perfectly when you have lots of other Gadgeteer modules. If you aren’t using Gadgeteer, then there’s no need to use Gadgeteer (adding more references consumes memory unnecessarily). But if you want to write one piece of code and have it work on multiple Gadgeteer devices irrespective of whose hardware platform it is, you might want to choose the Gadgeteer approach.

Getting back onto how to pick up the info about this. Have you been through the references at http://www.ghielectronics.com/support/dotnet-micro-framework as well as the tutorials ? There’s a lot of information there, but by starting on this knowledge early you should start to understand some of those more foundation concepts to start - they’re well worth the effort to go through them.

We’re all happy to help here, just let us know more about what you’re trying to achieve so we know how we might better guide you.

Thank you Brett and Justin,
Brett, I did search for answers in documentation but couldn’t find the explanation that you provided for example. I had seen the example in the link you provided. I couldn’t locate a socket initiation in it. I’ll do some more searching.

Justin, I am using a spider. I would like to read the output signal from a pressure sensor. Appreciate it.

Thanks

@ TJAG - This should read the the sensor connected to socket 9 and pin 5 every second (code not tested)


using Microsoft.SPOT;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

namespace AnalogTest
{
    public partial class Program
    {
        private Gadgeteer.Interfaces.AnalogInput _magicalPressureSensor;
        private GT.Socket _magicalPressureSensoreSocket;
        private GT.Timer _timer;
        private double _magicalPressureSensorValue;

        void ProgramStarted()
        {
            _magicalPressureSensoreSocket = GT.Socket.GetSocket(9, true, null, null);
            _magicalPressureSensor = new GT.Interfaces.AnalogInput(_magicalPressureSensoreSocket, GT.Socket.Pin.Five, null);
            _timer = new GT.Timer(1000);
            _timer.Tick += new GT.Timer.TickEventHandler(TimerTick);
            _timer.Start();
        }

        void TimerTick(GT.Timer timer)
        {
            _magicalPressureSensorValue= _magicalPressureSensor.ReadVoltage();
            Debug.Print(_magicalPressureSensorValue.ToString());
        }
    }
}

Thanks Justin : )

@ TJAG - no worries, let us know now you get on.

@ TJag, my only point about the examples was that if you have spent time going through them, you’d start to understand the [em]why[/em] behind the code. Also by looking at the API references you’d get an understanding of how the Gadgeteer interface differs and whether there was something in the way you wanted to use the sensor reading that made it better to be used one way of the other.

I hope Justin’s code has helped you out!

scaling can also be done be good old maths

Y = MX + C

where

Y = resultant
X = Input Value
M = linear rate Y2-Y1 / X2-X1
C = Offset



// Where result could be a scaled value between min max values of say 0-250 deg c
                Result = ((Raw Value) * ((ScaledMax - ScaledMin) / (InputMax - InputMin))) + Offset;