Swap With Dell 9300

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Thursday, November 10, 2011

InduinoX and wireless relays: Part II

Posted on 3:37 AM by Unknown
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.
Read More
Posted in arduino, ldr, relays, wireless relay | No comments
Newer Posts Older Posts Home
Subscribe to: Comments (Atom)

Popular Posts

  • Installing Boxee on Gentoo: my experience
    You have probably heard of Boxee and Gentoo (come on). I wanted to give Boxee a try, because of all the movies and tv shows that I can watc...
  • InduinoX: Interfacing with LCD
    After my quick Hello World app , I became a bit more adventurous and decided to interface with the LCD screen that was part of the basic ki...
  • InduinoX and wireless relays: Part I
    It has been a while since I received my wireless relay and I finally got some time this weekend to put them to good use. The connections we...
  • IR remotes
    As part of my  home automation project , I also wanted to control my home entertainment system. The controller can be a web interface or an ...
  • InduinoX: IR receiver
    From my previous post , you probably understood how IR remotes work in general. Now lets take a look at how we can read the signals coming f...
  • 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 ...
  • Google Chromium (the open source chrome browser) on Gentoo!
    Chromium is not yet complete no matter what I say here. Please do not post bugs or assume this is the final version of chromium. It still la...
  • What Darwin Never Knew: DNA
    I just finished watching one of the best PBS NOVA episodes - "What Darwin Never Knew". It is exceptionally good. I was finally abl...
  • VirtualBox additions
    I got everything working from with in virtual box, except for the additions. I was able to run VBoxLinuxAdditions.run (from the Guest additi...
  • Building a linux gaming PC: Update 1
    Today I received my 1GB memory, the upgrade I require to transform my media center desktop PC into a moderately powerful gaming PC. I am sti...

Categories

  • 555 timer
  • arduino
  • delay circuit
  • electronics
  • gentoo
  • home automation
  • induinox
  • ir emitter
  • ir receiver
  • ir remote
  • kubuntu
  • lcd
  • ldr
  • leds
  • lucid lynx
  • oscilloscope
  • picoscope
  • probots
  • receiver
  • relay
  • relays
  • scope
  • sensors
  • simple labs
  • sony remote protocol
  • transmitter
  • virtualbox
  • virtualization
  • wireless relay

Blog Archive

  • ▼  2011 (12)
    • ►  December (1)
    • ▼  November (1)
      • InduinoX and wireless relays: Part II
    • ►  October (6)
    • ►  September (4)
  • ►  2010 (5)
    • ►  May (1)
    • ►  March (1)
    • ►  January (3)
  • ►  2009 (10)
    • ►  December (5)
    • ►  November (3)
    • ►  May (1)
    • ►  April (1)
  • ►  2008 (29)
    • ►  August (1)
    • ►  June (1)
    • ►  May (4)
    • ►  April (1)
    • ►  March (1)
    • ►  February (10)
    • ►  January (11)
  • ►  2007 (7)
    • ►  May (5)
    • ►  April (1)
    • ►  March (1)
  • ►  2006 (8)
    • ►  November (1)
    • ►  October (3)
    • ►  September (2)
    • ►  August (2)
Powered by Blogger.

About Me

Unknown
View my complete profile