Sunday, March 1, 2015

Milo's Nightlight Part 1

The other day, prolly the one before this one.. I receive the goodies pictured here:


And today I got one of these things:


It's cold out in the garage and what I need to do with the printer is really a weekend thing.

Sooooo....

Last night before I got the sensor, I spent some time figuring out how to do PWM to control LED brightness and multicolor control. Today with the sensor I could implement my plan.

Milo lays on the bed at night till it's time to go to bed and I kick him out and he goes to his crate. Sometime between 3 and 6 he awakens and asks to get in bed again by whimpering very quietly. I always hear him though he's very quiet- he's a very considerate dog. About two years ago, I started leaving his crate open so I would no longer have to get up to let him in bed. Sometime since then, I have found a way to make the room very dark. Milo can't see it's so dark and I hear him doing this hopping thing trying to get the nerve to jump up in the bed blind and so I still have to wake up to tap the touch light I have by the bed so he can see. No more!

The point is for the arduino to 'see' him walk by and turn on enough light for him to see to jump up and turn it off. AND I want it to gradually come on and gradually fade so it's a gentle thing that doesn't wake me. See where I'm going with this?

After checking this page to see how to plug it in, I modified the code on the page to include My gradually changing pulse width modulation led control. It works nicely and I didn't take a picture because I intend to swap the Arduino Uno out a for a Nano I have on order when it gets here and use the Uno for other experiments and applications. Anywho! The dog sensing nightlight will get a nice case printed from the 3d printer and designed in Blender when the time is right. View this picture in lieu of a picture of my astounding creation:


Here's what the code looks like:

Code:
int pirPin = 3;
int pin = 8;
int val;
float act = 98; //actual brighness
int des = 98;//desired

void setup() {
  pinMode(pin, OUTPUT);
}

void loop() {
  val = digitalRead(pirPin); //read state of the PIR
  
  if (val == LOW) {
   des=0;
  }
  else {
    des=98;
  }
  
  if (des>act && des==98) {act=act+.1;}
  
  if (des<act && des==0) {act=act-.01;}
  
      for (int n = 1; n < 100; n++) {

     if ( n > act ) {digitalWrite(pin, LOW);} else {digitalWrite(pin, HIGH);}
     
      }

}

No comments:

Post a Comment