Don’t be Afraid of the Dark

With Halloween just around the corner, I thought we’d go back to electronics for a bit to determine if our little robot is afraid of the dark. First things first, however: He has to know that it’s dark to before he can be afraid of it. Since his lovely set of eyes are actually an ultrasonic rangefinder, they are insensitive to light or dark. Which means he can find his way around in total darkness. And he’s not afraid, since he doesn’t even know if its dark or not!CableSpaghettiNEW

We’ve gotta fix this, and the fix is pretty easy – add a photocell. If you look at the highlighted area of the photo, you’ll see a small round object with a little squiggle on it between a red and an orange wire. That’s his new photocell. A photocell measures ambient light, and gives a reading in terms of resistance. It is small, inexpensive (~$1), and only takes up one additional pin on his controller. Makes you wonder why they charge so much for automatic headlights on luxury cars? Some mysteries we may never solve. With the new photocell we can add any functionality we want: We can make stuff happen if it gets dark (like automatic lights) or if it gets light (we could give him a vampire mode that sends him scurrying for cover if it gets too light 🙂

To test the arduino light sensor, I added a simple functionality – when it gets dark, his red and green light will flash briefly as “navigation lights”, so that hopefully nobody will step on him when it’s dark. The code is pretty simple – one analog input pin is used to read the resistance ( analogread() ), which is on a scale of 0-1023. Zero is “very dark”, and 1023 is “really bright”. If you want something to happen at a specific ambient brightness, you have to play around with the values a little to see what level of brightness you want to trigger some action. Hooking up to a serial monitor via the same USB that you use to download the new code, in order to read the output values can be helpful for this. In my case, I determined that the level of 350 is dim enough to that I want to turn the navigation lights on. The relevant code is shown below. Note that the LEDs are first turned off – this was to create a brief flash without introducing any new delays in the main program – it essentially uses the entire rest of the main program (knowing that on the next loop; 500, the rangefinder function will be called) as a delay, while keeping the functionality together in a block.

  digitalWrite(ledPinG, LOW);
  digitalWrite(ledPinR, LOW);
  if (lightLevel < 350 && main_loop_count == 499) {
    digitalWrite(ledPinG, HIGH);
    digitalWrite(ledPinR, HIGH);
  }

One final point: reading the light level is a one-time event: you have to set up the program to re-read the input every so often to monitor the current level of illumination. I added it to the same loop used to drive the rangefinder for simplicity. Video test is below; note the flashing red and green lights when it gets dark. The other flashing light is the rangefinder.

So there you have it – he now knows if it’s dark or not. So is he afraid of the dark? Don’t know; I haven’t asked him yet. He can be sensitive discussing these things.

Leave a Reply

Your email address will not be published. Required fields are marked *

CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.