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

Two Fading LED's


  • Please log in to reply
15 replies to this topic

#1 Holfy89

Holfy89

    New Member

  • Members
  • Pip
  • 8 posts

Posted 17 April 2012 - 09:16 PM

Hey there, very new to this so bear with me. But would really appreciate any help

I've been successful getting one LED to fade on and off but i'm having trouble with two.
The problem with my code is the first LED fades on and off. STOPS
Then the second LED fades on and off on a loop while the first LED stays constantly on
without fading. (If that makes sense)

What i'm trying to accomplish Is having the two LED's fading alternately but so there's
a continuous flow between them. As if the lights fading from one and into the other.

Also I will most likely have to buy a microphone to plus into the board for this but is
there a way to activate this program when it detects sound?

Any questions just ask
Regards, Ryan

//Libraries that our program uses
using System;                               //use base classes                            
using System.Threading;                     //use threading classes        
using Microsoft.SPOT;                       //use smart personal objects technology classes
using Microsoft.SPOT.Hardware;              //use hardware related SPOT classes
using SecretLabs.NETMF.Hardware;            //use Secret Labs hardware framework
using SecretLabs.NETMF.Hardware.Netduino;   //use the Netduino specific classes

namespace NCIR02                    /// Define the namespace we are in ///
{
    public class Program            /// Define any variables used in the program and subrout
    //ines after Main()///
    {
        static PWM[] leds = new PWM[8];   //An array to hold the 8 pins the LE
        //Ds are connected to

        public static void Main()   /// The Main loop (run at power up) ///
        {
            /// Setup Portion of Program, runs once at startup ///
            //Set each LED pin to be an output
            leds[0] = new PWM(Pins.GPIO_PIN_D9);
            leds[1] = new PWM(Pins.GPIO_PIN_D10);

            while (true)            /// Do Forever ///
            {
                //oneAfterAnotherNoLoop();    //Turn on each LED one by one then turn them off
                oneAfterAnotherLoop();    //This does the same as oneAfterAnotherNoLoop() but with much less typing
                //oneOnAtATime();           //Turns each LED on one after the other
                //inAndOut();               //Displays a Nightrider effect
            }                       /// Close Forever Loop ///            
        }                           /// Close the Main() Loop ///

        /// <summary>
        /// Will light one LED then delay for delayTime then light
        /// the next LED until all LEDs are on it will then turn them off one after another.
        /// This does not use a loop and is similar to the method used in NCIR01
        /// </summary>
        static void oneAfterAnotherLoop()
        {

            int delaytime = 30;

            for (uint i = 0; i < 100; i++)  //Loop to fade from off to full on
            {
                leds[0].SetDutyCycle(i);        //Sets the duty cycle to the variable i (0-100)
                Thread.Sleep(delaytime);           //wait for 15 milliseconds
    
            }
            for (uint i = 100; i > 0; i--)  //Loop to fade from full on to off
            {
                leds[0].SetDutyCycle(i);        //Sets the duty cycle to the variable i (100-0)
                Thread.Sleep(delaytime);           //wait for 15 milliseconds


                for (uint f = 0; f < 100; f++)
                {
                    leds[1].SetDutyCycle(f);
                    Thread.Sleep(delaytime);

                }
                for (uint f = 100; f > 0; f--)
                {
                    leds[1].SetDutyCycle(f);
                    Thread.Sleep(delaytime);
                }

            }
        }                           /// Close Forever Loop ///   
    }                               /// Close the Main() Loop ///
}                                   /// Close the Program Loop ///
/// Close the Namespace Loop ///


#2 ErikN

ErikN

    Advanced Member

  • Members
  • PipPipPip
  • 119 posts
  • LocationNew York, NY

Posted 17 April 2012 - 10:55 PM

You can't do this with a single thread. You'll need to use 2 threads (and probably want to synchronize them in case one rushes the other by a little bit; otherwise the time difference could grow greater and greater with each loop.) I'd do it like this: MainThread: Prepare things [PWM ports, semaphore or reset events for synchronization] Create 2 threads; one to execute the dimming of LED 1, one to execute the dimming of LED 2 (possibly the same method but with invert parameter?) Methods loop forever just changing brightness of their LED. Start both threads. Thread.Sleep(Infinity); To prevent their timing from drifting use thread signaling. Have each thread block and wait at the end of their loop until the other thread also reaches their end before letting the loop restart. You could use two autoresetevents - one for each thread? I'm not sure if there's a potential for deadlock but I can't see how (assuming you call Set before Wait) but you could attempt to avoid it with a timeout in the wait.

#3 Holfy89

Holfy89

    New Member

  • Members
  • Pip
  • 8 posts

Posted 18 April 2012 - 02:51 PM

Thanks for the help. I think what you've described that needs to be done is a bit beyond me but I do need to get it done for a University Project... Any Tutorials you could recommend to help me with this sort of stuff or it's just going to take me forever >.< Thanks in advance Ry

#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 18 April 2012 - 03:20 PM

What i'm trying to accomplish Is having the two LED's fading alternately but so there's
a continuous flow between them. As if the lights fading from one and into the other.

Do you mean like this

LED1: 0 -- 50% -- 100% -- 50% -- 0 ------------------------- 0
LED2: 0 ------------------------ 0 -- 50% -- 100 % -- 50% -- 0                                   		
or more like this

LED1:   0 ---- 50% -- 100% -- 50% --- 0 ---- 50% -- 100%
LED2:  100% -- 50% --- 0 ---- 50% -- 100% -- 50% ---- 0

IMHO you'd just need to move loops for led[1] outside the second loop.

#5 Holfy89

Holfy89

    New Member

  • Members
  • Pip
  • 8 posts

Posted 18 April 2012 - 03:27 PM

More like the first diagram you have. Oh, okay. I'll have a fiddle with that then. I don't even think the loop I have is even making a difference to how the program operates anyway. God this is stressing me out. Have to have this working for an exhibition by next week lol. Appreciate the help

#6 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 18 April 2012 - 03:38 PM

You could try the following

static void oneAfterAnotherLoop()
{
  int delaytime = 30;
  for(int i = 0; i < 2; i++) // To fade all LEDs, replace '2' with leds.Length
  {
    var led = leds[i];
    for(uint d = 0; d < 100; d++)
    {
      led.SetDutyCycle(d);
      Thread.Sleep(delaytime);
    }
    for(uint d = 100; d > 0; d--)
    {
      led.SetDutyCycle(d);
      Thread.Sleep(delaytime);
    }
  }
}


#7 Holfy89

Holfy89

    New Member

  • Members
  • Pip
  • 8 posts

Posted 18 April 2012 - 03:48 PM

Worked perfectly, I see what I did wrong. Can't thank you enough!

#8 ErikN

ErikN

    Advanced Member

  • Members
  • PipPipPip
  • 119 posts
  • LocationNew York, NY

Posted 18 April 2012 - 04:11 PM

Ah, I misunderstood. I thought you were trying to achieve the CW2's second timing diagram.

#9 Holfy89

Holfy89

    New Member

  • Members
  • Pip
  • 8 posts

Posted 18 April 2012 - 04:19 PM

Nah, sorry my mistake on my poor explaining There anyway to make this program start/activate when a noise is picked up (for example a footstep) via a microphone? obviously I need buy a microphone as well...

#10 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 18 April 2012 - 04:23 PM

Ah, I misunderstood. I thought you were trying to achieve the CW2's second timing diagram.

I guess that could be achieved by removing the top level for(int i = ...) loop and replacing

led.SetDutyCycle(d);
with

leds[0].SetDutyCycle(d);
leds[1].SetDutyCycle(100 - d);


#11 ErikN

ErikN

    Advanced Member

  • Members
  • PipPipPip
  • 119 posts
  • LocationNew York, NY

Posted 18 April 2012 - 04:25 PM

Also true. I'd thought about that this morning. I tend to over-complicate things I guess.

#12 Holfy89

Holfy89

    New Member

  • Members
  • Pip
  • 8 posts

Posted 18 April 2012 - 05:31 PM

any ideas on the microphone guys?

#13 OppaErich

OppaErich

    Member

  • Members
  • PipPip
  • 22 posts
  • LocationEssen, Germany

Posted 19 April 2012 - 11:26 AM

Got a piezo disc 'squealer' ? These can be abused as microphones too.

#14 Holfy89

Holfy89

    New Member

  • Members
  • Pip
  • 8 posts

Posted 19 April 2012 - 12:23 PM

Yeah I have one of those. How exactly can they be used as microphone? I thought they were purely used for generating sound? Any tutorials or coding you could point me to?

#15 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 19 April 2012 - 05:00 PM

Yeah I have one of those. How exactly can they be used as microphone? I thought they
were purely used for generating sound? Any tutorials or coding you could point me to?


Hi,

The discs have a layer of piezo-electric crystal between a sandwich of thin metal on one side and conductive paint/film on the other.
Posted Image
Piezo-electric crystals generate current when they are forced to change shape, and they also change shape when a voltage is applied.

As a loud speaker, you just need to mount the metal disc on something hard to allow it to resonate (like the outside skin of a project's box) and apply an oscillating voltage (ideally AC, but pulsed DC will work e.g. you could use the Netduino PWM output).

As a microphone, you will probably need to connect the disc to an amplifier and provide some gain to get an audio signal big enough to hear. If you wound the gain up a lot and got the biasing right, you might then be able to drive a digital input on the Netduino such that it registers as an interrupt and causes an event handler to run.

This is a good page on Wikipedia about piezoelectric sensors.

I found an Arduino project where they connect a piezo sensor directly to the analogue input of the Arduino and read the voltage periodically to detect a knock.

Posted Image
Note that this requires physical contact rather than sound, so I am not sure it will work for you without some sort of amplifier.

Have Fun! - Paul

#16 Holfy89

Holfy89

    New Member

  • Members
  • Pip
  • 8 posts

Posted 19 April 2012 - 07:27 PM

Yeah I realised for what im trying to achieve id need some sort of amplifier so I've just gone and bought a microphone to make things easier so im going to have to play around with that... Thing is not many people play around with microphones so sample code seems quite hard to come across...




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.