Analog photocell reading

having a bit of trouble reading the values from a photocell to trigger an LED state high to low. i’ve referenced the wiki here: http://devwiki.electricimp.com/doku.php?id=electricimpapi#pin_class


and cannot figure out what I am doing wrong to read incoming data from the photocell in the log. Here is my code derived from the remote blink function example:

// Blink-O-Matic example code with remote control
 
// Variable to represent LED state
local ledState = 0;
 
// Variable to represent LED inhibit state
local inhibit = 0;
 
// blink function called every 100ms
function blink()
{
    // Check if we’ve been switched off
    if(inhibit)
    {
        // Blinking inhibited, turn off the LED
        hardware.pin9.write(0);
    }
    else
    {
        // Change state
        ledState = ledState?0:1;
 
        // Reflect state to the pin
        hardware.pin9.write(ledState);
 
        // Schedule the next state change
        imp.wakeup(0.1, blink);
    }
}
 
// input class for LED control channel
class input extends InputPort
{
  name = “LED control”
  type = “number”
 
 
 
 
  function set(value)
  {
      if(value == 0)
      {
          // 0 = Inhibit LED operation
          inhibit = 1;
      }
      else
      {
          // 1 = Enable LED operation
          inhibit = 0;
          blink();
      }
  }
}
 
// Configure pin 9 as an open drain output with internal pull up
hardware.pin9.configure(DIGITAL_OUT_OD_PULLUP);
hardware.pin2.configure(ANALOG_IN);
print(hardware.pin2.read()? 0.0:0.1); //reading in the log a photocell values
 
// Register with the server
imp.configure(“Blink-O-Matic”, [input()], []);
 
// Start blinking
blink();
 
// End of code.

So there are a few problems with your code, and possibly your hardware too.


A photocell is just a resistor. If you have a 10k nominal one, you need to put one side of the cell to ground, one side to pin2 in your example, then take a normal 10k resistor and connect it between pin2 and 3v3. As the resistance changes, the photocell acts as the bottom half of a resistor divider and the voltage on pin2 will vary, which is I guess what you’re looking for.

When you read an analog pin, you’ll get a value between 0 and 65535. Whilst you can try treating it as binary as you have here, it’ll very rarely get to exactly zero as there’s always ADC noise.

Next, there’s no such thing as “print”. You want to remove the print line and put this line at the end of the function blink():

server.log(hardware.pin2.read());

…so you can see the value as it changes.

Thanks for the suggestions. the wiring had been fine, but syntax was the issue. Thanks a bunch.


I’ve noticed though when I run the sketch through the code editor from planner, I continuously  need to press “run” to get updates. is their a way to continuously update the log? are the analog values updating regardless ?

Again thanks a bunch for your help.

and here is an updated code. The end result is that if photocell is < said value… do led states.


// Blink-O-Matic example code with remote control
 
// Variable to represent LED state
local ledState = 0;
 
// Variable to represent LED inhibit state
local inhibit = 0;
 
// blink function called every 100ms

function readVal()
{
  hardware.pin2.read();  
   server.log(hardware.pin2.read());
   
}

function blink()
{
    // Check if we’ve been switched off
    if(inhibit)
    {
        // Blinking inhibited, turn off the LED
        hardware.pin9.write(0);
    }
    else
    {
        // Change state
        ledState = ledState?0:1;
 
        // Reflect state to the pin
        hardware.pin9.write(ledState);
 
        // Schedule the next state change
        imp.wakeup(0.1, blink);
    }
}
 
// input class for LED control channel
class input extends InputPort
{
  name = “LED control”
  type = “number”
 
  function set(readVal)
  {
      if(readVal < 2000)
      {
          // 0 = Inhibit LED operation
          inhibit = 1;
      }
      else
      {
          // 1 = Enable LED operation
          inhibit = 0;
          blink();
      }
  }
}
 
// Configure pin 9 as an open drain output with internal pull up
hardware.pin9.configure(DIGITAL_OUT_OD_PULLUP);
 hardware.pin2.configure(ANALOG_IN);
// Register with the server
imp.configure(“Blink-O-Matic”, [input()], []);
 
// Start blinking
blink();
readVal();

 
// End of code.

nevermind! figured it out! silly mistake with not even assigning the “readVal” function!