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());
}
}
}