Analog in to Text

How do you convert an Analog input to Text to be displayed on the Touch screen
this is what I have no errors but …


using System;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GHIElectronics.NETMF.Glide;
using GHIElectronics.NETMF.Glide.Display;
using GHIElectronics.NETMF.Glide.UI;


namespace RangeFinderScreen
{
    

    public partial class Program
    {
        static GT.Interfaces.AnalogInput Ain;
        static GHIElectronics.NETMF.Glide.Display.Window window;
        private GHIElectronics.NETMF.Glide.UI.TextBox _Vin;
        private GHIElectronics.NETMF.Glide.UI.TextBox _Range;
       

        void ProgramStarted()
        {
          // Do one-time tasks here
            Debug.Print("Program Started");
            GlideTouch.Initialize();
            // Load the Window XML string.
            window = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.Window));
            // Resize any loaded Window to the LCD's size.
            Glide.FitToScreen = true;
            // Assign the Window to MainWindow; rendering it to the LCD.
            Glide.MainWindow = window;
           // Get the TextBox
             _Vin = (GHIElectronics.NETMF.Glide.UI.TextBox)window.GetChildByName("Vin");
             _Range = (GHIElectronics.NETMF.Glide.UI.TextBox)window.GetChildByName("Range");
             Ain = extender.SetupAnalogInput(GT.Socket.Pin.Five);
          double AinS = 0;
             AinS = Ain.ReadVoltage();
            String s = AinS.ToString(_Vin.Text);

            
     


        }
    }
     
  
    

    }
  

Bug in your code…you’re never actually assigning the string to the textbox.

Try changing:

String s = AinS.ToString(_Vin.Text);

to:

_Vin.Text = AinS.ToString();

If you still have trouble with that, try assigning a string constant to the textbox’s Text property instead, like so:

_Vin.Text = "Vin Test Value";
_Range.Text = "Range Test Value";

HTH!

Thanks there seems to be a major gap in my learning I need to identify

is there a way to do a simple test to see if I am even reviving the Analog input it is an Analog Voltage from a ultra sonic range finder

I have it running of the 3.3v pin of the extender which should be sending values of 6.4mV per inch of distance

OK so now I an testing this



using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace SerialTest
{
    public partial class Program
    {

        static GT.Interfaces.AnalogInput An;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {

            An = extender.SetupAnalogInput(GT.Socket.Pin.Five);
            Debug.Print("Program Started");
            double AnS = 0;
            
                AnS = An.ReadVoltage();
                while (true)
                {
                    string s = AnS.ToString();
                    Debug.Print(s);
                    Thread.Sleep(100);

                }

            


            }
        }
    }

and I am getting 1.869140625
in the debug window that is something I guess but it does not change when something is in front of the sensor

You have to read voltage inside the loop. Right now you have only read it once and then just showing same value inside the loop.

Also I would run it in a separate thread, but for testing while loop inside ProgramStarted should work.

Ah! Good catch, Architect!

I was only focused on the syntactical stuff, and missed that.

Sean,

In addition to using a loop, you can also check the value of the sensor using a timer. You can see an example of this in the following project:

http://code.tinyclr.com/project/407/gadgeteer-sounds-using-eblock-piezos/

Here’s a snippet of the code:

        

void ProgramStarted()
        {
            // Do one-time tasks here
            Debug.Print("Program Started");
 
            Piezo1 = eBlockExpansion.SetupPWMOutput(GT.Socket.Pin.Nine);
            Piezo2 = eBlockExpansion1.SetupPWMOutput(GT.Socket.Pin.Eight);
 
            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
            button.ButtonReleased += new Button.ButtonEventHandler(button_ButtonReleased);
 
            timer = new GT.Timer(100);
            timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
        }
 
        void timer_Tick(GT.Timer timer)
        {
            byte Pot1Pct = (byte)(potentiometer.ReadPotentiometerPercentage() * 10);
            byte Pot2Pct = (byte)(potentiometer1.ReadPotentiometerPercentage() * 10);
 
            Piezo1.Set(440 * Pot1Pct, 50);
            Piezo2.Set(523 * Pot2Pct, 50);
        }

In the project, I’m using a couple of potentiometers (they’re initialized in the program.generated.cs) to control the relative pitch of a couple of eblock piezos. I set up a timer in ProgramStarted, and each time the timer ticks, I read the pots. In my case, I’m calling a method that gives me a percentage based on the voltage being returned, but you could replace those lines in the snippet above with your call to:

AnS = An.ReadVoltage();
string s = AnS.ToString();
Debug.Print(s);

Well I can now get a value but I Still need to figure out if this is even a workable solution

I am guessing you can not use Serial on the Extender module?

But this is what I have now


using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace SerialTest
{
    public partial class Program
    {
        static GT.Interfaces.AnalogInput Anin;
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            Debug.Print("Program Started");
            Anin = extender.SetupAnalogInput(GT.Socket.Pin.Five);          
            while (true)
            {
                 string s = Anin.ReadVoltage().ToString();
                 Debug.Print(s);
                 Thread.Sleep(500);
            }

        }
    }
}

and I get this out put

Program Started
0.51240234374999993
0.074121093749999992
0.083789062499999997
0.11923828124999999
0.090234374999999992
0.074121093749999992
0.070898437499999994
0.045117187499999996
0.090234374999999992
0.038671874999999994
0.08056640625
0.022558593749999998
0.045117187499999996
0.045117187499999996
0.038671874999999994
0.041894531249999999
0.074121093749999992
0.061230468749999996
0.04833984375
0.058007812499999999
0.012890624999999999
0.054785156249999994
0.019335937499999997
0.070898437499999994
0.077343749999999989
0.035449218749999997
0
The program ‘[5] Micro Framework application: Managed’ has exited with code 0 (0x0).

I think I need to take a break :slight_smile:

thanks for everyone help

I assume you mean SerialPort but I don’t see where you’re trying to do anything like that… If that is what you mean then you’ll have to put the Extender in a U socket and use the SerialPort class and explicitly tell it which pins to use.

Sean,

I’m assuming that the sensor you’re using is a non-gadgeteer sensor, and you’re connecting it via the extender?

If so, then there are several things to double-check. One is that you have the sensor wired up correctly (if it’s a 3-wire connector, you’d run ground to ground on the extender, power to either 5V or 3.3V, and signal to the pin you want to use on the extender (looks like you’re using pin 5). Then, since you’re looking for an analog input, you need to make sure you’re plugged into a socket marked “A”.

If you haven’t seen it already, Architect posted a nice chart of the Gadgeteer socket spec, from the docs on codeplex, over on the Wiki:

http://wiki.tinyclr.com/index.php?title=Gadgeteer_Sockets

You could try serial the way Ian mentioned, but not sure you should give up on analog that easy. Have you scoped the output to see whether what the sensor is outputting actually matches (or at least aligns) with what the voltage output to the extender is reading?

One more thing to note…looping with while(true) is not a recommended pattern with Gadgeteer, as it will prevent any events you’ve registered for from firing. See here for more info:

http://blogs.msdn.com/b/net_gadgeteer/archive/2011/12/19/why-not-while-true.aspx

Instead, register for event handlers on modules that support raising them, and for modules that don’t (or sensors like the one you’re using) use a timer instead of the loop.

Thanks! not giving up just taking a break I spent the first part of the day working on an Arduino wow what a mess that thing is. A C++(or is is it C) programmer I am not! Well not much of a C# programmer but I under stand it sort of :slight_smile:

Yes I am using the Extender its a big change for me. On the Netduino for reading analog voltages you need to have a reference voltage. I believe I have it wired correctly but I think I need to do some math on the input to get the right values and I did not have the right value capacitor to clean up the noise issues I was having

I was asking about SerialPort because this range finder supports that sort of from the data sheet “Although the voltage of 0- Vcc is outside the RS232 standard”

Yea the while(true) was a cop out after failing on the event handlers.

This is one confusing - or is it “confused” - device, the maxbotix sonar (from my 2min look at the data sheet you linked to).

First there are a few things you need to take away from reading that document.

[quote]All interfaces are active
simultaneously
· Serial, 0 to Vcc, 9600Baud, 81N
· Analog, (Vcc/512) / inch
· Pulse width, (147uS/inch)[/quote]
So the device can be used by reading a serial, OR analog, OR a PWM signal (or if you wanted redundancy, all of them :slight_smile: ) but you only really need to read ONE of them to read the signal. You are currently using Analog, so you really don’t need any of the others.

[quote]Although the voltage of 0-
Vcc is outside the RS232 standard, most RS232 devices have
sufficient margin to read 0-Vcc serial data. If standard voltage level
RS232 is desired, invert, and connect an RS232 converter such as a
MAX232.[/quote]
This to me says that you can use a MAX232 to get real RS232 signal, but this on its own really only outputs TTL serial UART. Therefore you could connect it to the Rx pin on your device (however you chose - thru extender I assume since we’re talking Gadgeteer, but it most probably needs a UART capable port) and read the serial stream of data if you wanted to.

[quote]RX - This pin is internally pulled high. The EZ0™ will continually
measure range and output if RX data is left unconnected or held high.
If held low the EZ0™ will stop ranging. Bring high for 20uS or more
to command a range reading.[/quote]
This one was interesting to me - you could use it as a switch if you chose to, to turn of the uSonic function.

[quote]An - Outputs analog voltage with a scaling factor of (Vcc/512) per
inch. A supply of 5V yields ~9.8mV/in. and 3.3V yields ~6.4mV/in.[/quote] depending if you have VCC connected to your Fez 3v3 or 5v pin will mean you get a different voltage read in your code for an object of the same distance. To sanity check this, if you have an object say 60 inches (5 feet - i think; I live in metric-land :slight_smile: ) away from the sonar you should see approx 588mV or 384mV based on the above information.

hope that helps