Skip to main content

InduinoX and wireless relays: Part II

In my last post, I played a bit with the wireless relays to turn on a light. In this post I will show how to use LDR to detect when to turn on the light depending on the ambient light.

Lets dive into the code quickly

#define TRUE  1
#define FALSE 0

int LIGHT_PIN = 7;
int SLEEP_TIME = 100;
int LDR_PIN = 3;
int LDR_THRESHOLD_FOR_DARK = 350;
int MAX_COUNT_BEFORE_TOGGLE = 10;

int isLightOn = FALSE;
int toggleCount = 0;

void setup() {
  turnOffDevice(LIGHT_PIN);
  digitalWrite(LIGHT_PIN, LOW);

  Serial.begin(115200);
}

void turnOffDevice(int pin) {
  pinMode(pin, INPUT);
}

void turnOnDevice(int pin) {
  pinMode(pin, OUTPUT);
}

void toggleLight() {
  if (isLightOn) {
    turnOffDevice(LIGHT_PIN);
    isLightOn = FALSE;
  } else {
    turnOnDevice(LIGHT_PIN);
    isLightOn = TRUE;
  }
}

void toggleLightIfNecessary(int input, int threshold) {
  if (input > threshold) {
    toggleCount++;
    if (toggleCount > MAX_COUNT_BEFORE_TOGGLE) {
      toggleLight();
      toggleCount = 0;
    }
  } else {
    toggleCount = 0;
  }
}

void loop()
{
  delay(SLEEP_TIME);
  int ldrReading = 1023 - analogRead(LDR_PIN);
  Serial.println(ldrReading);

  Serial.println(toggleCount);
  if (isLightOn) {
    toggleLightIfNecessary(ldrReading, LDR_THRESHOLD_FOR_DARK);
  } else {
    toggleLightIfNecessary(1023 - ldrReading,
                           1023 - LDR_THRESHOLD_FOR_DARK);
  }
}

So in the loop(), I keep reading the data from the light sensor, invert it and check to see if the light is already turned on. If it is already on and there is enough ambient light, then I turn in off. If the light is off, then it is turned on if the ambient light is not sufficient.

In both cases, I make sure the sensor data is consistently above or below the threshold. Otherwise the light might turn on when some one's shadow causes the light sensor to report a low light etc.

Some things to note:
  1. In this experiment, I made sure that the light sensor is pointed away from the light source and towards a window from where sun light comes into the room.
  2. The threshold value is empirical, based on conditions in my room and my "feeling" of darkness.

Comments

Popular posts from this blog

Attesting General Power of Attorney in SF

Recently I had to go through the motions of getting a General Power of Attorney (GPA) document attested in San Francisco. I am an Indian by birth. My parents were trying to buy a house back in India for me. Since I did not want to travel to India they needed a GPA so that they can act on my behalf to sign all the documents required to buy the house. The problem however is that they needed it urgently because the seller lives in UK and wants to get all the things done quickly so he can go back. My parents send me a GPA document that they obtained from a lawyer. This is a document that will give the power to my parents to buy the said property in the document on my behalf. The lawyer said that I will have to get the document attested at an Indian Consulate in USA. The closest one for me is in SF and I can drive there in about an hour from where I live. So I though it will be like a day's work to get all the things done. I looked up at their  website  for the procedure t...

XBMC / Boxee remote control android app

I have been writing a few android apps over weekends at home and during 20% time at Google. However I never actually released any of them in the android market mainly because they were quick and dirty apps that fit my needs but perhaps would not be appealing to the general public. One such app that I quickly wrote over a couple of weekends is a XBMC remote. The media center that I use at home is XBMC and I have always wanted to have more control and faster access to my media. Using my remote to navigate through the menus is not as fast. Especially when I wanted to queue a lot of music it is very slow. So I wrote this nice little app called "XBMC remote" for my android phone to control XBMC from anywhere :). Give it a try. Search for "xbmc" in android market and install it if you use XBMC as your media center. When you first launch the app you will start with this screen. You will have to setup your web server address, username and password (if required) by ...

gtkdocize not found

If you are ever configuring an app and see the message "gtkdocize not found" in Gentoo, then you need to emerge gtk-doc. I had some hard time figuring this out so I am writing it in my blog for the next time. When I saw that error message I did an "emerge -s gtkdocize". Usually it is that simple in Gentoo. But not this time. The emerge command returned no results at all. Then I searched for gtkdoc and still no luck. After searching in Google, I still did not have a solution. After thinking for a while I decided to try to search for gtk-doc. Bingo! That worked! Interestingly, this is my first post from my Virtual machine :-)