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

Photocell problem


  • Please log in to reply
9 replies to this topic

#1 jlprest

jlprest

    Member

  • Members
  • PipPip
  • 10 posts

Posted 24 April 2012 - 07:10 AM

I'm not sure if this should be in some other forum so I apologize if it's in the wrong place. I am learning how to use a photocell (Sparkfun Mini Photocell sku: SEN-09088) with my new Netduino Plus. I constructed the circuit as attached (my first try at Fritzing so hopefully it makes sense) and have written the following program:
            // Setup
            OutputPort led2 = new OutputPort(Pins.GPIO_PIN_D0,false);
            AnalogInput analog0 = new AnalogInput (Pins.GPIO_PIN_A0);
            
            //Read value of Analog 0
            int light = 0;
            light = analog0.Read();
            
            Debug.Print("Value of light is: " + light.ToString());
            
            //Action depending on voltage of Analog0
            
            while (light > 300)
            {                
                led2.Write(true);
            }
The values Debug reports are about 100 under room light and about 550 with a flashlight shining on the photocell. If I check the voltage with my multimeter it reads 2.94 under room light and ~ 1.8 with the flashlight. For now all I am trying to do is turn the LED on if the light is above a certain value (currently set at 300). What actually happens is if under room light when the program is started the LED will be off and stay off and if the flashlight is over the photocell the LED comes on and stays on. What I want though is constant sampling so the LED will go on and off as the flashlight is moved over and away from the photocell. Perhaps I have the wrong type of loop?

Any suggestions appreciated.

Jackie

Attached Files


Edited by Stefan, 24 April 2012 - 07:22 AM.
Added [code] tags


#2 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 24 April 2012 - 07:56 AM

I guess you'd need something like

// Setup
...
while(true)
{
  light = analog.Read();
  if(light > 300)
  {
    led2.Write(true);
  }
  else
  {
    led2.Write(false);
  }
  Thread.Sleep(500); // Repeat every 500 ms
}


#3 jlprest

jlprest

    Member

  • Members
  • PipPip
  • 10 posts

Posted 25 April 2012 - 06:38 PM

I get the following when starting...

...
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'SecretLabs.NETMF.Diagnostics'
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Users\Jackie\documents\visual studio 2010\Projects\Photocell\Photocell\bin\Debug\le\Photocell.exe', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Secret Labs\Netduino SDK\Assemblies\v4.1\le\SecretLabs.NETMF.Hardware.NetduinoPlus.dll', Symbols loaded.
The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Done.

The thread '<No Name>' (0x1) has exited with code 0 (0x0).
The program '[1] Micro Framework application: Managed' has exited with code 0 (0x0).
Waiting for debug commands...



My device does respond appropriately to whatever the environment was like when the program started but does not respond to any changes, I assume because the program terminates?

#4 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 25 April 2012 - 08:42 PM

Hi Jackie, If you are still using your original program, it will only read the photo cell level once, and turn on the LED if it reads above 300. If instead you use CW2's version, it should loop for ever checking and setting / clearing the LED output every 500ms. Paul

#5 jlprest

jlprest

    Member

  • Members
  • PipPip
  • 10 posts

Posted 25 April 2012 - 11:39 PM

Thanks for the reply. I did make the changes as outlined by CW2 to no avail. A few questions if I may: 1. What exactly is the Analog0.Read returning - I assumed it was voltage. By using Debug.Print my light variable has readings from 100 (under room light) to about 600 with additional light shining on the photocell. Measuring voltage with a voltmeter returns values of 2.96 and 1.6 under the same conditions. 2. Does using Debug.Print have any other effect on program execution such as a breakpoint or some other action? 3. Do the error messages about thread exiting with code 0... have significance and can I deduce where to look for problems from the messages returned? I initially installed Visual Studio C# Express, .net micro framework and the netduino SDK. I then purchased a copy of Visual Studio 2010 Academic (thank goodness for sons in college) and had to uninstall and reinstall the .net micro framework and netduino SDK to get Visual Studio to recognize my Netduino Plus. I don't know if any of this is relevant or not. Thanks for the help. Jackie

#6 Paul Newton

Paul Newton

    Advanced Member

  • Members
  • PipPipPip
  • 724 posts
  • LocationBerkshire, UK

Posted 26 April 2012 - 05:47 AM

Hi Jackie, The analogue read command returns an integer value from the ADC. The value has the range 0 to 1023. At 0V you should get 0. At 3.3V (or higher) you should get1023. (So if you needed a reading in volts, you would have to scale the result * 3.3 / 1023.) I'm not sure about 2 & 3, but I thought my debg output said something similar (I don't have anything to hand to compare). Paul

#7 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 26 April 2012 - 06:58 AM

I did make the changes as outlined by CW2 to no avail.

Could you please show or attach the complete code?

#8 jlprest

jlprest

    Member

  • Members
  • PipPip
  • 10 posts

Posted 26 April 2012 - 05:04 PM

Here is my program code:

namespace Photocell
{
    public class Program
    {
        public static void Main()
        {
            // write your code here
            OutputPort led2 = new OutputPort(Pins.GPIO_PIN_D0, false);
            AnalogInput analog0 = new AnalogInput(Pins.GPIO_PIN_A0);

            //Read value of Analog 0
            int light = 0;
            light = analog0.Read();

            Debug.Print("Value of light is: " + light.ToString());

            //Action depending on voltage of Analog0

            if (light > 300)
            {
                led2.Write(true);
            }
            else
            {
                led2.Write(false);

            }
            Thread.Sleep(500);

        }

    }
}
I have tried commenting the line with Debug.Print with no change.

Here is the messages in the Output Debug window:

Attaching deployed file.

Assembly: Photocell (1.0.0.0) (240 RAM - 588 ROM - 294 METADATA)


Attaching deployed file.

Assembly: SecretLabs.NETMF.Hardware.NetduinoPlus (4.1.0.0) (268 RAM - 816 ROM - 423 METADATA)


Resolving.


Total: (10996 RAM - 90336 ROM - 49787 METADATA)



Total: (10996 RAM - 90336 ROM - 49787 METADATA)


The debugging target runtime is loading the application assemblies and starting execution.
Ready.

'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.1\Assemblies\le\mscorlib.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.1\Assemblies\le\Microsoft.SPOT.Native.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.1\Assemblies\le\Microsoft.SPOT.Hardware.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.1\Assemblies\le\Microsoft.SPOT.Net.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.1\Assemblies\le\System.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.1\Assemblies\le\Microsoft.SPOT.IO.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.1\Assemblies\le\System.IO.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.1\Assemblies\le\Microsoft.SPOT.Hardware.SerialPort.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.1\Assemblies\le\Microsoft.SPOT.Hardware.Usb.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Secret Labs\Netduino SDK\Assemblies\v4.1\le\SecretLabs.NETMF.Hardware.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'SecretLabs.NETMF.Diagnostics'
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Users\Jackie\documents\visual studio 2010\Projects\Photocell\Photocell\bin\Debug\le\Photocell.exe', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Secret Labs\Netduino SDK\Assemblies\v4.1\le\SecretLabs.NETMF.Hardware.NetduinoPlus.dll', Symbols loaded.
The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Value of light is: 601
Done.

Waiting for debug commands...

The thread '<No Name>' (0x1) has exited with code 0 (0x0).
The program '[14] Micro Framework application: Managed' has exited with code 0 (0x0).

Thanks,

Jackie

#9 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 26 April 2012 - 05:16 PM

If you want your code to react to photocell continuously, you have to have there that while(true) loop. Without it, it checks the read value only once and then exits.

#10 jlprest

jlprest

    Member

  • Members
  • PipPip
  • 10 posts

Posted 06 May 2012 - 08:24 AM

Thanks for the help...It works! I was somewhat dense and did not move the photocell.Read() into the while loop as you directed. Once I did this it worked properly. I know remember what was both fascinating and maddening about programming from my dabbling with Basic on my Commodore 64 years ago. I'll have to pay more attention next time. Thanks for your help. Jackie




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.