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

Assign Analog input value to PWM output


  • Please log in to reply
3 replies to this topic

#1 fileark

fileark

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationNM

Posted 18 April 2012 - 12:09 AM

Hello all. I would like to read the output from a Sharp IR distance sensor and output that value to a LED using PWM. As the distance increases or decreases the LED will glow brighter or dimmer.
Here is what I have so far, as you can see this project will later include some servos but they are unused at this time.
The error that I get is on the line where I set the duty cycle. “pwm.SetDutyCycle(irValue);” the error is Cannot convert int to uint. I tried making my variable “irValue” a uint but then it has trouble reading the analog pin.

Is there a convert or case that I can use? For instance there is a ToString function that works but not something that can convert an int to a uint. Can I even use the DutyCycle like I am wanting to? I have found some very confusing Netduino PWM articles and not one of them use PWM in the same manner as the other.

Sorry I am a beginner with .net so after hours of searching I decided to call it a day and hopefully someone here can answer this easily.


using System;
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;
using Servo_API;

namespace ServoKnob
{
    public class Program
    {
        public static void Main()
        {
            Servo servo = new Servo(Pins.GPIO_PIN_D9);
            AnalogInput irSensor = new AnalogInput(Pins.GPIO_PIN_A0);
            int irValue;

            PWM pwm = new PWM(Pins.GPIO_PIN_D10);

            while (true)
            {
                irValue = irSensor.Read();
                irValue = irValue / 5;
                pwm.SetDutyCycle(irValue);

                // servo.Degree = irValue;
                Debug.Print(irValue.ToString());
            }
        }
    }
}

Thanks!

#2 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 18 April 2012 - 01:13 AM

Hi fileark,

Without a circuit diagram and with this small snippet of code it's difficult to give you a clear path to follow. I do understand your frustration, as I am also fairly new as an active community member. It took me some to get a bit of grasp on some of the basics, but what I really found helpful were all the examples and the fantastic Netduino community support. There are a lot of examples in the forum, but this can be a mixed blessing since there's so much code to filter through. Here are a couple of links that I think may help you with your current project. I don't have any specific code for a Sharp IR sensor, but there are several examples of reading Analog Inputs that should be applicable and transferable to your needs.

You may want to try this Blinking LED example which also discusses setting the brightness of an LED using PWM, further down on the page. SetDutyCycle() takes an unit value from 0 to 100, which essentially is turning the LED on and off very quickly at a certain percentage of time.

From there you may want to invest in the Getting Started with Netduino eBook, which incidentally is 50% at O'Reilly. Just use the code 4CAST in the shopping cart when you check out and the discount will be applied to your order. It's a great buy and was written by the maker of Netduino, Chris Walker.

Chapter 6 of that book goes through an example where you read an Analog value from a potentiometer and uses that value to set the intensity of an LED, which is almost identical to your needs. It also shows the process of casting the int value received from the AnalogInput into a uint which is required by SetDutyCycle(). Essentially it would look like this in your code, assuming your irValue is between 0-100:

pwm.SetDutyCycle((unint)irValue);

The book also has a great section on servos which will come in handy for the next portion of your project. In the mean time you can also check out this Servo Tutorial found on the Netduino Wiki, and some there is a great servo explanation found on the Servocity.com tutorial, and another on Pete Brown's PWM blog post.

I hope this helps, and can get you pointed in the right direction.

Cheers,
Steve

#3 Stefan W.

Stefan W.

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts

Posted 18 April 2012 - 03:49 PM

To answer "how do you convert an int to an unsigned int", you cast it: (that's the (uint)
            while (true)
            {
                irValue = irSensor.Read();
                irValue = irValue / 5;
                if (irValue > 100)
                    irValue = 100;
                pwm.SetDutyCycle((uint)irValue);
            }
I also added a safeguard so that the value doesn't get bigger than 100 (100 is full-on for pwm).
I believe that no discovery of fact, however trivial, can be wholly useless to the race, and that no trumpeting of falsehood, however virtuous in intent, can be anything but vicious.
-- H.L. Mencken, "What I Believe"

#4 fileark

fileark

    Member

  • Members
  • PipPip
  • 12 posts
  • LocationNM

Posted 18 April 2012 - 04:10 PM

Thanks Stefan that is indeed how to cast to a uint and it works! Thanks Gutworks for the tips, I had downloaded the Getting Started with Netduino book and it had an example in there that also the (uint) cast. For some reason I didn't find that just searching the web last night. Thanks for all the help guys! Here is the final code that I believe I will use, the "irSensor.SetRange(0, 100);" will scale the analog input so that it is between 0 and 100. public static void Main() { AnalogInput irSensor = new AnalogInput(Pins.GPIO_PIN_A0); PWM pwm = new PWM(Pins.GPIO_PIN_D10); irSensor.SetRange(0, 100); int irValue; while (true) { irValue = irSensor.Read(); pwm.SetDutyCycle((uint)irValue); Debug.Print(irValue.ToString()); } }




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.