Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

New command for SetRange?

AnalogInput SetRange

  • Please log in to reply
4 replies to this topic

#1 Basset

Basset

    New Member

  • Members
  • Pip
  • 7 posts
  • LocationCanada

Posted 12 January 2013 - 04:31 AM

Hello, I just got my Netduino plus 2 and the Getting started with Netduino book.  I am a beginner and have been reading the forums a bit and have seen that old code from the Netduino plus sometimes won't work on the plus 2.  I am in need of some help updating the potentiometer example used in the book which can be seen below:

        public static void Main()        {            // write your code here            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);            AnalogInput pot = new AnalogInput(Pins.GPIO_PIN_A0);            pot.SetRange(100, 250);            int potValue = 0;            while (true)            {                //read the value of the pot                potValue = pot.Read();                //blink the led based on the pot's value                led.Write(true);                Thread.Sleep(potValue);                led.Write(false);                Thread.Sleep(potValue);            }        }

 

The problems I am having are with AnalogInput which I fixed below, the [font="'courier new', courier, monospace;"]SetRange()[/font] command, and the pots value is different from the 0 to 1023 range used in the book.  I did some debugging and with [font="'courier new', courier, monospace;"]pot.Read()[/font] it gives me values from 0 to 1, and with [font="'courier new', courier, monospace;"]pot.ReadRaw()[/font] it gives me a range of 0 to 4095.  I fixed the AnalogInput from looking at other posts on the forum, and fixed reading the pots value with [font="'courier new', courier, monospace;"]pot.ReadRaw()[/font] but can't find an equivalent command for [font="'courier new', courier, monospace;"]SetRange. [font="arial, helvetica, sans-serif;"] Here is my code:[/font][/font]

 
        public static void Main()        {            // write your code here            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);            AnalogInput pot = new AnalogInput(AnalogChannels.ANALOG_PIN_A0);            int potValue = 0;            while (true)            {                potValue = pot.ReadRaw();                //read the value of the potentiometer                //blink the led based on the potentiometer's value                led.Write(true);                Thread.Sleep(potValue);                led.Write(false);                Thread.Sleep(potValue);            }        }

 

 

I got the led to blink based on the pot and now just need to set the range between 100ms delay and 250ms delay.  Any help would be great.

 

Thanks!



#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 12 January 2013 - 09:21 AM

Hi Basset,

Welcome to the Netduino community.

Two options for you:

1. Upgrade your Netduino Plus 2 to the new 4.2.2 firmware, and grab the SecretLabs.NETMF.Hardware.AnalogInput assembly from the second post.
http://forums.netdui...-firmware-v422/

Add that assembly to your project, and you'll be able to use the code in the book (including the SetRange method) without any issues. You will want to add "SecretLabs.NETMF.Hardware." in front of the "AnalogInput" name to make sure that .NET uses the SecretLabs class instead of the new built-in NETMF AnalogInput class.

2. The new NETMF AnalogInput class has a constructor which accepts "offset" and "scale" parameters. Set "offset" to 100 and "scale" to 150 (i.e. 250-100), and you should get the results you seek.

Chris

#3 Basset

Basset

    New Member

  • Members
  • Pip
  • 7 posts
  • LocationCanada

Posted 12 January 2013 - 05:30 PM

Awesome, thanks for the help!  I got it all working now using the new firmware.  Here's the code that I used in case someone else has the same problem:

 

using System.Net;using System.Net.Sockets;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;//Make sure to add the 4.2.2 assemblies to your references if you're not on the 4.2.2 sdk.namespace Light_Pot{    public class Program    {        public static void Main()        {            // write your code here            OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);            SecretLabs.NETMF.Hardware.AnalogInput pot = new SecretLabs.NETMF.Hardware.AnalogInput(Pins.GPIO_PIN_A0);            pot.SetRange(100, 250);            int potValue = 0;            while (true)            {                potValue = pot.Read();                //read the value of the potentiometer                //blink the led based on the potentiometer's value                led.Write(true);                Thread.Sleep(potValue);                led.Write(false);                Thread.Sleep(potValue);            }        }    }}

 

 



#4 Dan Olson

Dan Olson

    New Member

  • Members
  • Pip
  • 3 posts

Posted 27 January 2013 - 08:25 PM

I have a NetDuino plus 2, which I just updated to the 4.2.2.0 firmware.  I tried to run the code above and got the following error message:

 

 

Error 1 The type or namespace name 'AnalogInput' does not exist in the namespace 'SecretLabs.NETMF.Hardware' (are you missing an assembly reference?) C:UsersDandocumentsvisual studio 2010Projectsanaloginput1analoginput1Program.cs 19 39 analoginput1
 
Chris, I don't understand what you mean when you say, "[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]grab the SecretLabs.NETMF.Hardware.[/color]AnalogInput[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;] assembly from the second post."  Could you post a direct link?  How do I "grab an assembly?"[/color]
 
[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]Sorry for the noob questions.[/color]
-Dan


#5 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 27 January 2013 - 08:38 PM

Hi Dan,

How do I "grab an assembly?"   Sorry for the noob questions.

No worries. Here you go. 1. Click on the "Project Menu > Add Reference..." 2. On the first (.NET) tab, select the "SecretLabs.NETMF.Hardware.AnalogInput" assembly. 3. Add it to your project. Chris





Also tagged with one or more of these keywords: AnalogInput, SetRange

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.