# Dimming light – Mitzi Martinez

Dimming Lamp.

My project was to create a lamp that would  dim as you step away from it.

Tried this example from the tutorials from the Arduino site.

Proximity Sensor

While browsing the Arduino forums I bumped into this library for another Sharp proximity sensor. It looks very similar to the one I’m using so I decided to give it a try. The library includes a few examples. One shows how to measure the distance from sensor in centimetres.

This was also helpful:
http://bildr.org/2011/03/various-proximity-sensors-arduino/

Code Example
#include <DistanceGP2Y0A21YK.h>

DistanceGP2Y0A21YK Dist;
int distance;

void setup()
{
Serial.begin(9600);
Dist.begin(A0);
}

void loop()
{
distance = Dist.getDistanceCentimeter();
Serial.print(“\nDistance in centimers: “);
Serial.print(distance);
delay(500); //make it readable
}

http://oomlout.com/PROX/PROX-Guide.pdf
http://bildr.org/2011/03/various-proximity-sensors-arduino/
Forum proximity sensors discussion: http://arduino.cc/forum/index.php/topic,83947.0.html
Sharp proximity sensors Arduino Library: http://code.google.com/p/gp2y0a21yk-library/

Fade + Distance Sensor
Combined both examples to control the fading based on the input from the proximity sensor.

/*
Fade + Distance Sensor

This example shows how to fade an LED on pin 9
using the readings from the distance sensor.

This example code is in the public domain.
*/

#include <DistanceGP2Y0A21YK.h>

DistanceGP2Y0A21YK Dist;

int distance;
int led = 9;

Int led2 = 10// the pin that the LED is attached to
int brightness = 255; // how bright the LED is

// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);

pinMode (led, OUTPUT);

Serial.begin(9600);

// Assign the distance sensor to A0
Dist.begin(A0);
}

// the loop routine runs over and over again forever:
void loop() {

// set the brightness of pins:
analogWrite(led, brightness);

analogWrite(led2, brightness);

// read the distance
distance = Dist.getDistanceCentimeter();

// change the brightness for next time through the loop:
// set brightness to distance x 5 (5cms = 25, 25cms = 120, 30cms = 150…)
brightness = distance * 5;
// make sure values is between 1 and 255
brightness = constrain(brightness, 0, 255);

brightness= abs(brightness-255) //to revert the values and to convert them into absolut values.
delay(700) //to make it more gradual
Serial.print(“\nDistance in centimers: “);
Serial.print(distance);
Serial.print(“\nBrightness: “);
Serial.print(brightness);

delay (100)
}
Here is the layout for the project: Fritzing
Later on I found this tool to document Arduino projects
http://fritzing.org

There is also a similar example using a proximity sensor and leds
http://fritzing.org/projects/distance-sensor-sharp-gp2d150a/

here is the video of the lamp working: