I am a .NET Developer but a newbie to micro-controllers and electronics. I have been going through the samples and reading through two books, one of which is Getting Started with the Internet of Things. I am sure that others here are familiar with this. The code below has an issue that I cannot seem to get resolved. When I search the forum I see it mentioned but it seems to be pre release of .NETMF 4.2. The issue is marked below with a comment. I cannot get the 'AnalogInput' class to resolve except through the Microsoft.SPOT.Hardware namespace at which time it wants a type of Cpu.AnalogChannel as an argument. I have tried 'Cpu.AnalogChannel.ANALOG_1' as the argument for this code but it does not work. I am somewhat lost.
I have connected a potentiometer to the Analog In (Pins 0, 1 and 2) but when I run the application the voltage always reads out as 0.00 so either I don't have it hooked up correctly or my code is wrong. I am guessing the latter.
I have seen previous postings where it is mentioned that you should use 'SecretLabs.NETMF.Hardware.AnalogInput' but I am not seeing that class exist. I do notice however that if I use:
var voltagePort = new Microsoft.SPOT.Hardware.AnalogInput(Cpu.AnalogChannel.ANALOG_0);
and then try to declare:
var lowPort = new OutputPort(Pins.GPIO_PIN_A0, false);
I get an error as if the first declaration took the resource on the board and is having a conflict. That makes me think that my code is actually correct and I don't know what I am doing with the potentiometer (that is highly likely).
Below is my current code taken from the book with my mods:
using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.NetduinoPlus; public class VoltageReader { public static void Main() { const double maxVoltage = 3.3; const int maxAdcValue = 1023; //var voltagePort = new AnalogInput(Pins.GPIO_PIN_A1); //THIS HAS AN ISSUE RESOLVING var voltagePort = new Microsoft.SPOT.Hardware.AnalogInput(Cpu.AnalogChannel.ANALOG_1); //REPLACED LINE WITH THIS var lowPort = new OutputPort(Pins.GPIO_PIN_A0, false); var highPort = new OutputPort(Pins.GPIO_PIN_A2, true); while (true) { int rawValue = voltagePort.Read(); double value = (rawValue * maxVoltage) / maxAdcValue; Debug.Print(rawValue + " " + value.ToString("f")); Thread.Sleep(3000); // 3 seconds } } }Any help is appreciate to point me in the right direction.
Thanks!
Don
Edited by Chris Walker, 30 August 2012 - 01:27 AM.
added [code] tags