Reading an Analog Input

Yesterday I received my first GHI Electronics product, namely a FEZ Spider II Tinker Kit.
I have worked through part of the .NET Gadgeteer for Beginners document, and have found
that the code in section 12, ‘Reading an Analog Input’, on page 35, (Example 1), causes exceptions as follows.
(I removed the LightSense related code because the 2 A ports are used for the DisplayT43 and the Joystick)

Using mainboard GHI Electronics FEZ Spider II version 1.0
#### Exception System.ArgumentException - 0x00000000 (4) ####
#### Message:
#### Microsoft.SPOT.Hardware.AnalogInput::.ctor [IP: 0076] ####
#### Gadgeteer.SocketInterfaces.NativeAnalogInput::set_IsActive [IP: 002e] ####
#### Gadgeteer.SocketInterfaces.NativeAnalogInput::ReadVoltage [IP: 0005] ####
#### Gadgeteer.SocketInterfaces.AnalogInput::ReadProportion [IP: 0004] ####
#### Gadgeteer.Modules.GHIElectronics.Joystick::GetPosition [IP: 0008] ####
A first chance exception of type ‘System.ArgumentException’ occurred in Microsoft.SPOT.Hardware.dll
An unhandled exception of type ‘System.ArgumentException’ occurred in Microsoft.SPOT.Hardware.dll

I found some code on the forum (see Example 2), and I was able to get it working.

I am using the following:

.NET Gadgeteer SDK ## , 2015 R1 , Built 2015-08-25 , Requires Gadgeteer SDK 2.43.1000 , Version 4.3.7.10

.NETMF V4.3
In FEZ Config, check device for update reports TinyBooter 4.3.7.7 and TinyCLR 4.3.7.10

My knowledge is very limited at this stage. I will appreciate some guidance towards getting the
code in the first example to work, unless the reason for it not working is bug-related.

// Example 1

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Input;

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

namespace GadgeteerAnalogInput
{
    public partial class Program
    {
        void ProgramStarted()
        {
            // Draw a border
            displayT43.SimpleGraphics.DisplayRectangle(
                GT.Color.Magenta, 2, GT.Color.Black, 1, 1, 100, 100);
            Thread GraphThread = new Thread(GraphLoop);
            GraphThread.Start();
        }
        void GraphLoop()
        {
            int x = 2;
            int LastJoyX = 0, LastJoyY = 0;
            while (true)
            {
                int JoyX = (int)(joystick.GetPosition().X * 15) + 40; // Scale and cast
                displayT43.SimpleGraphics.DisplayLine(GT.Color.Red, 1,
                    x - 1, LastJoyX, x, JoyX); // Draw
                LastJoyX = JoyX;
                int JoyY = (int)(joystick.GetPosition().Y * 15) + 70; // Scale and cast
                displayT43.SimpleGraphics.DisplayLine(GT.Color.Green, 1,
                 x - 1, LastJoyY, x, JoyY); // Draw
                LastJoyY = JoyY;
                if (x++ > 100)
                {
                    x = 2;
                    // Draw a border and clear the inside
                    displayT43.SimpleGraphics.DisplayRectangle(
                        GT.Color.Magenta, 2, GT.Color.Black, 1, 1, 100, 100);
                }
                Thread.Sleep(30);
            }
        }
    }
}

// Example 2

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Input;

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

namespace GadgeteerAnalogInput
{
    public partial class Program
    {
        AnalogInput lightSensor;
        GT.Timer LSTimer = new GT.Timer(100);
        double lightSensorReading = 0;

        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            lightSensor = new AnalogInput((Cpu.AnalogChannel)Cpu.AnalogChannel.ANALOG_7);
            LSTimer.Tick += LSTimer_Tick;
            LSTimer.Start();
        }

        void LSTimer_Tick(GT.Timer timer)
        {
            Loop();
        }

        void Loop()
        {
            lightSensorReading = lightSensor.Read();
                Debug.Print(lightSensorReading.ToString());
        }
    }
}

Thank you Andre.

I used Gadgeteer to configure the hardware. I am assuming you want to see the generated code below.

namespace GadgeteerAnalogInput {
    using Gadgeteer;
    using GTM = Gadgeteer.Modules;
    
    
    public partial class Program : Gadgeteer.Program {
        
        /// <summary>The USB Client EDP module using socket 1 of the mainboard.</summary>
        private Gadgeteer.Modules.GHIElectronics.USBClientEDP usbClientEDP;
        
        /// <summary>The Joystick module using socket 9 of the mainboard.</summary>
        private Gadgeteer.Modules.GHIElectronics.Joystick joystick;
        
        /// <summary>The Display T43 module using sockets 14, 13, 12 and 10 of the mainboard.</summary>
        private Gadgeteer.Modules.GHIElectronics.DisplayT43 displayT43;
        
        /// <summary>This property provides access to the Mainboard API. This is normally not necessary for an end user program.</summary>
        protected new static GHIElectronics.Gadgeteer.FEZSpiderII Mainboard {
            get {
                return ((GHIElectronics.Gadgeteer.FEZSpiderII)(Gadgeteer.Program.Mainboard));
            }
            set {
                Gadgeteer.Program.Mainboard = value;
            }
        }
        
        /// <summary>This method runs automatically when the device is powered, and calls ProgramStarted.</summary>
        public static void Main() {
            // Important to initialize the Mainboard first
            Program.Mainboard = new GHIElectronics.Gadgeteer.FEZSpiderII();
            Program p = new Program();
            p.InitializeModules();
            p.ProgramStarted();
            // Starts Dispatcher
            p.Run();
        }
        
        private void InitializeModules() {
            this.usbClientEDP = new GTM.GHIElectronics.USBClientEDP(1);
            this.joystick = new GTM.GHIElectronics.Joystick(9);
            this.displayT43 = new GTM.GHIElectronics.DisplayT43(14, 13, 12, 10);
        }
    }
}

@ Oldevel - There is a bug in the FEZ Spider II Gadgeteer mainboard driver. To work around it, add the below line as the first line in ProgramStarted.


Gadgeteer.Socket.SocketInterfaces.SetAnalogInputFactors(Gadgeteer.Socket.GetSocket(9, false, null, null), 3.3, 0.0, 12);

That worked, thanks. I’d like to know more about this.

What others like this are there, specifically relating to DisplayT43, Touch, Wireless Modules, and GSM/GPRS Modules.

@ Oldevel - I’m not sure what you would like to know more about.

@ John - I believe I should send you a direct message, I’ll do so one I have enough experience points :wink:

1 Like

@ Oldevel - You can always email support@ ghi… and reference this thread.